aboutsummaryrefslogtreecommitdiff
path: root/challenge-008
diff options
context:
space:
mode:
authorJoelle Maslak <jmaslak@antelope.net>2019-05-14 11:23:21 -0400
committerJoelle Maslak <jmaslak@antelope.net>2019-05-14 11:23:21 -0400
commit4a9c5d65770e2cf8dc5da72e416491539a6cec9a (patch)
treed6d135ee200deb42bf11846e4d7b4ef3f0d848b6 /challenge-008
parentaf81fccc7f9cd3bbe2ac530c2e71214e68bc53cf (diff)
downloadperlweeklychallenge-club-4a9c5d65770e2cf8dc5da72e416491539a6cec9a.tar.gz
perlweeklychallenge-club-4a9c5d65770e2cf8dc5da72e416491539a6cec9a.tar.bz2
perlweeklychallenge-club-4a9c5d65770e2cf8dc5da72e416491539a6cec9a.zip
Joelle's Solution for 8.3 in P6
Diffstat (limited to 'challenge-008')
-rwxr-xr-xchallenge-008/joelle-maslak/perl6/ch-3.p685
-rw-r--r--challenge-008/joelle-maslak/perl6/ch-3.readme26
2 files changed, 111 insertions, 0 deletions
diff --git a/challenge-008/joelle-maslak/perl6/ch-3.p6 b/challenge-008/joelle-maslak/perl6/ch-3.p6
new file mode 100755
index 0000000000..0b8f9d97b9
--- /dev/null
+++ b/challenge-008/joelle-maslak/perl6/ch-3.p6
@@ -0,0 +1,85 @@
+#!/usr/bin/env perl6
+use v6;
+
+#
+# Copyright © 2019 Joelle Maslak
+# All Rights Reserved - See License
+#
+
+use Cro::HTTP::Client;
+
+sub MAIN(
+ Str:D :$from,
+ Str:D :$to,
+ Str:D :$subject,
+ Str:D :$body-text,
+ Str:D :$config-file? = $*HOME.add(".mailgun").Str,
+ Str :$domain? is copy,
+) {
+ my $api-key = get-api-key($config-file);
+ $domain //= get-default-domain($config-file);
+
+ send-email(:$api-key, :$domain, :$from, :to($to.split(';')), :$subject, :$body-text);
+}
+
+sub get-api-key(Str:D $config-file -->Str:D) {
+ my @lines = $config-file.IO.lines().grep( *.chars > 0 );
+ die "Config-file ($config-file) must consist of two lines" if @lines.elems ≠ 2;
+
+ return @lines[0];
+}
+
+sub get-default-domain(Str:D $config-file -->Str:D) {
+ my @lines = $config-file.IO.lines().grep( *.chars > 0 );
+ die "Config-file ($config-file) must consist of two lines" if @lines.elems ≠ 2;
+
+ return @lines[1];
+}
+
+# Usage:
+# $api-key = Your Mailgun API key
+# $domain = The domain to use (configured with Mailgun)
+# $from = Source email address
+# @to = Array of destination email addresses
+# $subject = Subject of email
+# $body-text = Text of email
+#
+sub send-email(
+ Str:D :$api-key,
+ Str:D :$domain,
+ Str:D :$from,
+ :@to,
+ Str:D :$subject,
+ Str:D :$body-text,
+) {
+ my $url = "https://api.mailgun.net/v3/$domain/";
+
+ my @body;
+ @body.push: Pair.new('from', $from);
+ @body.push: Pair.new('subject', $subject);
+ @body.push: Pair.new('text', $body-text);
+
+ for @to.unique -> $to {
+ @body.push: Pair.new('to', $to);
+ }
+
+ my $client = Cro::HTTP::Client.new(
+ base-uri => $url,
+ auth => {
+ username => 'api',
+ password => $api-key,
+ },
+ );
+ my $resp = await $client.post('messages',
+ content-type => 'application/x-www-form-urlencoded',
+ body => @body);
+ if $resp.status == 200 { return }
+
+ CATCH {
+ when X::Cro::HTTP::Error {
+ my $body = await .response.body;
+ die "Error from API endpoint: $body<message>";
+ }
+ }
+}
+
diff --git a/challenge-008/joelle-maslak/perl6/ch-3.readme b/challenge-008/joelle-maslak/perl6/ch-3.readme
new file mode 100644
index 0000000000..70916cdd00
--- /dev/null
+++ b/challenge-008/joelle-maslak/perl6/ch-3.readme
@@ -0,0 +1,26 @@
+This assumes there is a config file ~/.mailgun which contains TWO lines:
+ api-key (without the "api" username)
+ domain (something like sandbox1111111111111111111111111111111.mailgun.org)
+
+MODULES REQUIRED:
+ Cro::HTTP::Client
+
+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:
+
+ --domain=<domain> (configured in Mailgun)
+ --from=<from email address>
+ --subject="<subject of email>"
+ --body="<body of email>"
+
+Example:
+
+perl6 ch-3.p6 --from=jmaslak@antelope.net --to=jmaslak@antelope.net \
+ --subject=Test --body-text="Test Message"
+
+No output means things went well. :)
+