Learn by easy steps


It is a common requirement to send email using any scripting language for the web application. The SMTP server of the Gmail account is commonly used for sending email using PHP or Perl or Python. Before May 2022, email can be sent very easily from the local server with any web programming script by enabling less secure app option of the Gmail account and the SMTP server of the Gmail account. But this option has been disabled now from the Gmail account for security purpose. So, the previously written all tutorials are not working now. The alternative option can be used now to send email using Gmail account which has been shown on this tutorial.

Setup the app password:

You have to setup the app password for your Gmail account to send email now. The steps to setup the app password has been given below.


Login to your Gmail account that you will use for sending email.


Go to the security section of the account and enable the two-factor authentication by using a valid mobile number.



Click on the app password or search the word app password to generate a random password. You have to use this password to authenticate your account when sending email using any script.


Set the app name and click on the GENERATE button for creating the password.


The following image will appear after generating the password.


Send the test email:

Two examples have been explained here to send email using PHP script and Perl script.

Example-1: Send email using PHP

Open the php.ini file and modify the file with the following configurations.

sendmail_from =sender@gmail.com

Open the sendmail.ini file and modify the file with the following configurations.

auth_password='app password'

The following script will send email by using PHP mail() function. You have to set the valid receiver email address to check whether the email is sent successfully or not.


$to = "receiver@gmail.com";
$subject = "test email";
$message = "Hello";
$headers = "From: Admin <admin@fahmidasclassroom.com>\r\n";
//Send email
if(mail($to, $subject, $message, $headers))
   echo "Email sent successfully.";
   echo "Unable to send the email.";


Example-2: Send email using Perl

The following script will send an email using Net::SMTP::SSL module of Perl script. You have to set the valid gmail address as the username, app password as the password, and valid receiver email address before executing the script.


use Net::SMTP::SSL;
#Declare subroutine to send email using gmail server

sub sEmail
    my $smtp;
    my $username = 'username@gmail.com';
    my $password = 'app password';
    my $to = 'receiver@gmail.com';
    my $from = 'sender@gmail.com';
    my $sub = 'test email';
    my $msg = 'Hello';

    if (not $smtp = Net::SMTP::SSL->new('smtp.gmail.com', Port =>465))
        die "Server is unreachable.\n";

    $smtp->auth($username, $password) || die "Invalid user.\n";
    $smtp->mail($from. "\n");
    $smtp->datasend($from . "\n");
    $smtp->datasend($to . "\n");
    $smtp->datasend($sub . "\n");



The Gmail configurations to send email using any scripting language has been shown in this tutorial.