PHPMailer cannot send email, client closes connection?

1.5k views Asked by At

Well, I am trying to send a email using the smtp server from my host(hosting2go), and according to one of their FAQ's I have to authenticate using pop3 first, it has to retrieve mails before I can attempt to connect to the smtp server and thats what I did(I think), but unfortunately it still disconnects me from the smtp server for some reason, even more confusing(if I am getting this right) it tells me that the client request to close the connection.

Here is my php code:

 echo 'running';
 require '../PHPMailerAutoload.php';

 $pop3mail = imap_open('{XXXX.hosting2go.nl:110/pop3}', '[email protected]', 'XXX');

// grab a list of all the mail headers
$headers = imap_headers($pop3mail);

// grab a header object for the last message in the mailbox
$last = imap_num_msg($pop3mail);
$header = imap_header($pop3mail, $last);

// grab the body for the same message
echo $body = imap_body($pop3mail, $last);



$mail = new PHPMailer;

$mail->SMTPDebug = 3; 
$mail->Debugoutput = 'html';                              // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP 
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->SMTPSecure = 'tls';                            // zet SMTP naar ssl
$mail->Host = 'XXX.hosting2go.nl';  // Specify main and backup SMTP servers
$mail->Port = 25;                                    // TCP port to connect to
$mail->Username = "[email protected]";                 // SMTP username
$mail->Password = 'XXXX';                           // SMTP password                          


$mail->From = '[email protected]';
$mail->FromName = 'Werktuigenvereniging Ruinerwold';


$mail->AddAddress("[email protected]");


//$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = "testing";
$mail->Body    = "testing";

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
imap_close($pop3mail);

And this is the response that I am getting:

runningConnection: opening to xxx.hosting2go.nl:25, t=300, opt=array ()
Connection: opened
SERVER -> CLIENT: 220 xxx.hosting2go.nl ESMTP
CLIENT -> SERVER: EHLO wtvruinerwoldnieuw.nl
SERVER -> CLIENT: 250-xxx.hosting2go.nl250-STARTTLS250-PIPELINING250     8BITMIME
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 ready for tls
CLIENT -> SERVER: QUIT


Connection: closed
SMTP connect() failed.
Mailer Error: SMTP connect() failed.

That CLIENT-> SERVER: QUIT confuses me but I might not understand it correctly. Weird thing is(or is it?) that the default mail() function build in php works just fine. But I do not want to use that function since its going to be used for newsletters and such as well(not the most important part). What is that function doing different then what I do here?

Update:

Right, I was looking into the response that I got from the PHPMailer and server and happend to come across this documentation about STARTTLS and found out about this:

 If the SMTP client decides that the level of authentication or
 privacy is not high enough for it to continue, it SHOULD issue an
 SMTP QUIT command immediately after the TLS negotiation is complete.

source: https://www.ietf.org/rfc/rfc2487.txt

But is it a server or client problem now. I also tried to use localhost (since the smtp server I am trying to use is on the same server where my website is hosted)(127.0.0.1) with the same result.

Update 2#(Found a solution but so weird): https://stackoverflow.com/a/12410579/4564466

Commenting out :

$mail->isSMTP();

Worked, and I have no clue why, the responses to that solution didnt tell me why it worked. I am not sure if its even the right way to do it, or that I am now doing something what noone should ever do...

The response was as follows:

runningServer -> Client: +OK Hello there. Server -> Client: +OK Password required. Server -> Client: +OK logged in. Message sent!

And I received the mail perfectly fine.

Thanks for any help.

P.S. I don't mind if you try to point me in a direction instead of typing it all out for me, I actually would like it more if you point me in a direction, since I do want to know how it works and why this does not work.

2

There are 2 answers

5
delatbabel On

Your server is trying to put you into TLS mode using STARTTLS. You have to configure PHPMailer to enable this mode. I think that what you need is:

$mail->SMTPSecure = 'tls';
2
Synchro On

What you are describing is called POP-before-SMTP. I've not seen anyone use it for over 20 years!

Don't bother rolling your own code - PHPMailer has built-in support for it. Check out this code example.

That said, I'd recommend finding an ISP that's a little less stone age.