2

I'm trying to send email through an email account managed by G Suite. I keep getting a connection error. I've tried changing various setting and using either tls or ssl, but it doesn't seem to make any difference. I have verified the login credentials, so I know those are not the issue. The emails being sent through this account are working properly, so the MX records are also configured correctly.

require "PHPMailer-master/PHPMailerAutoload.php";
include "info/user.inc";

$name = trim(htmlspecialchars($_POST['name']));
$email = trim(htmlspecialchars($_POST['email']));
$subject = trim(htmlspecialchars($_POST['subject']));
$phone = trim(htmlspecialchars($_POST['phone']));
$extension = "Ext. ".trim(htmlspecialchars($_POST['extension']))."";
if($etension == null) {
    $extension = "";
}

$message = nl2br(trim(htmlspecialchars($_POST["message"])));

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls"; //ssl or tls
$mail->Host = "smtp.gmail.com";
$mail->Port = "587"; //465 or 587
$mail->Username = $user;
$mail->Password = $pass;

$mail->SetFrom($email, $name);
$mail->AddAddress($address);
$mail->AddReplyTo($email);

$mail->WordWrap = 50;
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;

if(!$mail->Send()) {   
    echo "Message could not be sent. Please try again later.";
    echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
    header("Location: ../success.html");
}

With debugging set to 2, I get the following error:

2017-12-04 01:24:36 SERVER -> CLIENT: 220-a2ss55.a2hosting.com ESMTP Exim 4.89 #1 Sun, 03 Dec 2017 20:24:36 -0500 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail. 
2017-12-04 01:24:36 CLIENT -> SERVER: EHLO www.tigertorch.ca 
2017-12-04 01:24:36 SERVER -> CLIENT: 250-a2ss55.a2hosting.com Hello www.tigertorch.ca [68.66.216.5] 250-SIZE 52428800 250-8BITMIME 250-PIPELINING 250-AUTH PLAIN LOGIN 250-STARTTLS 250 HELP 
2017-12-04 01:24:36 CLIENT -> SERVER: STARTTLS 
2017-12-04 01:24:36 SERVER -> CLIENT: 220 TLS go ahead 
2017-12-04 01:24:36 SMTP Error: Could not connect to SMTP host. 
2017-12-04 01:24:36 CLIENT -> SERVER: QUIT 
2017-12-04 01:24:36 SERVER -> CLIENT: 221 a2ss55.a2hosting.com closing connection 
2017-12-04 01:24:36 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent. Please try again later.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Raptor
  • 53,206
  • 45
  • 230
  • 366
  • How about changing the line `$mail->Port = "587";` to `$mail->Port = 587;` ? – Raptor Dec 04 '17 at 01:43
  • It doesn't seem to make any difference. – Jordan Usselman Dec 04 '17 at 01:45
  • For some weird reason changing the host to `tls://smtp.gmail.com` worked for me. – Ionut Tatu Dec 04 '17 at 01:50
  • I actually tried this earlier and it didn't make any difference. I'm currently trying to figure out if this is being caused by a setting on google, but I don't see anything that would prevent the connection. – Jordan Usselman Dec 04 '17 at 01:58
  • There's also an option called "Allow less-secured apps" with google. You might want to have a look there as well. – Ionut Tatu Dec 04 '17 at 02:56
  • I found the problem, and it appears to be my hosting site. They don't allow email through external mail servers unless you have a specific plan for it. Thanks for the help anyways. – Jordan Usselman Dec 05 '17 at 04:22

0 Answers0