How To Send Email Using PHPMailer And GMail [With Example]

How To Send Email Using PHPMailer And GMail [With Example]

By Anand Lagad   December 8, 2017 PHP


Read the whole blog to understand,

“How To Send Email Using PHPMailer And GMail [With Example]”


PHP mail() function is too easy to use but it should have advanced features for sending emails.
But, for now, PHPMailer Library is the best solution for the same. PHPMailer is considered as the world’s most popular library for PHP to send well-structured emails.

PHPMailer is the library for PHP to support full-featured email creation and email sending functionality.

Send Email Using PHPMailer And GMail [With Example]


I am trying to put all the PHPMailer functionality information, download links and other helpful links in this blog to save the time of developers to get the code and all issues resolved in one place.


Some of the best features of PHPMailer Library:


  • PHPMailer is used by many other open-source technologies like WordPress, Joomla, Drupal, Yii and more.


  • PHP mail() function requires local mail server to send the emails whereas   PHPMailer supports non-local mail servers also.


  • Allows plain text as well as HTML content in the email body.


  • PHPMailer supports mail(), Sendmail and SMTP servers. Means we can send emails from PHP by using GMAIL account details! No need to have local mail server details!


  • PHPMailer supports for UTF-8 content and 8bit, base64, binary, and quoted-printable encodings


  • PHPMailer provides SMTP protocol support and authentication over SSL and TLS.


  • PHPMailer supports multiple To, CC, BCC and Reply-to email addresses which mean that we can use an array of emails for the same.


  • And the great thing is that PHPMailer is having active developer community which involved in keeping it up to date.



PHPMailer Installation Using Composer:


  1. Run the below command to install
    composer require phpmailer/phpmailer
  2. You can check the updated composer.json file to see whether its installed or not. Check the below array in composer.json file and check PHPMailer installed,
    "require": {
            "php": ">=5.5.9",
            "phpmailer/phpmailer": "^6.0"
  3. Include PHPMailer LIbrary/Class in code as below,
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;


PHPMailer Installation in PHP Script:



  • Include PHPMailer Library in PHP Script as below,
    require ‘path/to/PHPMailer/

    require ‘path/to/PHPMailer/

    require ‘path/to/PHPMailer/src/SMTP.php’;

     Send Email Using PHPMailer And GMail [ Example ]:

    PROGRAM: Create a simple random password in PHP which will be unbreakable
    // STEP 1. Define the password length
    function unbreakable_password($pass_len=8)  // default value, if length is not provided
    // all the characters which you want to use for password creation
    //shuffle all chars..
    $shuffled_chars= str_shuffle( $all_chars);
    //get first $pass_lennumber of characters from shuffled string as a password
    $password = substr( $shuffled_chars, 0, $pass_len);
    return $password;
    echo $unbreakable_password=unbreakable_password($password_length);

PHPMailer Class Properties:


SMTPDebug –

If you are having problems connecting or sending emails through your SMTP server, the SMTPDebug provides more information about the processing/errors taking place.

Numbers assigned to SMTPDebug property are the debugging levels explained as below,

$mail->SMTPDebug = 0;
– Disable debugging

$mail->SMTPDebug = 1;
– Output messages sent by the client.

$mail->SMTPDebug = 2;
– Output messages sent by the client
– and responses received from the server (this is the most useful setting).

$mail->SMTPDebug = 3;
– Output messages sent by the client
– and responses received from the server (this is the most useful setting)
– and more information about the initial connection – this level can help     diagnose STARTTLS failures.

$mail->SMTPDebug = 3;
– Output messages sent by the client
– and responses received from the server (this is the most useful setting)
– and more information about the initial connection – this level can help diagnose STARTTLS failures.
– and even lower-level information, very verbose.Note: You don’t need to use levels above 2 unless you’re having trouble connecting at all – it will just make output more verbose and more difficult to read.


isSMTP() – Tell PHPMailer to use SMTP


SMTPSecure – Set the encryption system to use – ssl or tls


SMTPAuth– set whether to use SMTP authentication (true/false)


Host – Name of local or non-local mail server hostname


Port- Set the SMTP port number to connect to (587 for authenticated TLS)


setFrom() – Set who the mail is to be sent from


addAddress() – Set who the mail is to be sent to (Name of recipient is Optional)


addReplyTo() – Set an alternative email address to whom recipient can reply


addAttachment – file attachments.
$mail->addAttachment(‘/var/tmp/file.tar.gz’); // Add attachments
$mail->addAttachment(‘/tmp/image.jpg’, ‘new.jpg’); // Optional name


setLanguage() – PHPMailer provides Error messages in 47 languages!

$mail->setLanguage(‘fr’, ‘/optional/path/to/language/directory/’);

Default is English.


Priority – Email priority (1 = High, 3 = Normal, 5 = low)


MsgHTML– Creates the email body from HTML content.


AltBody – Replace the plain text body with one created manually


WordWrap– Sets word wrapping on the body of the message to a given number of characters.


HELPFUL LINKS THAT MIGHT RESOLVE ‘Send email using phpmailer and gmail’ ISSUES:


  1. Improve qmail support
  2. SMTP connect() failed.
  3. Troubleshooting PHPMailer Problems
  4. PHPMailer Simple PHP mail() function
  5. PHPMailer All Examples:




Comment below if you have any doubts…


  • Hi there,

    I wanted to stop by and give my genuine feedback on your website. I really enjoyed to read your content, I think it’s interesting and very well written. I’ve been on your website for an hour or so and very enjoyed it.

    I didn’t find your Instagram page though, do you have one?


  • Leave a Reply

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

    error: Content is protected !!