# using SendGrid's Perl Library# https://github.com/sendgrid/sendgrid-perluseMail::SendGrid;useMail::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);
#!/usr/bin/perlusestrict;useMIME::Entity;useNet::SMTP;# from is your email address# to is who you are sending your email to# subject will be the subject line of your emailmy$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 detailsmy$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 displayedmy$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 filemy$my_file_txt='example.txt';$mime->attach(Path=>$my_file_txt,Type=>'text/txt',Encoding=>'base64')ordie"Error adding !\n";# Login credentialsmy$username='apikey';my$api_key="your_api_key";# Open a connection to the SendGrid mail servermy$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$smtp->mail($from);$smtp->to($to);$smtp->data($mime->stringify);$smtp->quit();