Reputation: 82
I'm trying to send emails using PHPMailer and it works just fine on dev environment (locally - my private leptop) but doesn't work at all on production environment(Azure Host).
This is the code I'm using:
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer(true); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465; // or 587 465-for gmail
$mail->CharSet = 'UTF-8';
$mail->IsHTML(false);
$mail->Username = '***@gmail.com';
$mail->Password = '*****';
$mail->SetFrom('****@gmail.com');
$mail->Subject = 'test phpmailer';
$mail->Body = 'body without html';
$mail->AddAddress('****@gmail.com');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
I was trying to check for solutions on their website as well as general online but I found nothing helpful: Troubleshooting PHPMailer Problems
What I tried and what I see:
On CMD:
ping smtp.gmail.com
Pinging gmail-smtp-msa.l.google.com [108.177.126.108] with 32 bytes of data:
Reply from 108.177.126.108: bytes=32 time=5ms TTL=40
Reply from 108.177.126.108: bytes=32 time=5ms TTL=40
Reply from 108.177.126.108: bytes=32 time=5ms TTL=40
Reply from 108.177.126.108: bytes=32 time=5ms TTL=40
Ping statistics for 108.177.126.108:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 5ms, Maximum = 5ms, Average = 5ms
On CMD 2:
telnet smtp.gmail.com 587
220 smtp.gmail.com ESMTP b11sm6804305edc.8 - gsmtp
Those results don't point on problem so I don't know how to solve it and I'm pretty stuck now.
When I run the code on production this is what I get:
2018-02-25 11:51:48 SMTP ERROR: Failed to connect to server: (0) 2018-02-25 11:51:48 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Does someone knows how to solve this problem?
Upvotes: 0
Views: 903
Reputation: 931
Update for 2021
I found that my host was blocking traffic on ports 465 and 587. Thereby blocking SMPT on SSL or TLS.
Luckily for me I'm using a host that allows me to enable this port. Not all hosts will allow it (Go Daddy). It did require that I contact support and request they open up the port for me. If the system ever does have a major update it might get disabled again though.
Upvotes: 0
Reputation: 3926
Work for me.
If secure domain to SSL
Add
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
Full code
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = "utf-8";
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Username = "[email protected]";
$mail->Password = "password";
$mail->SetFrom('[email protected]', 'xxx');
$mail->addReplyTo('[email protected]', 'yyy');
$mail->addAddress('[email protected]', 'addressName');
$mail->Subject = 'Subject Test';
$mail->msgHTML("Welcome to bamossza.");
$mail->AltBody = 'bamossza.com';
Upvotes: 0
Reputation: 82
After a lot of work and searching all over the web, I recommend to those who use Azure to use third-party service such as SendGrid to send emails.
Upvotes: 0