Debian Codegraph @

eg/sendgrid

raw
1 #!/usr/bin/env perl
2
3 use v5.10;
4
5 use strict;
6 use warnings;
7
8 use Courriel::Builder;
9 use Email::Sender::Simple qw( sendmail );
10 use Email::Sender::Transport::SMTP;
11
12 my $from = '[email protected]';
13 my $to = '[email protected]';
14 my $subject = 'Example Perl Email';
15
16 # We will create an email with both a plain text and HTML body. If the
17 # recipient has an HTML-capable email reader, they'll see that version.
18 my $plain = <<'EOF';
19 Hi, You
20
21 How are you? I'm doing *great*!
22 EOF
23
24 my $html = <<'EOF';
25 <html>
26 <head></head>
27 <body>
28 <p>
29 Hi, You,
30 </p>
31 <p>
32 How are you? I'm doing <strong>great</strong>!
33 </p>
34 </body>
35 </html>
36 EOF
37
38 my $email = build_email(
39 from($from),
40 to($to),
41 subject($subject),
42 plain_body($plain),
43 html_body($html),
44 );
45
46 # Sendgrid login credentials
47 my $username = '[email protected]';
48 my $password = "yourpassword";
49
50 my $transport = Email::Sender::Transport::SMTP->new(
51 host => 'smtp.sendgrid.net',
52 port => 587,
53 sasl_username => $username,
54 sasl_password => $password,
55 helo => 'yourdomain.com',
56 );
57
58 sendmail( $email, { transport => $transport } );
59
60 __END__
61
62 =head1 INSTALLING MODULES
63
64 The Courriel and Email::Sender libraries can be installed from CPAN using any
65 cpan client. For example, using cpanminus
66 (https://metacpan.org/module/App::cpanminus) you would run the following shell
67 command:
68
69 cpanm Courriel Email::Sender
70
71 =head1 SMTP VERSUS A LOCAL MAIL SERVER
72
73 If you really want to ensure that emails get delivered, you need
74 to handle the (rare) case where Sendgrid is unavailable and queue the email
75 for retrying.
76
77 We could try to implement that in this code, but that would get complicated
78 fast, and soon we'd have reimplemented an entire mail server. A better
79 solution is to configure a local mail server running locally to deliver email
80 using Sendgrid. See http://sendgrid.com/docs/Integrate/Mail_Servers/index.html
81 for more details on how to do this. The mail server will queue the email if
82 Sendgrid is unavailable and try to send it again later.
83
84 If you're using a local mail server, you don't need to use
85 Email::Sender::Transport::SMTP at all. You can just use the C<sendmail()>
86 subroutine in its default mode:
87
88 sendmail($email);
89
90 This looks for a sendmail binary program in the path and invokes it to send
91 the email. On Unix systems, this binary should always do the right thing if
92 you've installed a local mail server.
93
94 =head1 AUTHOR
95
96 David Rolsky <[email protected]>
97
98 =head1 LICENSE
99
100 To the extent possible under law, David Rolsky has waived all copyright and
101 related or neighboring rights to this example. This work is published from:
102 United States.
103
104 See http://creativecommons.org/publicdomain/zero/1.0/ for details.