Perl

Send an Email Using Perl

Many modules exist in Perl to send an email from the Ubuntu operating system. The “Net::SMTP::SSL” module is one of them. Any valid SMTP server with the correct username and password is required to send an email using this Perl module. The SMTP server of the Gmail account and the valid username and password are used in this tutorial to send an email using the Perl script from the Ubuntu operating system. However, the Gmail account requires some configurations to send the email using the Gmail SMTP server. The “less secure app” option of any Gmail account is required to enable the sending of email using the Gmail SMTP server. But this option of the Gmail account is removed from the year 2022. Now, you have to set the “App Password” of the Gmail account to send the emails. You have to set this password for the Gmail account which will be used for sending the emails before practicing the examples of this tutorial.

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.

#!/usr/bin/perl

#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.

#!/usr/bin/perl

#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.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.