Different Examples of Sending an Email
The methods of sending an email using the Gmail SMTP server and a Gmail account are shown in this part of the tutorial.
Example 1: Sending a Simple Text Email
Create a Perl file with the following script that sends a simple text email using a Gmail SMTP server. You have to set the valid email address and the password that is set for the app password into the $username and $password variables. The dummy username and password are assigned in the script. The email will not be sent if the app password is not configured properly. Next, set the valid receiver and the sender email addresses to the $to and $from variables to check if the email is sent properly. The other part of the script remains unchanged. A simple text message is sent to the receiver’s email address if the SMTP server is connected and the user’s information is authenticated successfully.
#Import necessary modules
use strict;
use Net::SMTP::SSL;
#Declare subroutine to send email using Gmail server
sub sendEmail
{
#Username and password for authentication
my $username = '[email protected]';
my $password = 'app password';
#Initialize the variables for sending email
my $to = '[email protected]';
my $from = 'Meher Nigar <[email protected]>';
my $subject = 'It is a testing email';
my $message = 'Hello, sending email using Perl.';
#Declare the smtp variable
my $smtpServer;
#Connect with the Gmail smtp server
if (not $smtpServer = Net::SMTP::SSL->new('smtp.gmail.com', Port =>465,Debug => 1))
{
die "Unable to connect with the SMTP server.\n";
}
#Check the username and password are valid
$smtpServer->auth($username, $password) || die "Authentication error.\n";
$smtpServer->mail($from. "\n");
$smtpServer->to($to."\n");
$smtpServer->data();
$smtpServer->datasend("From: " . $from . "\n");
$smtpServer->datasend("To: " . $to . "\n");
$smtpServer->datasend("Subject: " . $subject . "\n");
$smtpServer->datasend("\n");
}
#Call the subroutine
&sendEmail();
Output:
The following similar information appears at the beginning of the output after executing the script if the email was sent successfully:
When you open the receiver email address, you will get the following email in the inbox:
Example 2: Sending an HTML Formatted Email
Create a Perl file with the following script that sends an HTML-formatted email using a Gmail SMTP server. You have to set a valid email address and password like in the previous example. Next, set the valid receiver and sender email addresses. The HTML code is added as the email message in the script. The content type of the email is set to the text/html to send the HTML-formatted email. The other part of the script is the same as the previous example. An HTML-formatted email is sent to the receiver email address if the SMTP server is connected and the user’s information is authenticated successfully.
#Import necessary modules
use strict;
use Net::SMTP::SSL;
#Declare subroutine to send email using Gmail server
sub sendEmail
{
#Username and password for authentication
my $username = '[email protected]';
my $password = 'app password';
#Initialize the variables for sending email
my $to = '[email protected]';
my $from = 'Meher Nigar <[email protected]>';
my $subject = 'Registration completed';
my $message = '<html><body><h1>Welcome to our site</h1></body></html>';
#Declare the smtp variable
my $smtpServer;
#Connect with the Gmail smtp server
if (not $smtpServer = Net::SMTP::SSL->new('smtp.gmail.com', Port =>465,Debug => 1))
{
die "Unable to connect with the SMTP server.\n";
}
#Check the username and password are valid
$smtpServer->auth($username, $password) || die "Authentication error.\n";
$smtpServer->mail($from. "\n");
$smtpServer->to($to."\n");
$smtpServer->data();
$smtpServer->datasend("From: " . $from . "\n");
$smtpServer->datasend("To: " . $to . "\n");
$smtpServer->datasend("Subject: " . $subject . "\n");
#Set the email content type to HTML for sending html formatted email
$smtpServer->datasend("Content-Type: text/html; charset=utf-8\n\n");
$smtpServer->datasend($message."\n");
$smtpServer->dataend();
$smtpServer->quit;
}
#Call the subroutine
&sendEmail();
Output:
The following similar information appears at the beginning of the output after executing the script if the email was sent successfully:
When you open the receiver email address, you will get the following email in the inbox:
Conclusion
The methods of sending an email using Perl through the Gmail SMTP server is not as easy as before after disabling the “less secure app” of the Gmail account. But you can send an email by setting the app password now.