SMTP failure when posting contact form

the mail() function

Moderators: egami, macek, gesf

Post Reply
stevoc
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Oct 16, 2017 11:58 am

Tue Oct 17, 2017 7:25 am

Hello,

I am hoping someone can help me. I would like to have my contact form send emails to a dedicated google mail recipient from a godaddy hosted domain.

I have configured phpmailer to send the mails and configured the domains mx records to gmails and set up the php smtp setting (from what I can see correctly).

I have been googling why this isnt working to the point of I am trying the same things over and over again (without them working). I am hoping someone can help me before I consider packing this lark in and becoming a bus driver.. or something. Here is my php code, seems straight forward enough but I am always getting an alert

"I could not send the email.SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/ ... leshooting"

<?php
/*

*/

require 'PHPMailer-master/PHPMailerAutoload.php';

/*
* CONFIGURING PARAMS
*/

// an email address that will be in the From field of the email.
$fromEmail = 's.......@gmail.com';
$fromName = 'Demo contact form';

// an email address that will receive the email with the output of the form
$sendToEmail = 's...........@gmail.com';
$sendToName = 'Demo contact form';

// subject of the email
$subject = 'New message from contact form';

// smtp credentials and server

$smtpHost = 'smtp.gmail.com';
$smtpUsername = 's..........@gmail.com';
$smtpPassword = '............';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');

// message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';

// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';

/*
* THE SENDING
*/

// if no debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);

try
{

if(count($_POST) == 0) throw new \Exception('Form is empty');

$emailTextHtml = "<h1>You have a new message from your contact form</h1><hr>";
$emailTextHtml .= "<table>";

foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
}
}
$emailTextHtml .= "</table><hr>";
$emailTextHtml .= "<p>Have a nice day,<br>Best,<br>Ondrej</p>";

$mail = new PHPMailer;

$mail->setFrom($fromEmail, $fromName);
$mail->addAddress($sendToEmail, $sendToName); // you can add more addresses by simply adding another line with $mail->addAddress();
$mail->addReplyTo($from);

$mail->isHTML(true);

$mail->Subject = $subject;
$mail->Body = $emailTextHtml;
$mail->msgHTML($emailTextHtml); // this will also create a plain-text version of the HTML email, very handy


$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
$mail->Host = gethostbyname($smtpHost);

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = $smtpHost;

//Password to use for SMTP authentication
$mail->Password = $smtpPassword;


if(!$mail->send()) {
throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
}

$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
// $responseArray = array('type' => 'danger', 'message' => $errorMessage);
$responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}


// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);

header('Content-Type: application/json');

echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
}

User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 315
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Sat Oct 21, 2017 1:21 pm

I was having a similar problem and came across this fix for PHPMailer ->

Code: Select all

        if (filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_URL) == "localhost") {
            $mail->isSmtp(); // Local Host:
            $mail->Port = EMAIL_PORT; // Local Host Port: (Usually 587)
        } else {
            $mail->isSendMail(); // Remote Host:
        }
For some strange reason PHPMailer change the way they handled SMTP and that is why I switched over to SwiftMailer.
Life is a fig newton of your imagination! https://www.pepster.com/index

stevoc
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Oct 16, 2017 11:58 am

Sun Oct 22, 2017 11:03 am

Hey Strider.. really appreciate your input! This has been really driving me mad!! Thanks again dude!!

Post Reply