From 81021ba7000a57056ac75b315ff802e5f4269086 Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Thu, 23 May 2019 21:19:09 -0600 Subject: Add solution for week 9 ch-3 in Perl 5. --- challenge-009/joelle-maslak/perl5/ch-3.pl | 99 +++++++++++++++++++++++++++ challenge-009/joelle-maslak/perl5/ch-3.readme | 27 ++++++++ 2 files changed, 126 insertions(+) create mode 100755 challenge-009/joelle-maslak/perl5/ch-3.pl create mode 100644 challenge-009/joelle-maslak/perl5/ch-3.readme diff --git a/challenge-009/joelle-maslak/perl5/ch-3.pl b/challenge-009/joelle-maslak/perl5/ch-3.pl new file mode 100755 index 0000000000..9c8dcca56d --- /dev/null +++ b/challenge-009/joelle-maslak/perl5/ch-3.pl @@ -0,0 +1,99 @@ +#!/usr/bin/env perl +use v5.26; +use strict; +use warnings; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use autodie; +use Getopt::Long; +use Mojo::Util qw(url_escape); +use Mojo::UserAgent; +use Path::Tiny; +use Perl6::Slurp; + +# +# Copyright (C) 2019 Joelle Maslak +# All Rights Reserved - See License +# + +MAIN: { + my $configfile = path( $ENV{HOME} // "", ".sparkpost" )->stringify(); + my ( $from, $to, $subject, $bodytext, $sandbox ); + GetOptions( + "from=s" => \$from, + "to=s" => \$to, + "subject=s" => \$subject, + "body-text=s" => \$bodytext, + "config-file=s" => \$configfile, + "sandbox" => \$sandbox, + ); + + die("Please provide --from") unless defined($from); + die("Please provide --to") unless defined($to); + die("Please provide --subject") unless defined($subject); + die("Please provide --bodytext") unless defined($bodytext); + + my $apikey = get_api_key($configfile); + + send_email( + apikey => $apikey, + from => $from, + to => [ split(';', $to) ], + subject => $subject, + bodytext => $bodytext, + sandbox => $sandbox, + ); +} + +sub get_api_key($configfile) { + my (@lines) = grep { length($_) } slurp($configfile); + die "Config-file ($configfile) must consist of one line" if @lines != 1; + chomp $lines[0]; + return $lines[0]; +} + +# Usage: +# The send_email function takes a hash with the following keys: +# apikey = Your Sparkpost API key +# from = Source email address +# to = *ARRAY REFERENCE* of destination email addresses +# subject = Subject of email +# bodytext = Text of email +# sandbox = True if we are using the sandbox account +# +sub send_email(%args) { + my $url = 'https://api.sparkpost.com/api/v1/transmissions'; + + my %json = ( + content => { + from => $args{from}, + subject => $args{subject}, + text => $args{bodytext}, + }, + recipients => [], + ); + foreach my $email (sort $args{to}->@*) { + push $json{recipients}->@*, { address => $email }; + } + $json{options} = { sandbox => \1 } if $args{sandbox}; + + my $ua = Mojo::UserAgent->new(); + + $ua->once(start => sub ($ua, $tx) { + $tx->req->headers->header('Authorization' => $args{apikey}); + }); + + my $tx = $ua->post($url, json => \%json); + if ($tx->result->code == 200) { return; } + + my $body = $tx->result->json; + if (exists $body->{errors}[0]{description}) { + die "Error from API endpoint: " . $body->{errors}[0]{description}; + } else { + die "Error from API endpoint: " . $body->{errors}[0]{message}; + } +} + diff --git a/challenge-009/joelle-maslak/perl5/ch-3.readme b/challenge-009/joelle-maslak/perl5/ch-3.readme new file mode 100644 index 0000000000..a94c67b510 --- /dev/null +++ b/challenge-009/joelle-maslak/perl5/ch-3.readme @@ -0,0 +1,27 @@ +This assumes there is a config file ~/.sparkpost which contains ONE line: + api-key (the actual value of the API key, not the string "api-key") + +MODULES REQUIRED: + Mojolicious + Path::Tiny + Perl6::Slurp + +To specify multiple email addresses using command line version of script, use +";" between addresses. + +See the source code for how to use the send-email() API implementation. + +For command line that uses that API implementation, pass the following: + + --from= + --subject="" + --body="" + --sandbox (used to enable sandbox mode) + +Example: + +perl ch-3.pl --from=sandbox@sparkpostbox.com --to=jmaslak@antelope.net \ + --sandbox --subject=Test --body-text="Test Message" + +No output means things went well. :) + -- cgit