1

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}', 'noreply@wtvruinerwoldnieuw.nl', '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 = "noreply@wtvruinerwoldnieuw.nl";                 // SMTP username
$mail->Password = 'XXXX';                           // SMTP password                          


$mail->From = 'noreply@wtvruinerwoldnieuw.nl';
$mail->FromName = 'Werktuigenvereniging Ruinerwold';


$mail->AddAddress("e.zenderink@gmail.com");


//$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.

Community
  • 1
  • 1
  • with TLS you need to send the certificate, try 587 port – volkinc Feb 13 '15 at 18:48
  • Unfortunately my host does not have that port open, but I tried it anyway and this is the response; runningConnection: opening to xxx.hosting2go.nl:587, t=300, opt=array () SMTP ERROR: Failed to connect to server: Connection refused (111) SMTP connect() failed. Mailer Error: SMTP connect() failed. , Funny thing is that the default mail() function works so I might use that one, but I know that its not the best thing to use for mail. – eldin zenderink Feb 13 '15 at 18:55
  • Mail() just submits to your local mail server, so it will just fail later. – Synchro Feb 13 '15 at 20:13

2 Answers2

1

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.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • wow, then it's indeed a stone age webhost(isp is a internet service provider right, or am I now being stupid?), I wished that I did some more research on it before taking a year contract(insert facepalm), but they also announced that they are working on getting ssl support, but for the main time I would still like to get mail working. I already knew about that function in PHPMailer, but my webhost also requires retrieving email before attempting a connection to the smtp server, PHPMailer only makes the connection to the pop server, making it lighter then other fully useable pop3 libraries. – eldin zenderink Feb 13 '15 at 21:56
  • 1
    Thanks, this was the solution for mine problem, you saved my day :D – Bjorn Jan 30 '16 at 12:25
0

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';
delatbabel
  • 3,601
  • 24
  • 29
  • Ah my mistake, I see you already have that. Still, something is going wrong in the TLS connection, possibly an invalid or unsigned certificate. You might try connecting to the server using the openssl command line client and see what happens. – delatbabel Feb 13 '15 at 19:38
  • I guess that my host needs ssl support for that, unfortunately it doesnt... but thanks anyway – eldin zenderink Feb 13 '15 at 19:41
  • Your mail *SERVER* is forcing you into STARTTLS mode. If your PHP *HOST* does not have SSL support then you can't talk to that server. – delatbabel Feb 13 '15 at 19:47
  • my php host and smtp host are on the same server(i know since 127.0.0.1) gives the same result, does that still mean that I need SSL support? – eldin zenderink Feb 15 '15 at 12:09
  • It does mean from a security point of view that you don't need TLS but that's not the issue. The mail server is forcing you to use TLS but your PHP setup doesn't have it. Find another web host, sorry. – delatbabel Feb 15 '15 at 13:00