forked from sendgrid/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperl.html
More file actions
132 lines (107 loc) · 3.85 KB
/
Copy pathperl.html
File metadata and controls
132 lines (107 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
---
layout: page
weight: 0
title: Perl
navigation:
show: true
---
{% anchor h2 %}
Using the sendgrid-perl module
{% endanchor %}
The <a href="http://github.com/sendgrid/sendgrid-perl">sendgrid-perl</a> module is the officially maintained perl module and is our recommended approach.
See the github repo for more detailed information.
{% codeblock lang:perl %}
use warnings;
use strict;
use Mail::SendGrid;
use Mail::SendGrid::Transport::SMTP;
my $sg = Mail::SendGrid->new( from => 'from@example.com',
to => 'to@example.com',
subject => 'Testing',
text => "Some text http://sendgrid.com/\n",
html => '<html><body>Some html
<a href="http://sendgrid.com">SG</a>
</body></html>' );
#disable click tracking filter for this request
$sg->disableClickTracking();
#turn on the unsubscribe filter here with custom values
$sg->enableUnsubscribe( text => "Unsubscribe here: <% %>", html => "Unsubscribe <% here %>" );
#set a category
$sg->header->setCategory('first contact');
#add unique arguments
$sg->header->addUniqueIdentifier( customer => '12345', location => 'somewhere' );
my $trans = Mail::SendGrid::Transport::SMTP->new( username => 'sendgrid_username', password => 'sendgrid_password' );
my $error = $trans->deliver($sg);
die $error if ( $error );
{% endcodeblock %}
{% anchor h2 %}
Without sendgrid-perl
{% endanchor %}
The following code builds a MIME mail message demonstrating all the portions of the SMTP API protocol. To use this example, you will need to have the following perl modules installed:
<ul>
<li><a href="http://search.cpan.org/perldoc?MIME::Entity">MIME::Entity</a></li>
<li><a href="http://search.cpan.org/perldoc?Authen::SASL">Authen::SASL</a></li>
</ul>
{% codeblock lang:perl %}
#!/usr/bin/perl
use strict;
use MIME::Entity;
use Net::SMTP;
# from is your email address
# to is who you are sending your email to
# subject will be the subject line of your email
my $from = 'you@yourdomain.com';
my $to = 'person@youaremailing.com';
my $subject = 'Example Perl Email';
# Create the MIME message that will be sent. Check out MIME::Entity on CPAN for more details
my $mime = MIME::Entity->build(Type => 'multipart/alternative',
Encoding => '-SUGGEST',
From => $from,
To => $to,
Subject => $subject
);
# Create the body of the message (a plain-text and an HTML version).
# text is your plain-text email
# html is your html version of the email
# if the reciever is able to view html emails then only the html
# email will be displayed
my $text = "Hi!\nHow are you?\n";
my $html = <<EOM;
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
</p>
</body>
</html>
EOM
# attach the body of the email
$mime->attach(Type => 'text/plain',
Encoding =>'-SUGGEST',
Data => $text);
$mime->attach(Type => 'text/html',
Encoding =>'-SUGGEST',
Data => $html);
# attach a file
my $my_file_txt = 'example.txt';
$mime->attach ( Path => $my_file_txt,
Type => 'text/txt',
Encoding => 'base64'
) or die "Error adding !\n";
# Login credentials
my $username = 'yourlogin@sendgrid.net';
my $password = "yourpassword";
# Open a connection to the SendGrid mail server
my $smtp = Net::SMTP->new('smtp.sendgrid.net',
Port=> 587,
Timeout => 20,
Hello => "yourdomain.com");
# Authenticate
$smtp->auth($username, $password);
# Send the rest of the SMTP stuff to the server
$smtp->mail($from);
$smtp->to($to);
$smtp->data($mime->stringify);
$smtp->quit();
{% endcodeblock %}