How To Send Email Using Gmail SMTP Server And Swift Mailer Library

Do you want to send your emails using Gmail SMTP server? When we use SMTP server for sending emails, your emails go to the user’s inbox. Basically, it prevents your emails marked as a spam. In this article, we study how to send email using Gmail SMTP server and Swift Mailer library.

Why Need To Use SMTP Server?

For a website, it is necessary to have a component which can send email to users. It can be your newsletter, contact form or registration process where you need to send email as an acknowledgment.

PHP provides a function mail() for sending an emails. However, if your server does not configure mail settings correctly then this method does not work. Another possibility is when we send emails using mail() function, it can be ended up into the spam folder. And landing your email in spam is not what we are looking for.

To overcome in above 2 situations, we should use SMTP server for our website emails.

Using Swift Mailer we can use any SMTP server like Gmail or your own hosting SMTP server for our emails.

Having said that, let’s take a look at how to use Swift Mailer for sending emails.

Installation

In order to use Swift Mailer library, you should have PHP version 7.0 or higher on your server. If you are using the older version then please upgrade it to the latest PHP version. If you are using the Bluehost hosting, then on request they upgrade your PHP version. You don’t need to invest your time in process of version update.

We recommend to use Composer to install the Swift Mailer library. Open the terminal in your project root directory and run the command below:

composer require swiftmailer/swiftmailer

As we are using Gmail SMTP, we need to change some settings on our Google account. Login to your Google account and click on My Account. Once you are on My Account page then click on Sign-in & Security.

Sign In Security Gmail

On the next page, scroll down to the bottom and you will find ‘Allow less secure apps’ settings. Set it to ON.

Less Secure Apps Gmail

Send Email Using Gmail SMTP Server

At this stage, we are ready with the Swift Mailer library and we changed the Gmail account settings. Now, we are good to go ahead.

Let’s say we created a file sendmail.php where we need to write a code for emails.

sendmail.php

<?php
require_once 'vendor/autoload.php';

// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.googlemail.com', 465, 'ssl'))
  ->setUsername('YOUR_GMAIL_USERNAME')
  ->setPassword('YOUR_GMAIL_PASSWORD')
;

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

// Create a message
$body = 'Hello, <p>Email sent through <span style="color:red;">Swift Mailer</span>.</p>';

$message = (new Swift_Message('Email Through Swift Mailer'))
  ->setFrom(['FROM_EMAIL_ADDRESS' => 'FROM_NAME'])
  ->setTo(['RECEPIENT_1_EMAIL_ADDRESS'])
  ->setCc(['RECEPIENT_2_EMAIL_ADDRESS'])
  ->setBcc(['RECEPIENT_3_EMAIL_ADDRESS'])
  ->setBody($body)
  ->setContentType('text/html')
;

// Send the message
$mailer->send($message);
?>

In the above code, we passed below values for Gmail SMTP server settings.

  • Google SMTP Server Address: smtp.googlemail.com
  • Gmail SMTP Port: 465
  • Encryption: ssl

Apart from these values, you need to change other placeholders like FROM_EMAIL_ADDRESS, FROM_NAME, etc.

After replacing all values, run this file on a browser. You will get the email in the inbox, not in the spam.

Send Single Or Multiple Attachments In An Email

Of course, sometimes we need to send attachments in an email. Using Swift Mailer we can send single or multiple attachments as below:

$message->attach(Swift_Attachment::fromPath(__DIR__. '/sample.png'));
$message->attach(Swift_Attachment::fromPath(__DIR__. '/sample-ebook.pdf'));

All we need to do is use attach method and pass absolute path of the file we need to send as an attachment. Here we assume you need to send sample.png and sample-ebook.pdf as attachments.

Our final code is as follows.

sendmail.php

require_once 'vendor/autoload.php';

// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.googlemail.com', 465, 'ssl'))
  ->setUsername('YOUR_GMAIL_USERNAME')
  ->setPassword('YOUR_GMAIL_PASSWORD')
;

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

// Create a message
$body = 'Hello, <p>Email sent through <span style="color:red;">Swift Mailer</span>.</p>';

$message = (new Swift_Message('Email Through Swift Mailer'))
  ->setFrom(['FROM_EMAIL_ADDRESS' => 'FROM_NAME'])
  ->setTo(['RECEPIENT_1_EMAIL_ADDRESS'])
  ->setCc(['RECEPIENT_2_EMAIL_ADDRESS'])
  ->setBcc(['RECEPIENT_3_EMAIL_ADDRESS'])
  ->setBody($body)
  ->setContentType('text/html')
  ->attach(Swift_Attachment::fromPath(__DIR__. '/sample.png'))
  ->attach(Swift_Attachment::fromPath(__DIR__. '/sample-ebook.pdf'))
;

// Send the message
$mailer->send($message);

We hope you understand how to send email using Gmail SMTP server and Swift Mailer library. We would like to hear your thoughts in the comment section below.

Related Articles

If you liked this article, then please subscribe to our Youtube Channel for video tutorials.

Recommended Tutorials For You

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *