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.
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:
- Run the below command to install
composer require phpmailer/phpmailer
- 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"
},
- 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 ]:
<?php
/*******
PROGRAM: Create a simple random password in PHP which will be unbreakable
******/
// STEP 1. Define the password length
$password_length=10;
// STEP 2. DEFINE AND CALL PASSOWRD GENERATOR FUNCTION
function unbreakable_password($pass_len=8) // default value, if length is not provided
{
// all the characters which you want to use for password creation
$all_chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%abcdefghijklmnopqrstuvwxyz';
//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.
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:
- Improve qmail support
https://github.com/PHPMailer/
PHPMailer/issues/39
- SMTP connect() failed.
https://github.com/PHPMailer/
PHPMailer/issues/270
- Troubleshooting PHPMailer Problems
https://github.com/PHPMailer/
PHPMailer/wiki/Troubleshooting
- PHPMailer Simple PHP mail() function
https://github.com/PHPMailer/
PHPMailer/blob/master/
examples/mail.phps
- PHPMailer All Examples:
https://github.com/PHPMailer/
PHPMailer/tree/master/
examples
DOWNLOAD PHPMAILER
Comment below if you have any doubts…
Comments