phpmailerHow do I configure PHPMailer to send emails using Gmail?
Configuring PHPMailer to send emails using Gmail
- Download and install PHPMailer:
composer require phpmailer/phpmailer
- Create a new
PHPMailer
instance:
$mail = new PHPMailer;
- Configure the SMTP settings:
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_gmail_username';
$mail->Password = 'your_gmail_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
- Set the sender and recipient email addresses:
$mail->setFrom('[email protected]', 'Sender Name');
$mail->addAddress('[email protected]', 'Recipient Name');
- Set the email subject and body:
$mail->Subject = 'Email Subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
- Send the email:
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Output example
Message has been sent
For more information and detailed instructions, please refer to the PHPMailer documentation.
More of Phpmailer
- How can I use PHPMailer with React?
- How can I configure PHPMailer to work with GoDaddy?
- How can I configure PHPMailer to support Polish characters?
- How do I use PHPMailer with Yii2?
- How do I use PHPMailer with OAuth 2.0?
- How do I view the log for my PHPMailer emails?
- How can I use PHPMailer with Mailtrap?
- How do I use PHPMailer to attach a ZIP file?
- How can I set up PHPMailer to use Zimbra SMTP?
- How can I use PHPMailer in Yii 1?
See more codes...