From 673de5aeb33bbe1f4e301a9913a7696a41a3bd25 Mon Sep 17 00:00:00 2001 From: Dave Cross Date: Tue, 2 Feb 2016 13:15:38 +0000 Subject: [PATCH] Add email utility --- README.md | 1 + email_snds_data | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100755 email_snds_data diff --git a/README.md b/README.md index f335fa1..09ccafb 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,4 @@ Utilities --------- get_snds_data - Gets the two current data files for your SNDS account. +email_snds_data - Get the data files and send them to an email address. diff --git a/email_snds_data b/email_snds_data new file mode 100755 index 0000000..af5c48f --- /dev/null +++ b/email_snds_data @@ -0,0 +1,51 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use 5.010; + +use LWP::UserAgent; +use Email::Stuffer; + +my $to = shift || die "No email address given\n"; + +my @urls = qw[ + https://postmaster.live.com/snds/data.aspx + https://postmaster.live.com/snds/ipStatus.aspx +]; +my $key = shift || $ENV{SNDS_KEY}; + +die "No SNDS key given\n" unless defined $key; + +my $ua = LWP::UserAgent->new; + +my $email = Email::Stuffer->new + ->to($to) + ->subject('SDNS Data') + ->from('postmaster@mag-sol.com') + ->text_body('Here is your SNDS data'); + +foreach (@urls) { + if (my $file = get_url($ua, $_, $key)) { + $email->attach_file($file); + } +} + +$email->send_or_die; + +sub get_url { + my ($ua, $url, $key) = @_; + + my $resp = $ua->get("$url?key=$key"); + my $data = $resp->content; + unless (length $data) { + warn "No data for $url\n"; + return; + } + + my $file = $resp->filename; + open my $out_fh, '>', $file or die "$file: $!"; + print $out_fh $data; + + return $file; +}