I am trying to send an email using phpmailer. This is the code that I have written.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'shamir.towsif@gmail.com';
$mail->Password = '*********';
$mail->Port = 25;
$mail->From = 'shamir.towsif@gmail.com';
$mail->FromName = 'Shamir Towsif';
$mail->addAddress('shamir.towsif@gmail.com', 'Shamir Towsif');
$mail->addReplyTo('shamir.towsif@gmail.com', 'Information');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo "Message could not be sent.\n";
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Here is the error that I am getting.
Message could not be sent. Mailer Error: SMTP connect() failed.
What am I doing wrong. The other questions in SO is not helping. Thanks in advance.
I am facing a similar problem, but I think you should try adding this to your code:
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
This is PHPMailer recommended settings for GMail, you can see an example in their Github page .
Title: Sending Email From Server Using PhpMailer & Gmail
Here's how I solved a similar issue:
Download PHPMAILER from github (https://github.com/PHPMailer/PHPMailer) to your computer.
Upload it to your server as .zip file, then extract it by clicking the extract icon. Rename the folder to 'phpmailer' or any name you wish.
Inside the mail sending .php file put the following codes:
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "sender@gmail.com";
$mail->Password = "password";
$mail->setFrom('sender@gmail.com', 'Name');
$mail->addAddress('receiver@yahoo.com', 'Name');
$mail->Subject = 'Subject';
$mail->Body = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Save the php file.
Open your browser and sign in to your gmail(google) account that you used in the php script.
Go to this link: https://support.google.com/accounts/answer/6009563
Expand Your device or app might not support Google’s security standards.
Click Less secure apps.
Toggle Allow less secure apps: to ON (if it is OFF).
Expand 2-Step Verification is not supported by the app
Click the link: https://accounts.google.com/DisplayUnlockCaptcha
Click Continue
Open the email sending php file in the browser.
Now the email will be sent.
This technique worked for me. I hope it'll work for you guys as well.
Happy Coding :-)
Solved an almost identical problem, by adding these lines to the standard PHPMailer configuration. Works like a charm.
$mail->SMTPKeepAlive = true;
$mail->Mailer = “smtp”; // don't change the quotes!
All of the standard settings for SMTP using TLS and default settings for ports etc were being followed. Eventually, I came across this code (from Simon Chen) while researching a solution here, https://webolio.wordpress.com/2008/03/02/phpmailer-and-smtp-on-1and1-shared-hosting/#comment-89