SMTP Autentication

HI Guys

I am using Muse to help me learn webdesign and hope to learn php/ coding as I go along.

I have created a test site and added a contact form. The site was uploaded to my server but the form wont work as it required smtp authentication. I have set smtp autentication via my web panel (password etc…) but now require code to be added to the muse form script. I havent got a clue how to do this as Iam learning. I do have this info from my host Link here and after digging around I’m sure that this code controls the Muse form.

<?php 
/* 	
If you see this text in your browser, PHP is not configured correctly on this webhost. 
Contact your hosting provider regarding PHP configuration for your site.
*/

require_once('form_throttle.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
	if (formthrottle_too_many_submissions($_SERVER["REMOTE_ADDR"]))
	{
		echo '{"MusePHPFormResponse": { "success": false,"error": "Too many recent submissions from this IP"}}';
	} 
	else 
	{
		emailFormSubmission();
	}
} 

function emailFormSubmission()
{
	if(!defined('PHP_EOL'))
		define('PHP_EOL', '\\r\
');

	$to = 'removed';
	$subject = 'Contact Form Submission';
	
	$message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><title>' . htmlentities($subject,ENT_COMPAT,'UTF-8') . '</title></head>';
	$message .= '<body style="background-color: #ffffff; color: #000000; font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; line-height: 18px; font-family: helvetica, arial, verdana, sans-serif;">';
	$message .= '<h2 style="background-color: #eeeeee;">New Form Submission</h2><table cellspacing="0" cellpadding="0" width="100%" style="background-color: #ffffff;">'; 
	$message .= '<tr><td valign="top" style="background-color: #ffffff;"><b>Name:</b></td><td>' . htmlentities($_REQUEST["custom_U205"],ENT_COMPAT,'UTF-8') . '</td></tr>';
	$message .= '<tr><td valign="top" style="background-color: #ffffff;"><b>Email:</b></td><td>' . htmlentities($_REQUEST["Email"],ENT_COMPAT,'UTF-8') . '</td></tr>';
	$message .= '<tr><td valign="top" style="background-color: #ffffff;"><b>Message:</b></td><td>' . htmlentities($_REQUEST["custom_U211"],ENT_COMPAT,'UTF-8') . '</td></tr>';
	$message .= '<tr><td valign="top" style="background-color: #ffffff;"><b>Phone:</b></td><td>' . htmlentities($_REQUEST["custom_U267"],ENT_COMPAT,'UTF-8') . '</td></tr>';

	$message .= '</table><br/><br/>';
	$message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">Form submitted from website: ' . htmlentities($_SERVER["SERVER_NAME"],ENT_COMPAT,'UTF-8') . '</div>';
	$message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">Visitor IP address: ' . htmlentities($_SERVER["REMOTE_ADDR"],ENT_COMPAT,'UTF-8') . '</div>';
	$message .= '</body></html>';
	$message = cleanupMessage($message);
	
	$formEmail = cleanupEmail($_REQUEST['Email']);
	$headers = 'From: removed' . PHP_EOL . 'Reply-To: ' . $formEmail . PHP_EOL .'X-Mailer: Adobe Muse 2014.0.30 with PHP/' . phpversion() .'/'. PHP_OS . PHP_EOL . 'Content-type: text/html; charset=utf-8' . PHP_EOL;
	
	$sent = @mail($to, $subject, $message, $headers);
	
	if($sent)
	{
		echo '{"FormResponse": { "success": true,"redirect":"index.html"}}';

	}
	else
	{
		echo '{"MusePHPFormResponse": { "success": false,"error": "Failed to send email"}}';
	}
}

function cleanupEmail($email)
{
	$email = htmlentities($email,ENT_COMPAT,'UTF-8');
	$email = preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\\
|\\\\r)\\S).*=i', null, $email);
	return $email;
}

function cleanupMessage($message)
{
	$message = wordwrap($message, 70, "\\r\
");
	return $message;
}
?>

Many thanks for any help guys
John

From the link to your host, you can’t use the [fphp]mail[/fphp] function. There are a couple of options. If you want to utilize the example from your host, you’ll want to use the Pear Mail package or you can use [url=https://github.com/Synchro/PHPMailer]PHPMailer, which is one of my favorites.

The homepage for PHPMailer even shows an SMTP authentication example, so it should be relatively easy to see how to replace your @mail(…) with the PHPMailer exmaple.

HI Cpradio

Thanks for the reply. I am a complete novice and dont understand most of what you said in relation to what I need to do.

Do I alter the Adobe muse code or use your suggestion in its place?

Okay, so the two lines in question that need altering are

$headers = 'From: removed' . PHP_EOL . 'Reply-To: ' . $formEmail . PHP_EOL .'X-Mailer: Adobe Muse 2014.0.30 with PHP/' . phpversion() .'/'. PHP_OS . PHP_EOL . 'Content-type: text/html; charset=utf-8' . PHP_EOL;
	
	$sent = @mail($to, $subject, $message, $headers);


They would become (after you download PHPMailer and place it in your folder at the same level as your current script):

require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.mydomain.com'; // update this line
$mail->SMTPAuth = true;
$mail->Username = 'myusername@mydomain.com'; // update this line
$mail->Password = 'mysecretpassword'; // update this line
$mail->SMTPSecure = 'tls';

$mail->From = 'john@level200.com';
$mail->FromName = 'john@level200.com';
$mail->addAddress($to);
$mail->isHTML(true);

$mail->Subject = $subject;
$mail->Body = $message;

$sent = $mail->send();

Ok Think i’m following. With PHPMailer do I download just the PHPMailerAutoload.php file and put in in the same folder? then alter the code as you highlighted?

Yes, visit the releases section at https://github.com/Synchro/PHPMailer/releases

Download the zip file of the latest release. Extract it, and copy it to your server (you can leave it in the PHPMailer-5.2.8 folder if you wish. That won’t harm anything, but if you do, edit the following line from my code:

require 'PHPMailerAutoload.php';

Would then read as (so it includes the folder name)

require 'PHPMailer-5.2.8/PHPMailerAutoload.php';

If you run into issues, post your updated code (remove any username and password/any other private information) and I’ll be glad to review it.

Ok Im on it , Wish me luck lol

BTW thanks for the help

Hmmm sorry cpradio still no luck :frowning:

I uploaded phpmailer into the same folder as the scripts and altered the code as you highlighted, code here:

<?php 
/* 	
If you see this text in your browser, PHP is not configured correctly on this webhost. 
Contact your hosting provider regarding PHP configuration for your site.
*/

require_once('form_throttle.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
	if (formthrottle_too_many_submissions($_SERVER["REMOTE_ADDR"]))
	{
		echo '{"MusePHPFormResponse": { "success": false,"error": "Too many recent submissions from this IP"}}';
	} 
	else 
	{
		emailFormSubmission();
	}
} 

function emailFormSubmission()
{
	if(!defined('PHP_EOL'))
		define('PHP_EOL', '\\r\
');

	$to = 'test@sdfencing.co.uk';
	$subject = 'Contact Form Submission';
	
	$message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><title>' . htmlentities($subject,ENT_COMPAT,'UTF-8') . '</title></head>';
	$message .= '<body style="background-color: #ffffff; color: #000000; font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; line-height: 18px; font-family: helvetica, arial, verdana, sans-serif;">';
	$message .= '<h2 style="background-color: #eeeeee;">New Form Submission</h2><table cellspacing="0" cellpadding="0" width="100%" style="background-color: #ffffff;">'; 
	$message .= '<tr><td valign="top" style="background-color: #ffffff;"><b>Name:</b></td><td>' . htmlentities($_REQUEST["custom_U205"],ENT_COMPAT,'UTF-8') . '</td></tr>';
	$message .= '<tr><td valign="top" style="background-color: #ffffff;"><b>Email:</b></td><td>' . htmlentities($_REQUEST["Email"],ENT_COMPAT,'UTF-8') . '</td></tr>';
	$message .= '<tr><td valign="top" style="background-color: #ffffff;"><b>Message:</b></td><td>' . htmlentities($_REQUEST["custom_U211"],ENT_COMPAT,'UTF-8') . '</td></tr>';
	$message .= '<tr><td valign="top" style="background-color: #ffffff;"><b>Phone:</b></td><td>' . htmlentities($_REQUEST["custom_U267"],ENT_COMPAT,'UTF-8') . '</td></tr>';

	$message .= '</table><br/><br/>';
	$message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">Form submitted from website: ' . htmlentities($_SERVER["SERVER_NAME"],ENT_COMPAT,'UTF-8') . '</div>';
	$message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">Visitor IP address: ' . htmlentities($_SERVER["REMOTE_ADDR"],ENT_COMPAT,'UTF-8') . '</div>';
	$message .= '</body></html>';
	$message = cleanupMessage($message);
	
	$formEmail = cleanupEmail($_REQUEST['Email']);
require 'PHPMailer-5.2.8/PHPMailerAutoload.php';  
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'removed'; // update this line
$mail->SMTPAuth = true;
$mail->Username = 'removed'; // update this line
$mail->Password = 'removed'; // update this line
$mail->SMTPSecure = 'tls';

$mail->From = 'test@sdfencing.co.uk';
$mail->FromName = 'test@sdfencing.co.uk';
$mail->addAddress($to);
$mail->isHTML(true);

$mail->Subject = $subject;
$mail->Body = $message;

$sent = $mail->send();  
	
	if($sent)
	{
		echo '{"FormResponse": { "success": true,"redirect":"index.html"}}';

	}
	else
	{
		echo '{"MusePHPFormResponse": { "success": false,"error": "Failed to send email"}}';
	}
}

function cleanupEmail($email)
{
	$email = htmlentities($email,ENT_COMPAT,'UTF-8');
	$email = preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\\
|\\\\r)\\S).*=i', null, $email);
	return $email;
}

function cleanupMessage($message)
{
	$message = wordwrap($message, 70, "\\r\
");
	return $message;
}
?>

Have I done anything wrong?

Are you getting an errors?

‘Server Encountered an Error’ when i send the form. http://www.sdfencing.co.uk/test/contact.html

Please update the following line:

echo '{"MusePHPFormResponse": { "success": false,"error": "Failed to send email"}}';

To be

echo '{"MusePHPFormResponse": { "success": false,"error": "Failed to send email" . $mail->ErrorInfo}}';

So I can see what the exact error is being shown. Let me know when you have it updated and I’ll explain how I get the error information.

Hi Just done that but got the same error displayed

Oops, I made a silly mistake.

Here is the update I needed applied (my bad)

echo '{"MusePHPFormResponse": { "success": false,"error": "Failed to send email ' . $mail->ErrorInfo . '"}}';  

:frowning:

When running your form, I’m using Chrome (because I like their dev tools). So before I submit the form, I hit F12 to get the Dev Tools opened, go to the Network Tab, then submit the form and you’ll see a request for form-u203.php appear in the Network section. Click on it. Click on Preview and you’ll see the output from your form submission

Done. Any luck?

Yep, it returned “MusePHPFormResponse: {success:false, error:Failed to send email SMTP connect() failed.}”

So it thinks your username and password is incorrect. Be sure those are set to what you use to login to view your emails.

I added the smtp details my host gave me. I’ll check them

Just checked and the smtp mail details are correct. Not sure what to do now?

There are other scripts associated with the form, will it help if i put that code on here too?

Okay, try it after commenting out the following line:

$mail->SMTPSecure = 'tls';

So that line should look like:

//$mail->SMTPSecure = 'tls';

That worked!

Why did commenting that out work?

TLS would alter how the data gets sent, if TLS isn’t supported (and potentially the port it uses), then it would fail. It seems your host doesn’t use TLS. I only figured that out after watching their video on setting up your email in Thunderbird and they didn’t check the TLS encryption option :slight_smile:

FYI: You may want to alter your error message to be

echo '{"MusePHPFormResponse": { "success": false,"error": "Failed to send email"}}'; 

That way you don’t expose anything unnecessarily.