phpmailerHow can I get PHPMailer to send emails to Gmail?
PHPMailer is a popular open-source library used to send emails with PHP. To send emails to Gmail, you need to configure the PHPMailer object with the correct SMTP settings.
Here's an example code block to send an email to Gmail using PHPMailer:
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'your-gmail-password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Code explanation
- Import PHPMailer classes -
use PHPMailer\PHPMailer\PHPMailer;
anduse PHPMailer\PHPMailer\Exception;
- Load Composer's autoloader -
require 'vendor/autoload.php';
- Instantiate PHPMailer object -
$mail = new PHPMailer(true);
- Configure SMTP settings -
$mail->SMTPDebug = 0;
,$mail->isSMTP();
,$mail->Host = 'smtp.gmail.com';
,$mail->SMTPAuth = true;
,$mail->Username = '[email protected]';
,$mail->Password = 'your-gmail-password';
,$mail->SMTPSecure = 'tls';
and$mail->Port = 587;
- Set recipient and sender -
$mail->setFrom('[email protected]', 'Mailer');
and$mail->addAddress('[email protected]', 'Joe User');
- Set email content -
$mail->isHTML(true);
and$mail->Subject = 'Here is the subject';
and$mail->Body = 'This is the HTML message body <b>in bold!</b>';
- Send email -
$mail->send();
If the email was sent successfully, the output of the code should be Message has been sent
.
For more information, please refer to the official PHPMailer documentation.
More of Phpmailer
- How do I use PHPMailer to attach a ZIP file?
- How can I configure PHPMailer to support Polish characters?
- How to use PHPMailer with PHP 7.4?
- How do I determine which version of PHPMailer I'm using?
- How can I set up PHPMailer to use Zimbra SMTP?
- How do I configure PHPmailer to use Zoho SMTP?
- How can I configure PHPMailer to work with GoDaddy?
- How do I update PHPMailer?
- How do I use PHPMailer with IMAP?
- How can I use PHPMailer in Yii 1?
See more codes...