0

I have a site hosted with rackspace and upgraded my site technology from php5.4 to php5.6. Now I can no longer send emails from the site. The worst part is I'm not even getting any errors and the logs show nothing. I am using phpmailer 5.2.14.

Here is my mail script

require 'phpmailer/PHPMailerAutoload.php';
if (isset($_POST['contactForm'])) {
        $email = $_POST['email'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];
        $body = '
        <html>
            <body>
                <div style="float:left; width:100%; margin:0 0 25px 0; padding:20px; background:#222; text-align:center;">
                    <div style="display:inline-block; vertical-align:top;">
                        <a href="http://website.com"><img src="img/logoEmail.png" alt="waesf"></a>
                    </div>
                </div>
                <main style="float:left; width:100%; padding:20px;">
                    <p style="font-family:Arial; font-size:18px;">'.$message.'</p>
                </main>
            </body>
        </html>';
        $mail = new PHPMailer;

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

        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        $mail->isSMTP();                                                                // Set mailer to use SMTP
        $mail->Host = 'smtp.office365.com';                                             // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                                                         // Enable SMTP authentication
        $mail->Username = 'person@email.com';                                 // SMTP username
        $mail->Password = 'secret';                                                // SMTP password
        $mail->SMTPSecure = 'tls';                                                      // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                                              // TCP port to connect to

        $mail->SetFrom('person@email.com', 'Ballpoint Machinist');
        $mail->AddAddress('person@email.com', "BPM");                         // Add a recipient
        $mail->addReplyTo('person@email.com', 'Information');
        // $mail->addCC('cc@example.com');
        // $mail->addBCC('bcc@example.com');

        // $mail->addAttachment('/var/tmp/file.tar.gz');                                // Add attachments
        // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');                           // Optional name
        $mail->isHTML(true);                                                            // Set email format to HTML

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

        if (!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'Message has been sent';
        }
    }

When the script runs the site then hangs for a while then gives a says server timed out and nothing else. No php error codes nothing and when I check the logs there are no errors related to my mail code. However this only happens with office 365 when I change the smtp settings to gmail then I at least get php errors on the page.

I have already read a bunch of threads on the subject but I fail to understand. I have also gone through https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting and added the SMTPOptions to exclude ssl but this didn't help.

Im perplexed that I dont get any errors. I have SMTPDebug = 3 and error_reporting(E_ALL).

edit with Willy Pt suggestion. Still doesn't work when script runs server times out.

if (isset($_POST['contactForm'])) {
        $email = $_POST['email'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];
        $body = '
        <html>
            <body>
                <div style="float:left; width:100%; margin:0 0 25px 0; padding:20px; background:#222; text-align:center;">
                    <div style="display:inline-block; vertical-align:top;">
                        <a href="http://website.com"><img src="img/logoEmail.png" alt="Ballpoint Machinist"></a>
                    </div>
                </div>
                <main style="float:left; width:100%; padding:20px;">
                    <p style="font-family:Arial; font-size:18px;">'.$message.'</p>
                </main>
            </body>
        </html>';
        $mail = new PHPMailer(true);

        // $mail->SMTPDebug = 4;                                                         // Enable verbose debug output

        try {

        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        $mail->isSMTP();                                                                // Set mailer to use SMTP
        $mail->Host = 'smtp.office365.com';                                             // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                                                         // Enable SMTP authentication
        $mail->Username = 'person@person.com';                                 // SMTP username
        $mail->Password = 'test';                                                // SMTP password
        $mail->SMTPSecure = 'tls';                                                      // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                                              // TCP port to connect to

        $mail->SetFrom('person@person.com', 'Ballpoint Machinist');
        $mail->AddAddress('person@person.com', "BPM");                         // Add a recipient
        $mail->addReplyTo('person@person.com', 'Information');
        // $mail->addCC('cc@example.com');
        // $mail->addBCC('bcc@example.com');

        // $mail->addAttachment('/var/tmp/file.tar.gz');                                // Add attachments
        // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');                           // Optional name
        $mail->isHTML(true);                                                            // Set email format to HTML

        $mail->Subject = $subject;
        $mail->Body    = $body;
        $mail->AltBody = $body;
        $mail->send();
        echo "Message Sent OK\n";
        /*if (!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'Message has been sent';
        }*/


        } catch (phpmailerException $e) {
            echo $e->errorMessage(); //Pretty error messages from PHPMailer
        } catch (Exception $e) {
          echo $e->getMessage(); //Boring error messages from anything else!
        }
    }
} 

Can anyone help?

badsyntax
  • 311
  • 4
  • 14
  • Try posting the stacktrace provided by the PHPMailer using the same method as described in this answer http://stackoverflow.com/questions/2386544/error-handling-with-phpmailer It's better reading stacktrace rather than seeing the ErrorInfo only – Willy Pt Jan 18 '16 at 06:59
  • SMTPDebug = 4 is needed to show low level connection info. Was PHP the only thing you upgraded? If the connection times out, there's not really anything PHPMailer can say as there is no other info. Time outs are usually network problems - DNS and firewall. Try the telnet and other checks in the guide. – Synchro Jan 18 '16 at 07:10
  • @WillyPt I added an edit with my code using the suggestion you posted and still get the same problem. When the script runs the site hangs for a while then times out. – badsyntax Jan 18 '16 at 20:03
  • @Synchro setting SMTPDebug = 4 has no effect. The same script worked on live server with php5.4 and it works on my localhost using php5.5. It doesn't work on the live server hosted by rackspace. the live server was updated to php5.6. dns and firewall had no changes – badsyntax Jan 18 '16 at 20:03
  • @synchro Ive seen your name in the other threads and the github page helping other people with similar problems. I have been trying to solve this one on my own but your help is very appreciated. If you want you can see exactly whats happening here (http://ballpointmachinist.com/contact.php). You can try it yourself to see when it times out. – badsyntax Jan 18 '16 at 20:42
  • So use the tests in the guide to narrow down the problem - making the ssl changes in response to a timeout problem is illogical. Does DNS work? Can you telnet? Don't assume anything, test it. – Synchro Jan 18 '16 at 20:44
  • @syncro After reading about dns and telnet I was able to telnet the the smtp server and got this back (220BY2PR11CA0059.outlook.office365.com Microsoft ESMTP MAIL Service ready at Mon, Jan 2016 21:13:36 +0000). I cant install dnsutils. Is their another way to check if dns works? – badsyntax Jan 18 '16 at 21:25
  • You wouldn't have been able to telnet to a named host if your DNS wasn't working. It's very strange that you could telnet there but then have no debug output at all doing the same thing from PHPMailer. – Synchro Jan 19 '16 at 23:07
  • If I switch the site back to php5.4 it works with office365. With php5.6 and office365 I get the timeouts. Using gmail and php5.6 it works. Very confusing – badsyntax Jan 20 '16 at 03:33

2 Answers2

0

Don't know if this counts as an answer but I was able to get this working with gmail. exact same code just use gmail credentials and smtp host instead of office365. I did have to set gmail to allow for less secure apps.

badsyntax
  • 311
  • 4
  • 14
0

The problem is with PHP5.6 and self signed certificate verification. With PHP5.6, certificate verification is enabled by default and certificate can not be self signed.

The correct fix for this is to replace the invalid, misconfigured or self-signed certificate with a good one.

Or, configure it to use a non-self signed certificate:

$mail->SMTPOptions = array(
'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
    )
);

You can also change these settings globally in your php.ini, but that's a really bad idea; PHP 5.6 made this change for very good reasons.

Sometimes this behavior is not quite so apparent; sometimes encryption failures may appear as the client issuing a QUIT immediately after trying to do a STARTTLS. If you see that happen, you should check the state of your certificates or verification settings.

Michael Fever
  • 845
  • 2
  • 8
  • 26