phpmailer / IsSMTP causes 500 error but sends the

2019-08-14 16:23发布

问题:

whenever $mail->isSMTP(); is set in phpmailer it "hangs" for about 1 minute, then it redirects to a: 500 Service Unavailable page with XID: 2060448470.

BUT one minute later i GET the mail that was sent. in the mail i can read: Received-SPF: pass (google.com: domain of admin@skivbasar.se designates 2a00:16d8:0:4::200 as permitted sender)

When i remove "$mail->isSMTP();" the email get sent right away BUT they dont get SPI = pass: spf=none (google.com: 117658@atapa01.citynetwork.se does not designate permitted sender hosts).

I really need SMTP to work since i got a lots of lost mails in the past. I've spent several days looking for a solution including 20+articles here on stackoverflow.

I've re-tried the sample code several times from both worxware and github. It's always "$mail->isSMTP();" that refuses to work propperly.

Here is my mail code:

require("PHPMailerAutoload.php");
require("class.phpmailer.php");
require("class.smtp.php");

$mail = new PHPMailer();
$mail->Host = "mail.skivbasar.se";
$mail->Port = 587;
$mail->SMTPAuth = true;    
$mail->SMTPSecure = "ssl";
$mail->IsSMTP();   
$mail->Username = "-----.se";  // SMTP username
$mail->Password = "-------"; // SMTP password
$mail->AddReplyTo($replyto);
$mail->From     = "admin@skivbasar.se";
$mail->FromName = "skivbasar.se";
$mail->AddAddress($row['email']);
$mail->Subject  = $row['subject'];
$mail->Body     = $row['message'];
$mail->WordWrap = 150;

if(!$mail->Send()) {
    $error = true;
} else {

    $error = false;
}

you can access a testpage to see the error in action at: http://skivbasar.se/action/mail/t3.php

UPPDATE:

Thank you so much for replying Synchro! You seem to be a real phpmailer expert!

i've tried both tls and ssl countless times and even commenting the line and using both port 587 and 465. Both actually works to send mail as long as "isSMTP" is of.

The real problem i'm trying to solve is why the script hangs for 1 minute whenever "isSMTP" is set but still send the mail after redirecting to the 500 page.

the softfail ones without isSMTP is being sent by: from smtp02.mailout.citynetwork.se (mailout.citynetwork.se. [91.123.193.90])

and the passed (but hanged) ones are being sent by: from smtp03.citynetwork.se (mail.citynetwork.se. [91.123.193.200]).

Is feels like the script is waiting for a reply from the mailserver for 60 seconds (like a HELO) and when the server does not respond. it send the mail anyway after 60 minutes. I've read about this somewhere. I also found some entries in the code where it refers to "HELO" like in class.phpmailer.php on line 1340 "// We must resend HELO after tls negotiation".

Since it's redirect to a 500 page i cant print any error messages. I also dont have access to my log. But it works with "isSMTP" on localhost, it's only when the script is run online is hangs like this.

回答1:

Some mistakes here that could cause problems.

You effectively have duplicate requires - you only need to load PHPMailerAutoload.php - get rid of the other two.

You're using 'ssl' but on port 587. That probably won't work - for port 587, set $mail->SMTPSecure = 'tls';.

Your mail service is using TLS - see what happens when you try to connect to them:

telnet mail.skivbasar.se 587
Trying 91.123.193.200...
Connected to mail.skivbasar.se.
Escape character is '^]'.
220 smtp05.citynetwork.se ESMTP Postfix
EHLO localhost
250-smtp05.citynetwork.se
250-PIPELINING
250-SIZE 31457280
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
quit
221 2.0.0 Bye
Connection closed by foreign host.

Notice the STARTTLS in that list of extensions.

If you're going to use PHPMailer, you're best off starting with one of the bundled examples, especially the gmail one, as it will help you avoid silly errors like these.



回答2:

I had the same problem, getting 500 respone on my $mail->send(), but after following Synchros suggestion to look up phpMailers gmail example, i used

$mail->Host = gethostbyname('mail.citynetwork.se');

instead of

$mail->Host = 'mail.citynetwork.se';

and that solved my problem.