PHPmailer can't connect to smtp server

2019-09-02 04:14发布

问题:

I've been using PHPmailer (https://github.com/Synchro/PHPMailer) to send email through amazon SES for a few months. At some time in the last two weeks it has stopped working, and I haven't touched it. I'm getting an error msg:

SMTP Error: Could not connect to SMTP host.

This is my code.

public function sendEmail($to,$subject,$body){

    $mail = new PHPMailer;
    $mail->IsSMTP();     // Set mailer to use SMTP
    $mail->Host = 'amazonaws....';  // Specify main and backup server
    $mail->SMTPAuth = true;  // Enable SMTP authentication    
    $mail->Username = 'mySMTPuname';   // SMTP username
    $mail->Password = 'smtpPword';   // SMTP password
    $mail->SMTPSecure = 'tls';  // Enable encryption, 'ssl' also accepted
    $mail->From = 'example';
    $mail->FromName = 'me';
    $mail->AddAddress($to); // Name is optional
    $mail->IsHTML(true); // Set email format to HTML

    $mail->Subject = $subject;
    $mail->Body    = $body;

    return $mail->Send();       
}

My amazon account is still upto date and active. Is there any way to print out more detailed error msgs for debugging? Has there been any known issues lately?

回答1:

Try :

$mail->SMTPDebug = 1;
// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';


回答2:

This is a very old question but I just had the same problem so it may still be relevant to others. If it stopped working without you changing anything it is probably connected to your hosting company / isp blocking SMTP traffic from your server to other servers. There are a few topics on this, as multiple hosting companies using Cpanel and also Godaddy have implemented such measures for combating spam. Try:

$mail->SMTPDebug = 3;

to get the maximum level of detail on the error. One solution is to use the mail server in the same hosting account (if it blocks SMTP to the outside it probably has an internal service you can use). To keep using Amazon SES you need to open SMTP traffic on your server. IF you have control over Cpanel/WHM "tweak settings" you can do it yourself, otherwise you need to ask your hosting provider. Check this answer for details "Password not accepted from server: 535 Incorrect authentication data" when sending with GMail and phpMailer