Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

v2 API Perl Code Example


(information)

Info

We recommend using SendGrid Perl, our client library, available on GitHub(link takes you to an external page), with full documentation.


Using SendGrid's Perl Library

using-sendgrids-perl-library page anchor

_13
# Using SendGrid's Perl Library
_13
# https://github.com/sendgrid/sendgrid-perl
_13
use Mail::SendGrid;
_13
use Mail::SendGrid::Transport::REST;
_13
_13
my $sendgrid = Mail::SendGrid->new(
_13
from => "test@sendgrid.com",
_13
to => "example@example.com",
_13
subject => "Sending with SendGrid is Fun",
_13
html => "and easy to do anywhere, even with Perl"
_13
);
_13
_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:


_72
# !/usr/bin/perl
_72
_72
use strict;
_72
use MIME::Entity;
_72
use Net::SMTP;
_72
_72
# from is your email address
_72
# to is who you are sending your email to
_72
# subject will be the subject line of your email
_72
my $from = 'you@yourdomain.com';
_72
my $to = 'example@example.com';
_72
my $subject = 'Example Perl Email';
_72
_72
# Create the MIME message that will be sent. Check out MIME::Entity on CPAN for more details
_72
my $mime = MIME::Entity->build(Type => 'multipart/alternative',
_72
Encoding => '-SUGGEST',
_72
From => $from,
_72
To => $to,
_72
Subject => $subject
_72
);
_72
# Create the body of the message (a plain-text and an HTML version).
_72
# text is your plain-text email
_72
# html is your html version of the email
_72
# if the receiver is able to view html emails then only the html
_72
# email will be displayed
_72
my $text = "Hi!\nHow are you?\n";
_72
my $html = <<EOM;
_72
<html>
_72
<head></head>
_72
<body>
_72
<p>Hi!
_72
How are you?
_72
</p>
_72
</body>
_72
</html>
_72
EOM
_72
_72
# attach the body of the email
_72
$mime->attach(Type => 'text/plain',
_72
Encoding =>'-SUGGEST',
_72
Data => $text);
_72
_72
$mime->attach(Type => 'text/html',
_72
Encoding =>'-SUGGEST',
_72
Data => $html);
_72
_72
# attach a file
_72
my $my_file_txt = 'example.txt';
_72
_72
$mime->attach ( Path => $my_file_txt,
_72
Type => 'text/txt',
_72
Encoding => 'base64'
_72
) or die "Error adding !\n";
_72
_72
# Login credentials
_72
my $username = 'example@example.com';
_72
my $api_key = "your_api_key";
_72
_72
# Open a connection to the SendGrid mail server
_72
my $smtp = Net::SMTP->new('smtp.sendgrid.net',
_72
Port=> 587,
_72
Timeout => 20,
_72
Hello => "yourdomain.com");
_72
_72
# Authenticate
_72
$smtp->auth($username, $api_key);
_72
_72
# Send the rest of the SMTP stuff to the server
_72
$smtp->mail($from);
_72
$smtp->to($to);
_72
$smtp->data($mime->stringify);
_72
$smtp->quit();


Rate this page: