Free and Unlimited Web Hosting

How to send email with Gmail SMTP

How to send email with Gmail SMTP

Outgoing email with PHP mail() is heavily restricted on InfinityFree. Fortunately, you can still send email using an external email service. This guide will explain to you how to send email using your Gmail account.

Download PHPMailer

PHPMailer is a popular tool to send email with PHP. By default, it supports sending messages with PHP mail() and SMTP. For this tutorial, we will use version 5 of PHPMailer. Version 6 has already been released, but is a lot harder to install.

To install PHPMailer:

  1. Download PHPMailer: https://github.com/PHPMailer/PHPMailer/archive/5.2-stable.zip
  2. Extract the downloaded zip file on your computer.
  3. Rename the extracted folder to PHPMailer.
  4. Upload the extracted folder to your hosting account with an FTP client of your choice. As always, you should upload the folder to the htdocs folder for the domain you want to use the script on.

Prepare your Gmail account

Gmail supports SMTP by default, but you will need to change some settings to use it.

If your Google account has two factor authentication enabled, you will need to create a new App Specific Password for your website. You can read more about that here: https://support.google.com/accounts/answer/185833

If your Google account does not have two factor authentication enabled, you need to enable access for less secure apps on your account: https://support.google.com/accounts/answer/6010255

Setup your PHPMailer script

The next step is to set up a script to use PHPMailer to send email. Below you will find an example.

To use this example, create a new file in the directory above the directory which contains all the PHPMailer stuff and paste the contents below into it. You must also: - Set the Username and Password parameter. - Set the message content.

<?php

date_default_timezone_set('Etc/UTC');

// Edit this path if PHPMailer is in a different location.
require './PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP();

/*
 * Server Configuration
 */

$mail->Host = 'smtp.gmail.com'; // Which SMTP server to use.
$mail->Port = 587; // Which port to use, 587 is the default port for TLS security.
$mail->SMTPSecure = 'tls'; // Which security method to use. TLS is most secure.
$mail->SMTPAuth = true; // Whether you need to login. This is almost always required.
$mail->Username = "username@gmail.com"; // Your Gmail address.
$mail->Password = "yourpassword"; // Your Gmail login password or App Specific Password.

/*
 * Message Configuration
 */

$mail->setFrom('from@example.com', 'Awesome Website'); // Set the sender of the message.
$mail->addAddress('whoto@example.com', 'John Doe'); // Set the recipient of the message.
$mail->Subject = 'PHPMailer GMail SMTP test'; // The subject of the message.

/*
 * Message Content - Choose simple text or HTML email
 */

// Choose to send either a simple text email...
$mail->Body = 'This is a plain-text message body'; // Set a plain text body.

// ... or send an email with HTML.
//$mail->msgHTML(file_get_contents('contents.html'));
// Optional when using HTML: Set an alternative plain text message for email clients who prefer that.
//$mail->AltBody = 'This is a plain-text message body'; 

// Optional: attach a file
$mail->addAttachment('images/phpmailer_mini.png');

if ($mail->send()) {
    echo "Your message was sent successfully!";
} else {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
Still have questions? Check our forum!