Site icon FahmidasClassroom

How to send email using Gmail account

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.

Step-1:

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

Step-2:

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

Step-3:

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.

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from =sender@gmail.com
mail.add_x_header=On

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

smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=tls
error_logfile=error.log
debug_logfile=debug.log
auth_username='username@gmail.com'
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.

<?php

$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.";
else
   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.

#!/usr/bin/perl

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->to($to."\n");
    $smtp->data();
    $smtp->datasend($from . "\n");
    $smtp->datasend($to . "\n");
    $smtp->datasend($sub . "\n");
    $smtp->datasend("\n");
}

&sEmail();

Conclusion:

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

Exit mobile version