Skip to contentSkip to navigationSkip to topbar
On this page

v2 API Perl Code Example


(warning)

This documentation is for the SendGrid Web API v2

We will deprecate this version on February 10, 2025. To access all the latest features and upcoming developments, please see our v3 API. For assistance with transitioning, refer to our migration guide.

(information)

Using SendGrid's Perl Library

using-sendgrids-perl-library page anchor
1
# Using SendGrid's Perl Library
2
# https://github.com/sendgrid/sendgrid-perl
3
use Mail::SendGrid;
4
use Mail::SendGrid::Transport::REST;
5
6
my $sendgrid = Mail::SendGrid->new(
7
from => "test@sendgrid.com",
8
to => "example@example.com",
9
subject => "Sending with SendGrid is Fun",
10
html => "and easy to do anywhere, even with Perl"
11
);
12
13
Mail::SendGrid::Transport::REST->new( username => $api_user, api_key => $api_key );

If you choose not to use SendGrid's client library you may use Perl's generic SMTP library.

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:

1
# !/usr/bin/perl
2
3
use strict;
4
use MIME::Entity;
5
use Net::SMTP;
6
7
# from is your email address
8
# to is who you are sending your email to
9
# subject will be the subject line of your email
10
my $from = 'you@yourdomain.com';
11
my $to = 'example@example.com';
12
my $subject = 'Example Perl Email';
13
14
# Create the MIME message that will be sent. Check out MIME::Entity on CPAN for more details
15
my $mime = MIME::Entity->build(Type => 'multipart/alternative',
16
Encoding => '-SUGGEST',
17
From => $from,
18
To => $to,
19
Subject => $subject
20
);
21
# Create the body of the message (a plain-text and an HTML version).
22
# text is your plain-text email
23
# html is your html version of the email
24
# if the receiver is able to view html emails then only the html
25
# email will be displayed
26
my $text = "Hi!\nHow are you?\n";
27
my $html = <<EOM;
28
<html>
29
<head></head>
30
<body>
31
<p>Hi!
32
How are you?
33
</p>
34
</body>
35
</html>
36
EOM
37
38
# attach the body of the email
39
$mime->attach(Type => 'text/plain',
40
Encoding =>'-SUGGEST',
41
Data => $text);
42
43
$mime->attach(Type => 'text/html',
44
Encoding =>'-SUGGEST',
45
Data => $html);
46
47
# attach a file
48
my $my_file_txt = 'example.txt';
49
50
$mime->attach ( Path => $my_file_txt,
51
Type => 'text/txt',
52
Encoding => 'base64'
53
) or die "Error adding !\n";
54
55
# Login credentials
56
my $username = 'example@example.com';
57
my $api_key = "your_api_key";
58
59
# Open a connection to the SendGrid mail server
60
my $smtp = Net::SMTP->new('smtp.sendgrid.net',
61
Port=> 587,
62
Timeout => 20,
63
Hello => "yourdomain.com");
64
65
# Authenticate
66
$smtp->auth($username, $api_key);
67
68
# Send the rest of the SMTP stuff to the server
69
$smtp->mail($from);
70
$smtp->to($to);
71
$smtp->data($mime->stringify);
72
$smtp->quit();

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.