title: PHPMailer cannot send email, client closes connection?
body: <p>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.</p>
  
  <p>Here is my php code:</p>
  
  <pre><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-&gt;SMTPDebug = 3; 
  $mail-&gt;Debugoutput = 'html';                              // Enable verbose debug output
  
  $mail-&gt;isSMTP();                                      // Set mailer to use SMTP 
  $mail-&gt;SMTPAuth = true;                               // Enable SMTP authentication
  $mail-&gt;SMTPSecure = 'tls';                            // zet SMTP naar ssl
  $mail-&gt;Host = 'XXX.hosting2go.nl';  // Specify main and backup SMTP servers
  $mail-&gt;Port = 25;                                    // TCP port to connect to
  $mail-&gt;Username = "noreply@wtvruinerwoldnieuw.nl";                 // SMTP username
  $mail-&gt;Password = 'XXXX';                           // SMTP password                          
  
  
  $mail-&gt;From = 'noreply@wtvruinerwoldnieuw.nl';
  $mail-&gt;FromName = 'Werktuigenvereniging Ruinerwold';
  
  
  $mail-&gt;AddAddress("e.zenderink@gmail.com");
  
  
  //$mail-&gt;isHTML(true);                                  // Set email format to HTML
  
  $mail-&gt;Subject = "testing";
  $mail-&gt;Body    = "testing";
  
  //send the message, check for errors
  if (!$mail-&gt;send()) {
      echo "Mailer Error: " . $mail-&gt;ErrorInfo;
  } else {
      echo "Message sent!";
  }
  imap_close($pop3mail);
  </code></pre>
  
  <p>And this is the response that I am getting:</p>
  
  <pre><code>runningConnection: opening to xxx.hosting2go.nl:25, t=300, opt=array ()
  Connection: opened
  SERVER -&gt; CLIENT: 220 xxx.hosting2go.nl ESMTP
  CLIENT -&gt; SERVER: EHLO wtvruinerwoldnieuw.nl
  SERVER -&gt; CLIENT: 250-xxx.hosting2go.nl250-STARTTLS250-PIPELINING250     8BITMIME
  CLIENT -&gt; SERVER: STARTTLS
  SERVER -&gt; CLIENT: 220 ready for tls
  CLIENT -&gt; SERVER: QUIT
  
  
  Connection: closed
  SMTP connect() failed.
  Mailer Error: SMTP connect() failed.
  </code></pre>
  
  <p>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?</p>
  
  <p>Update:</p>
  
  <p>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:</p>
  
  <pre><code> 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.
  </code></pre>
  
  <p>source: <a href="https://www.ietf.org/rfc/rfc2487.txt" rel="nofollow noreferrer">https://www.ietf.org/rfc/rfc2487.txt</a></p>
  
  <p>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. </p>
  
  <p>Update 2#(Found a solution but so weird):
  <a href="https://stackoverflow.com/a/12410579/4564466">https://stackoverflow.com/a/12410579/4564466</a></p>
  
  <p>Commenting out :</p>
  
  <pre><code>$mail-&gt;isSMTP();
  </code></pre>
  
  <p>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...</p>
  
  <p>The response was as follows:</p>
  
  <pre><code>runningServer -&gt; Client: +OK Hello there. Server -&gt; Client: +OK Password required. Server -&gt; Client: +OK logged in. Message sent!
  </code></pre>
  
  <p>And I received the mail perfectly fine.</p>
  
  <p>Thanks for any help. </p>
  
  <p>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.</p>