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 configure PHPMailer to support Polish characters?
- How can I use PHPMailer without SMTP secure?
- How do I determine which version of PHPMailer I'm using?
- How can I configure PHPMailer to work with GoDaddy?
- How can I use PHPMailer with React?
- How can I configure PHPMailer to ignore TLS certificate errors?
- How do I configure the timeout settings for PHPMailer?
- How do I use PHPMailer to send a file?
- How do I use PHPMailer with OAuth 2.0?
- How can I use PHPMailer to send emails through Outlook?
See more codes...