v2 API Perl Code Example

We recommend using SendGrid Perl, our client library, available on GitHub, with full documentation.

Using SendGrid's Perl Library

# Using SendGrid's Perl Library
# https://github.com/sendgrid/sendgrid-perl
use Mail::SendGrid;
use Mail::SendGrid::Transport::REST;

my $sendgrid = Mail::SendGrid->new(
  from => "test@sendgrid.com",
  to => "example@example.com",
  subject => "Sending with SendGrid is Fun",
  html => "and easy to do anywhere, even with Perl"

Mail::SendGrid::Transport::REST->new( username => $api_user, api_key => $api_key );

Without sendgrid-perl

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:


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 = 'example@example.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 receiver is able to view html emails then only the html
# email will be displayed
my $text = "Hi!\nHow are you?\n";
my $html = <<EOM;
       How are you?<br>

# 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 = 'example@example.com';
my $api_key = "your_api_key";

# 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, $api_key);

# Send the rest of the SMTP stuff to the server
Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the SendGrid tag on Stack Overflow.

Thank you for your feedback!

We are always striving to improve our documentation quality, and your feedback is valuable to us. Please select the reason(s) for your feedback or provide additional information about how we can improve:

Sending your feedback...
🎉 Thank you for your feedback!
Something went wrong. Please try again.

Thanks for your feedback!

Refer us and get $10 in 3 simple steps!

Step 1

Get link

Get a free personal referral link here

Step 2

Give $10

Your user signs up and upgrade using link

Step 3

Get $10

1,250 free SMSes
OR 1,000 free voice mins
OR 12,000 chats
OR more