Mefth Loader Gif

How to send emails using a new method of Gmail SMTP using PHP?


Last Updated: 5 months ago
Send SMTP email using Gmail

Want To send emails from your domain using Gmail SMTP server using PHP, here is the solution. And have you tried to send using Google Less secure apps feature and it doesn't work? here is the solution too.

Using Gmail SMTP server you can send about 100-500 emails per day from your domain. Each time When you try to send emails from your web hosting, your web hosting company may limit your outgoing emails per day or min to prevent IP-Address spams behavior or simply you may want to send your custom Gmail account. So in this case you may want to send emails using your Gmail account, here is the solution.

It was working by providing your email and real password but Google removes this feature and replaced it with new way called App passwords.
To let this feature works First:

  • Enable 2FA on your Google account:
    This feature must be enabled first to work with the new email sending SMTP server.
  • Generate App passwords
    App password is a completely different password from your real password but any one can have an access with this password, So to let you access and send emails using your Gmail account SMTP server you must generate this App password. To generate App passwords please click here https://security.google.com/settings/security/apppasswords
Google Generate App password
Keep in mind that once you've created it you can't see your App Password again, So you have to save it in a safe place or you have to regenerate it again.

Now lets do our scripting.

Download the PHPMailer Library

To send emails, you can use mail() function using PHP but this method has does not provide any assistance for making use of features like encryption, authentication, HTML messages, and attachments.

So to have feature like authentication, encryption, HTML messages, and attachments in your email message you need to download it and include the file in your PHP message processor file.

To download the library go to this https://github.com/PHPMailer/PHPMailer

require('PHPMailer/PHPMailer.php'); // MUST ADD THIS      
require('PHPMailer/SMTP.php'); // DON'T REMOVE THIS      
$mail = new PHPMailer(); // CREATE NEW OBJECT      
$mail->IsSMTP(); // ENABLE SMTP      
$mail->SMTPDebug = 0; // DEBUGGING: 1 = ERRORS & MESSEGES, 2 = MESSAGES ONLY      
$mail->SMTPAuth = true; // AUTHENTICATION ENABLED      
$mail->SMTPSecure = 'ssl'; // SECURE TRANSFER ENABLED IS REQUIRED FOR GMAIL      
$mail->Host = "smtp.gmail.com"; // GMAIL SMTP      
$mail->Port = 465; // OR IF THIS NOT WORKING TRY THIS -$gt; 587      
$mail->IsHTML(true); // SET EMAIL FORMAT TO HTML      
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //ENABLE IMPLICIT TLS ENCRYPTION      
$mail->Username = "YOUR_GMAIL@gmail.com";  // YOUR GMAIL      
$mail->Password = "YOUR_GENERATED_APP_PASSWORD"; // YOUR GENERATED PASSWORD       
$mail->SetFrom("YOUR_GMAIL@gmail.com", "USERNAME"); // NAME YOU WANT TO DISPLAY IN THE FROM PLACE OF THE EMAIL MESSAGE      
$mail->Subject = ("SUBJECT"); // SUBJECT OF THE MESSAGE      
$mail->Body = ("THE_MAIN_MESSAGE"); //  MAIN BODY OF THE MESSAGE      
$mail->AddReplyTo("EMAIL_TO_REPLY", "REPLY_USERNAME"); // THIS IS OPTIONAL IF YOU WANT RECIEVE THE REPLY IN DIFFERENT EMAIL ACCOUNT      
$mail->AddAddress("EMAIL_AS_BBC_RECEIVER@gmail.com", "USERNAME"); // THIS IS OPTIONAL THAT IF YOU WANT TO ADD ANOTHER EMAIL TO RECEIVE THE SAME EMAIL AS THE RECEIVER      
$mail->Send() // SEND THE MESSAGE      

Now lets see how the above code works,

First you have to download the files from GitHub and include them at the top of your PHP file that process the email message.

When you integrate these files at the end you can check weither the email message has been sent or not and to do this just call this $mail->Send() within the if else statements.

if($mail->Send()){       
// EMAIL HAS BEEN SENT      
}else{      
// EMAIL NOT SENT      
}      
 

And That's it, I hope you have got the solution if not or have question please let me know it on the comment box.

Happy coding 😀

Writer Image Kifle Tesfay

Full stack Web developer, Founder of Mefth and Userparser.

Report for Comment or Reply




Mefth Loader gif

Login to Write... comment.

You must Login to write Comment.

Subscribe

Get Notified through your email for new Blogs.

You may like these posts