I'm very nearly finished with the website I've been working on for 6 months. It's just come time to deploy to the server.
Unfortunately, i can't get emails to send. Previously I was always testing either locally, or on my personal test server, sending emails with phpmailer and my netfirms email account. Now I'm on the new server, sending with my netfirms account works just fine, but not with the client's namecheap email, through mail.privateemail.com. Here's the code:
<?php
include_once 'db_connect.php';
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
function sendFormEmail($mail, $to, $subject, $senderName, $senderEmail, $message, $sendCopy) {
$_SESSION['token'] = random_string(50);
$subject = filter_var($subject, FILTER_SANITIZE_STRING);
$senderName = filter_var($senderName, FILTER_SANITIZE_STRING);
$senderEmail = filter_var($senderEmail, FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST['body'], FILTER_SANITIZE_STRING);
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.privateemail.com'; // Specify main and backup SMTP servers
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'my-email'; // SMTP username
$mail->Password = 'my-pass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->setFrom('webmaster@mysite.com', 'mysite.com');
$mail->addAddress('my-email', 'me');
$mail->addReplyTo($senderEmail, $senderName);
$mail->Subject = "mysite.com: " . $subject;
$mail->Body = "<h1>You've received a message through the contact form at mysite.com: </h1><br><p style='white-space: pre-wrap;'>" . $message . "</p>";
$mail->AltBody = "You've received a message through the contact form at mysite.com: " . $message;
if($mail->send()) {
return "wooho!";
}
}
}
It keeps returning that the function went through, but no emails. If I change only the host, username, password, and port to those of my netfirms email, it works perfectly. I've also signed into this email address using a third party client to verify I have the correct server settings, and that worked just fine.
Here is the phpmailer debug output:
2016-01-22 23:13:47 SERVER -> CLIENT: 220 PrivateEmail.com Mail Node
2016-01-22 23:13:47 CLIENT -> SERVER: EHLO mysite.com
2016-01-22 23:13:47 SERVER -> CLIENT: 250-mta2.ox.privateemail.com 250-PIPELINING 250-SIZE 81788928 250-ETRN 250-STARTTLS 250-AUTH PLAIN LOGIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN
2016-01-22 23:13:47 CLIENT -> SERVER: STARTTLS
2016-01-22 23:13:47 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS 2016-01-22 23:13:47 CLIENT -> SERVER: EHLO mysite.com
2016-01-22 23:13:47 SERVER -> CLIENT: 250-mta2.ox.privateemail.com 250-PIPELINING 250-SIZE 81788928 250-ETRN 250-AUTH PLAIN LOGIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN
2016-01-22 23:13:47 CLIENT -> SERVER: AUTH LOGIN
2016-01-22 23:13:47 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2016-01-22 23:13:47 CLIENT -> SERVER: d2VibWFzdGVyQGNocmlzY29tcG9zZXMuY29t
2016-01-22 23:13:47 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2016-01-22 23:13:47 CLIENT -> SERVER: U3VwM3ItUzNjcjN0
2016-01-22 23:13:47 SERVER -> CLIENT: 235 2.7.0 Authentication successful
2016-01-22 23:13:47 CLIENT -> SERVER: MAIL FROM:
2016-01-22 23:13:47 SERVER -> CLIENT: 250 2.1.0 Ok
2016-01-22 23:13:47 CLIENT -> SERVER: RCPT TO:
2016-01-22 23:13:47 SERVER -> CLIENT: 250 2.1.5 Ok
2016-01-22 23:13:47 CLIENT -> SERVER: DATA
2016-01-22 23:13:47 SERVER -> CLIENT: 354 End data with .
2016-01-22 23:13:47 CLIENT -> SERVER: Date: Fri, 22 Jan
2016 18:13:46 -0500
2016-01-22 23:13:47 CLIENT -> SERVER: To: me
2016-01-22 23:13:47 CLIENT -> SERVER: From: "mysite.com"
2016-01-22 23:13:47 CLIENT -> SERVER: Reply-To: me
2016-01-22 23:13:47 CLIENT -> SERVER: Subject: mysite.com: test
2016-01-22 23:13:47 CLIENT -> SERVER: Message-ID: <57602f8384a430fc72ff9ba41db09f97@mysite.com>
2016-01-22 23:13:47 CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.13 (https://github.com/PHPMailer/PHPMailer)
2016-01-22 23:13:47 CLIENT -> SERVER: MIME-Version: 1.0
2016-01-22 23:13:47 CLIENT -> SERVER: Content-Type: multipart/alternative;
2016-01-22 23:13:47 CLIENT -> SERVER: boundary="b1_57602f8384a430fc72ff9ba41db09f97"
2016-01-22 23:13:47 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2016-01-22 23:13:47 CLIENT -> SERVER:
2016-01-22 23:13:47 CLIENT -> SERVER: This is a multi-part message in MIME format.
2016-01-22 23:13:47 CLIENT -> SERVER:
2016-01-22 23:13:47 CLIENT -> SERVER: --b1_57602f8384a430fc72ff9ba41db09f97
2016-01-22 23:13:47 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
2016-01-22 23:13:47 CLIENT -> SERVER:
2016-01-22 23:13:47 CLIENT -> SERVER: You've received a message through the contact form at mysite.com: test
2016-01-22 23:13:47 CLIENT -> SERVER:
2016-01-22 23:13:47 CLIENT -> SERVER:
2016-01-22 23:13:47 CLIENT -> SERVER: --b1_57602f8384a430fc72ff9ba41db09f97
2016-01-22 23:13:47 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
2016-01-22 23:13:47 CLIENT -> SERVER:
2016-01-22 23:13:47 CLIENT -> SERVER:
You've received a message through the contact form at mysite.com:
test
2016-01-22 23:13:47 CLIENT -> SERVER: 2016-01-22 23:13:47 CLIENT -> SERVER:
2016-01-22 23:13:47 CLIENT -> SERVER:
2016-01-22 23:13:47 CLIENT -> SERVER: --b1_57602f8384a430fc72ff9ba41db09f97--
2016-01-22 23:13:47 CLIENT -> SERVER:
2016-01-22 23:13:47 CLIENT -> SERVER: .
2016-01-22 23:13:47 SERVER -> CLIENT: 250 2.0.0 Ok: queued as 8B426600ED
2016-01-22 23:13:47 CLIENT -> SERVER: QUIT
2016-01-22 23:13:47 SERVER -> CLIENT: 221 2.0.0 Bye
I've been at this for quite awhile and am really out of ideas. Any help would be appreciated.
Also, I know the client could just make a gmail account and we could probably send through that just fine. I'd just really like to use the email address they paid for, if possible. Starting to really dislike namecheap.
if you don't get errors from the SMTP server, the email is sent but it doesn't reach the recipient mailbox. I think the email was classified as spam, did you configured the SPF record correctly? On my experience this is the most common problem... For example if your production site is mysite.com and the SMTP server domain is privateemail.com, on your mysite.com DNS records you have to add a TXT record like this:
mysite.com. TXT v=spf1 include:privateemail.com +a +mx ~all
I hope this can help
For anyone who has the same problem in the future, there is a dedicated description on the Namecheap site here: https://www.namecheap.com/support/knowledgebase/article.aspx/9751/31/how-to-configure-php-mailsmtp-authentication-for-different-cms
The key difference compared to the default config in my case was that I had to change the port to 587 (like this $mail->Port = 587;
) and it was working immediately.