We recommend using SendGrid Perl, our client library, available on GitHub, with full documentation.
_13# Using SendGrid's Perl Library_13# https://github.com/sendgrid/sendgrid-perl_13use Mail::SendGrid;_13use Mail::SendGrid::Transport::REST;_13_13my $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_13Mail::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_72use strict;_72use MIME::Entity;_72use 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_72my $from = 'you@yourdomain.com';_72my $to = 'example@example.com';_72my $subject = 'Example Perl Email';_72_72# Create the MIME message that will be sent. Check out MIME::Entity on CPAN for more details_72my $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_72my $text = "Hi!\nHow are you?\n";_72my $html = <<EOM;_72<html>_72 <head></head>_72 <body>_72 <p>Hi!_72 How are you?_72 </p>_72 </body>_72</html>_72EOM_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_72my $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_72my $username = 'example@example.com';_72my $api_key = "your_api_key";_72_72# Open a connection to the SendGrid mail server_72my $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();