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,
  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/src/Exception.php’;
    require ‘path/to/PHPMailer/src/PHPMailer.php’;
    require ‘path/to/PHPMailer/src/SMTP.php’;

     Send Email Using PHPMailer And GMail [ Example ]:

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.
Exanmples:
$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!
Example:

$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
    https://github.com/PHPMailer/PHPMailer/issues/39
  2. SMTP connect() failed.
    https://github.com/PHPMailer/PHPMailer/issues/270
  3. Troubleshooting PHPMailer Problems
    https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  4. PHPMailer Simple PHP mail() function
    https://github.com/PHPMailer/PHPMailer/blob/master/examples/mail.phps
  5. PHPMailer All Examples:
    https://github.com/PHPMailer/PHPMailer/tree/master/examples

 

DOWNLOAD PHPMAILER

 

Comment below if you have any doubts…

Blog Summary
How To Send Email Using PHPMailer And GMail [With Example]
Article Name
How To Send Email Using PHPMailer And GMail [With Example]
Description
Explained how to send email using phpmailer and gmail with an example in php. Explained the phpmailer objects in details.
Author
Publisher Name
Anand Lagad- Online Entrepreneur
Publisher Logo

Comments

Visit Us On FacebookVisit Us On PinterestVisit Us On TwitterVisit Us On LinkedinVisit Us On Instagram