[PHP / HTML] Mail form not working - no error

Hi,

I am working on a simple script for capturing data from my form, enter it into a table and then send to an email distribution list. The script worked on my company testserver but I cannot seem to get it to work on a free webhost. It doesnt throw any error despite E_ALL being on…

Would appreciate if someone could look over the script and give me some suggestions, I suspect the error to be related to the mail() function but am not sure.

<html>
<head> </head>

<body>
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);

	if(isset($_POST['name'])) {
		 
		// CHANGE THE TWO LINES BELOW
		$email_to = "[email protected]";
		$email_from = "[email protected]";
		$email_subject = "DADHA - Guest Problem Alert";
		 
		 
		function died($error) 
			{
				// your error code can go here
				echo "<table><tr><img src=\"../../images/iclogo_small.jpg\" style=\"width: 300px\"/></tr><tr><br><strong>ERROR</strong><br />We are very sorry, but there were error(s) found with the form you submitted.</tr>";
				echo "These errors appear below.<br /><br />";
				echo $error."<br /><br />";
				echo "Please go <a href=\"javascript:history.back()\">back</a> and fix these errors.<br /><br />";
				die();
			}
		 
		// validation expected data exists
			if
				(	!isset($_POST['staff_name']) ||
					!isset($_POST['staff_department']) ||
					!isset($_POST['problem']) ||
					!isset($_POST['priority']) ||
					!isset($_POST['gst_name']) ||
					!isset($_POST['gst_room']) ||
					!isset($_POST['incident']) ||
					!isset($_POST['action']) ||
					!isset($_POST['followup']) ||
					!isset($_POST['gst_temp']) ||
					!isset($_POST['case_status']) ||
					!isset($_POST['gst_history'])
				) 
				{
				died('ERROR - There seems to be a problem with the form you submitted.');       
				}
		 
			$name = $_POST['staff_name']; // required
			$department = $_POST['staff_department']; // required
			$problem = $_POST['problem']; // required
			$priority = $_POST['priority']; // not required
			$gst_name = $_POST['gst_name']; // not required
			$gst_room = $_POST['gst_room']; // not required
			$incident = $_POST['incident']; // required
			$action = $_POST['action']; // required
			$followup = $_POST['followup']; // required
			$gst_temp = $_POST['gst_temp']; // required
			$case_status = $_POST['case_status']; // required
			$gst_history = $_POST['gst_history']; // required
			$time = date("F j, Y, g:i a");
			$timestamp = strtotime($time);
			$time_GMT11 = $timestamp + 39600; // 3600 sec. = 1 hour
		
		
			$error_message = "";
			$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
			$string_exp = "/^[A-Za-z .'-]+$/";
			
			if	(strlen($incident) < 2) 
				{
				$error_message .= 'The Incident comments you entered do not appear to be valid.<br />';
				}
			if	(strlen($action) < 2) 
				{
				$error_message .= 'The Action Taken comments you entered do not appear to be valid.<br />';
				}
			if	(strlen($error_message) > 0) 
				{
				died($error_message);
				}

		
	  
		$email_message = "Kindly be informed that a Guest Problem Alert has been raised. Form details below.\n\n"; 
		function clean_string($string) 
			{
			  $bad = array("content-type","bcc:","to:","cc:","href");
			  return str_replace($bad,"",$string);
			}
//		EMAIL BODY - TEXT ONLY NO TABLE		
//		$email_message .= "Date: ".($time_GMT11)."\n"; 
//		$email_message .= "Name: ".clean_string($name)."\n";
//		$email_message .= "Department: ".clean_string($department)."\n";
//		$email_message .= "Problem: ".clean_string($problem)."\n";
//		$email_message .= "Priority: ".clean_string($priority)."\n"."\n";
//		$email_message .= "Guest Name: ".clean_string($gst_name)."\n";
//		$email_message .= "Guest Room: ".clean_string($gst_room)."\n";
//		$email_message .= "Incident: ".clean_string($incident)."\n";
//		$email_message .= "Action: ".clean_string($action)."\n";
//		$email_message .= "Gst. Manner: ".clean_string($gst_temp)."\n";
//		$email_message .= "Updated by: ".clean_string($gst_history)."\n";

//		EMAIL BODY - TABLE
	 $email_message .= '
			<html>
			<head>
			  <title><strong>Guest Problem Alert<strong></title>
			</head>
			<body>
			  <p>A guest problem alert has been raised for your property, please see details below:</p>
			  <table>
				<tr>
				  <th>Item</th><th>Score</th>
				</tr>
				<tr>
				  <td>Date & Time</td><td>'.clean_string($time_GMT11).'</td>
				</tr>
				<tr>
				  <td>Staff Name</td><td>'.clean_string($staff_name).'</td>
				</tr>
				<tr>
				  <td>Staff Department</td><td>'.clean_string($staff_department).'</td>
				</tr>
				<tr>
				  <td>Problem</td><td>'.clean_string($problem).'</td>
				</tr>
				<tr>
				  <td>Priority</td><td>'.clean_string($priority).'</td>
				</tr>
				<tr>
				  <td>Guest Name</td><td>'.clean_string($gst_name).'</td>
				</tr>
				<tr>
				  <td>Incident</td><td>'.clean_string($incident).'</td>
				</tr>
				<tr>
				  <td>Action</td><td>'.clean_string($action).'</td>
				</tr>
				<tr>
				  <td>Follow up</td><td>'.clean_string($followup).'</td>
				</tr>
				<tr>
				  <td>Guest Manner</td><td>'.clean_string($gst_temp).'</td>
				</tr>
				<tr>
				  <td>Case status</td><td>'.clean_string($case_status).'</td>
				</tr>
				<tr>
				  <td>Guest history update</td><td>'.clean_string($history).'</td>
				</tr>
			  </table>
			  <p> All OPEN CASES must be follow up within 1 hour from now. Please consult with the Resort Assistant Manager on-duty for more details.<br /><br />Kind regards,<br />Patrick Maurer<br />Director of QCI<br />
			</body>
			</html>'
			
		 
		// create email headers
		$headers = 'From: '.$email_from."\r\n".
		'Reply-To: '.$email_from."\r\n" .
		'X-Mailer: PHP/' . phpversion();
		mail($email_to, $email_subject, $email_message, $headers);  
?>
 
<!-- place your own success html below -->
 
<font>Thank you for submitting the alert. Please call RAM to raise awareness as well.<br /><a href="javascript:history.back()">Go back</a></font>
 
<?php
}
die();
?>

</body>
</html>

Thanks a lot!
A2k

You’re missing a closing ; at the end of your $email_message var right after the in the string.

i closed it and still no no progress. No error either.

The main problem must be elsewhere since the missing ; would have triggered a default PHP error, but instead I dont get any error at all.

Could this be related to the mail() function in general? I heard its a bit tricky to use…
Thanks,
A2k

Several places you use died instead of die.

if I use died() or die() makes no difference actually. As I mentioned, I tested it earlier on my company server and it works well, it just doesnt want to work on a public host. I did however replace the died’s to die and still cant get it to work…

what really bugs me here:

  1. there is no error message here AT ALL, despite the error messages being turned on
  2. the script works on my company network but not here

I looked up the PHPinfo of my company server and found no difference in there either. whats going on?!

really nobody who can help with this?

Thanks,
A2k

Hello,

Sorry for the late response but are you receiving the email? Looking at your script you don’t test to see if the mail() function sends or not. If you use the following you should see errors if the function doesn’t send properly:

[php]if(!mail($email_to, $email_subject, $email_message, $headers)) {
died(“The message(s) could not be sent at this time.”);
}[/php]

If it is a problem with the mail(), PHP wouldn’t provide you with an error, I don’t think, since the mail() works with the mail application on the web server which is outside of the script and parser and will only return true if it was successful. You said you are using a free web host. Did you see if you are able to send emails? Some free hosting only allows the client to host their site and nothing more. I am sorry no one here has been able to find a solution yet but looking at your script it is syntactically correct and you said it did work on your company’s environment; it probably is the host you are using.

Cheers!

There is no PHP function called died. It is simply die only.

Kevin,

The OP created a function called died in his script. He’s using that function to display his error messages.

Cheers!

Yes, you are correct. I looked back at the code. Probably not the best name for a function since a simple typo can cause problems.

Hi,

thanks for all your replies.

Generally, even if I place an error message, the email is not sent and no error is returned. I compared the phpinfos() of both my company and my free host, and do not find any differences. I have my IT guys setting up another webserver with PHP5 and PEAR now to see if I can get it to work from there. However, it seems that standard PEAR does not support this Mail() function, but rather I need to install PEAR Core Packs + Mail Libraries + SMTP_NET libraries (SMTP because I am now thinking to have the email sent via my local email account).

Still, this bugs me to no end since the earlier script (without SMTP) works on the company server, but freehosts (Such as easybyet or 000webhosts who actually offer free email servers, etc.) just dont seem to run it.

Really no idea. I also tried other contact form scripts from various sources, but dont have any error either.

Can you please post the html form page that is gathering this info so I can test the full process.

Thank you fatsol, I appreciate the interest.

Well, it has been well over a week that I am fighting with this issue, so I decided to give up on using PEAR. Instead I came up with using PHPmailer which seems to at least work, but now I am struggling here with changing the message body…

<?php

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require './scripts/PHPMailer-master/PHPMailerAutoload.php';

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

if(isset($_POST['submit']))
	{

		// validation expected data exists
		if
			(	!isset($_POST['staff_name']) ||
				!isset($_POST['staff_department']) ||
				!isset($_POST['problem']) ||
				!isset($_POST['priority']) ||
				!isset($_POST['gst_name']) ||
				!isset($_POST['gst_room']) ||
				!isset($_POST['incident']) ||
				!isset($_POST['action']) ||
				!isset($_POST['followup']) ||
				!isset($_POST['gst_temp']) ||
				!isset($_POST['case_status']) ||
				!isset($_POST['gst_history'])
			) 
			{
			die('ERROR - There seems to be a problem with the form you submitted.');       
			}
			
			$name = $_POST['staff_name']; // required
			$department = $_POST['staff_department']; // required
			$problem = $_POST['problem']; // required
			$priority = $_POST['priority']; // not required
			$gst_name = $_POST['gst_name']; // not required
			$gst_room = $_POST['gst_room']; // not required
			$incident = $_POST['incident']; // required
			$action = $_POST['action']; // required
			$followup = $_POST['followup']; // required
			$gst_temp = $_POST['gst_temp']; // required
			$case_status = $_POST['case_status']; // required
			$gst_history = $_POST['gst_history']; // required
			$time = date("F j, Y, g:i a");
			$timestamp = strtotime($time);
			$time_GMT11 = $timestamp + 39600; // 3600 sec. = 1 hour
				
			$email_message .= '<html>
			<head>
			  <title><strong>Guest Problem Alert<strong></title>
			</head>
			<body>
			  <p>A guest problem alert has been raised for your property, please see details below:</p>
			  <table>
				<tr>
				  <th>Item</th><th>Score</th>
				</tr>
				<tr>
				  <td>Date & Time</td><td>'.clean_string($time_GMT11).'</td>
				</tr>
				<tr>
				  <td>Staff Name</td><td>'.clean_string($staff_name).'</td>
				</tr>
				<tr>
				  <td>Staff Department</td><td>'.clean_string($staff_department).'</td>
				</tr>
				<tr>
				  <td>Problem</td><td>'.clean_string($problem).'</td>
				</tr>
				<tr>
				  <td>Priority</td><td>'.clean_string($priority).'</td>
				</tr>
				<tr>
				  <td>Guest Name</td><td>'.clean_string($gst_name).'</td>
				</tr>
				<tr>
				  <td>Incident</td><td>'.clean_string($incident).'</td>
				</tr>
				<tr>
				  <td>Action</td><td>'.clean_string($action).'</td>
				</tr>
				<tr>
				  <td>Follow up</td><td>'.clean_string($followup).'</td>
				</tr>
				<tr>
				  <td>Guest Manner</td><td>'.clean_string($gst_temp).'</td>
				</tr>
				<tr>
				  <td>Case status</td><td>'.clean_string($case_status).'</td>
				</tr>
				<tr>
				  <td>Guest history update</td><td>'.clean_string($history).'</td>
				</tr>
			  </table>
			  <p> All OPEN CASES must be follow up within 1 hour from now. Please consult with the Resort Assistant Manager on-duty for more details.<br /><br />Kind regards,<br />Patrick Maurer<br />Director of QCI<br />
			</body>
			</html>';
		
		$mail->Host = "mail.server.com";
		$mail->Port = 25;
		$mail->SMTPAuth = true;
		$mail->Username = "[email protected]";
		$mail->Password = "myPW";
		
		$mail->setFrom('[email protected]', 'My Name');					// Sent FROM
		//$mail->addReplyTo('[email protected]', 'Not Needed');		// Add REPLY TO ADDRESS
		$mail->addAddress('[email protected]', 'My Recipient');		// Send TO

		$mail->Subject = 'DADHA Guest Problem Alert';				// Subject Line
		$mail->msgHTML(file_get_contents($email_message), dirname(__FILE__));		// Message body
		//$mail->addAttachment('./images/iclogo_small.jpg');		// Attachment if necessary

		
		//send the message, check for errors
		if (!$mail->send()) {
			echo "Mailer Error: " . $mail->ErrorInfo;
		} else {
			echo "Message sent!";
		}
		
	}
?>

<html>
	<head>
		<title>DADHA - Guest Problem Alert</title>
		<link rel="shortcut icon" href="./images/favicon.ico">			
	</head>

	<body>
		<table width="300px" bgcolor="#DDD9C4">
		<font size="1" face="verdana">
			<tr>
				<td><img src="./images/iclogo_small.jpg" style="width: 300px"/><br />
				<b><center>DADHA Guest Problem Alert Form</center></b></td>
			</tr>
			<tr>
				<td>
				<form name="contactform" action="" method="post">
					<div>
						<label for="name">Staff name (*):</label><br />
						<input type="text" name="staff_name" />
					</div>
					<div>
						<label for="department">Department (*):</label><br />
							<select name="staff_department" style="width: 300px">
								<option value="blank"> </option>
								<option value="FO">Front Office</option>
								<option value="F&B">Food & Beverage</option>
								<option value="HSKP">Housekeeping</option>
								<option value="S&M">Sales & Marketing</option>
								<option value="HR">Human Resources</option>
							</select
					</div>
					<div>
						<label for="problem">Problem (*):</label><br />
							<select name="problem" style="width: 300px">
								<option value="blank"> </option>
								<option value="Staff Service - Service Quality">Staff Service - Service Quality</option>
							</select
					</div>
					<div>
						<label for="priority">Priority:</label><br />
							<select name="priority" style="width: 300px">
								<option value="blank"> </option>
								<option value="Low">Low</option>
								<option value="Medium">Medium</option>
								<option value="High">High</option>
							</select>
					</div>
				
					<br />

<br />

					<div>
						<label for="gst_name">Guest Name:</label><br />
						<input type="text" name="gst_name" />
					</div>
					<div>
						<label for="gst_room">Guest Room (*):</label><br />
						<input type="text" name="gst_room" />
					</div>
					<div>
						<label for="incident">Incident (*):</label><br />
						<textarea style="width: 300px; height: 150px;" name="incident"></textarea>
					</div>
					<div>
						<label for="action">Action taken (*):</label><br />
						<textarea style="width: 300px; height: 150px;" name="action"></textarea>
					</div>
					<div>
						<label for="action">Follow up action (by whom)?:</label><br />
						<textarea style="width: 300px; height: 75px;" name="followup"></textarea>
					</div>
					<div>
						<label for="gst_temp">Guest manner?</label><br />
							<select name="gst_temp" style="width: 300px">
								<option value="blank"> </option>
								<option value="Very Satisfied">Very satisfied</option>
								<option value="Satisfied">Satisfied</option>
								<option value="So-so">So-so</option>
								<option value="Unhappy">Unhappy</option>
								<option value="Upset">Upset</option>
							</select>
					</div>
					<div>
						<label for="gst_temp">Case resolved? (*)</label><br />
							<select name="case_status" style="width: 300px">
								<option value="blank"> </option>
								<option value="YES">YES</option>
								<option value="NO">NO</option>
							</select>
					</div>
					<div>
						<label for="gst_history">Guest history updated by:</label><br />
						<input type="text" name="gst_history" />
					</div>
					<div align="center">
						<input type="submit" value="Submit" /> <input type="reset" value="Reset" />
					</div>
				</form>

				</td>
			</tr>
		</font>
		</table>
		
	</body>
	
</html>

Basically, what I am trying to do is to capture the information from the form and send them out via email.

Would appreciate some feedback.

Thanks,
A2k

I think this was a good choice for you, it’ll be much more maintainable.
Your current issue I believe lies in that you are trying to get the contents of a file that doesn’t actually exist cause you never made it in the script. Granted I don’t know why you would want to. This line is looking for a file to get the $email_message.
[php]$mail->msgHTML(file_get_contents($email_message), dirname(FILE));[/php]
But all you need is this
[php]$mail->msgHTML($email_message);[/php]
Now I use phpmailer in my cms but I don’t use the msgHTML method, not sure if it’s correct or not, I use simply $mail->Body. But by using that you must use this line beforehand $mail->isHTML(TRUE);. So maybe yours will work the same and easier.

thanks for the reply.

I did try the direct $mail->msgHTML($email_message); approach before but without success, so I thought that maybe the GET() function was required by PHPmailer or something like that.

Anyway, after making the adjustments, I am still facing the same problem. The script does not “submit” the data and doesnt start the email sending procedure. I suspected some issues with my inclusion of the mail() attributes into the POST section, but even after putting the POST initiator on top of the script, I still dont get any results.

EDIT: Just now separated the HTML form and the PHP code, letting the form submit to the parser, which is where it hangs. Submission process seems ok, but once it reaches the php file, it stops. I turned the E_ALL() function on but still dont see any errors.

Any idea what I am doing wrong?

Thanks,
A2k

OK, after hours of playing around, reproducing, testing other scripts and adjusting my script, I finally made it work…

<html>
	<head>
		<title>DADHA - Guest Problem Alert</title>
		<link rel="shortcut icon" href="./images/favicon.ico">	
		<meta name="viewport" content="width=device-width, user-scalable=no">
	</head>

	<body>
	
<?php
require("../PHPMailer-master/class.phpmailer.php");			// load the PHPmailer script
date_default_timezone_set('Etc/UTC');						// set timezone in PHP.ini,required by SMTP
ini_set('display_errors', 'On');							// show all errors
   error_reporting(E_ALL);
  
// VARIABLES HANDED OVER FROM GPA FORM  
	$staff_name = $_POST['staff_name']; 					// required
	$staff_department = $_POST['staff_department']; 		// required
	$problem = $_POST['problem']; 							// required
	$priority = $_POST['priority']; 						// not required
	$gst_name = $_POST['gst_name']; 						// not required
	$gst_room = $_POST['gst_room']; 						// not required
	$incident = $_POST['incident']; 						// required
	$action = $_POST['action']; 							// required
	$followup = $_POST['followup']; 						// required
	$gst_temp = $_POST['gst_temp']; 						// required
	$case_status = $_POST['case_status']; 					// required
	$gst_history = $_POST['gst_history']; 					// required
	$time = date("Y-m-d H:i:s", strtotime('+7 hours'));		// Add 7 hours to time
		
	$email_message = "<img src='cid:ic_logo' width='400' height='66' alt='IC Logo' /><br/>
	<p>A guest problem alert has been raised for your property, please see details below:</p>
	  <table>
		<tr><th>Item</th>
		<th>Details</th></tr>
		
		<tr><td>Date & Time</td><td>$time</td></tr>
		<tr><td>Staff Name</td><td>$staff_name</td></tr>
		<tr><td>Staff Department</td><td>$staff_department</td></tr>
		<tr><td>Problem</td><td>$problem</td></tr>
		<tr><td>Priority</td><td>$priority</td></tr>
		<tr><td>Guest Name</td><td>$gst_name</td></tr>
		<tr><td>Incident</td><td>$incident</td></tr>
		<tr><td>Action</td><td>$action</td></tr>
		<tr><td>Follow up</td><td>$followup</td></tr>
		<tr><td>Guest Manner</td><td>$gst_temp</td></tr>
		<tr><td>Case closed?</td><td><b>$case_status</b></td></tr>
		<tr><td>Guest history update by:</td><td>$gst_history</td></tr>
	  </table>
	  <p><b>All OPEN CASES must be follow up within 1 hour from now</b>. Please consult with the Resort Assistant Manager on-duty for more details.";

	
	if ($staff_name == "" OR $problem == "" OR $incident == "") 
		{
		echo "<img src=\"../../images/iclogo_small.jpg\" style=\"width: 300px\"><br/><font size=\"3\"><b><u>ERROR</u></b><br/></font>
		You did not enter a valid Staff Name, Guest Problem or Incident Message.<br/><br/>Please <a href=\"javascript:history.go(-1)\">[Go Back]</a> and fix this.<br/><br />
		<input action=\"action\" type=\"button\" value=\"Back\" onclick=\"history.go(-1);\" />";
		exit;
		}

		
// PHPMAILER START
	$mail = new PHPMailer();
	$mail->isSMTP();												// Set connection type to SMTP
	$mail->SMTPDebug = 2;											// Enable SMTP debugging, 0 = off (for production use), 1 = client messages, 2 = client and server messages
	$mail->Debugoutput = 'html';									// Set the output format for SMTP Debug
		
	$mail->Host = "mail.server.com";								// Mail Host Server
	$mail->Port = 25;												// SMTP Port (usually 25)
	$mail->SMTPAuth = true;											// Authentication needed? true/false
	$mail->Username = "user";							// Login Username
	$mail->Password = "password";									// Login Password
	
	$mail->setFrom('[email protected]', 'My Name');			// Sent FROM
	//$mail->addReplyTo('[email protected]', 'Not Needed');			// Add REPLY-TO address
	$mail->addAddress('[email protected]', 'Send To');	// Send TO

	$mail->Subject = 'My Subject Line';
	$mail->isHTML(TRUE);											// HTML body? true/false
	$mail->Body = $email_message;									// Message body - regular
	//$mail->AltBody = $email_message;								// Message body - plain text
	//$mail->addAttachment('../../images/iclogo_small.jpg');		// Attachment if necessary
	$mail->AddEmbeddedImage('../../images/iclogo_small.jpg', 'ic_logo');
	

	
	//send the message, check for errors
	if (!$mail->send()) 
	{
		echo "Mailer Error: " . $mail->ErrorInfo;
	} 
	else	{
			echo "Message sent!";
			}

?>

Several problems I found with PHPmailer

  1. PHPmailer is picky when it comes to the parser syntax, respectively the order in which I define my variables. Previously I defined my POST_GET items before I loaded the PHPmailer.class.php file. Turns out phpmailer doesnt like that, also there is a difference between the class.phpmailer.php file and the class.autoloader.php file, since the later managed to get the script working.

  2. When planning to have a HTML body, place the HTML/HEAD/BODY tags into the parser before starting with the <?php tag. When starting off with HTML tags in the $mail->body tag, the script seems to break (unless you escape \n\n\ everything first)

  3. HTML messages should be written into the $mail->body tag, not the $mail->MsgHTML($yourbody); tag. (So what the hell is this function good for then? -.- )

Well, at last all the issues with the script resulted in me cleaning up the script and doing some more “clean coding”.

hope this helps someone. Last word of advice, forget PHP + PEAR for sending emails, just go directly for PHPmailer, saves you lots and lots of troubles…

Thanks for all the help guys,
A2k

Sponsor our Newsletter | Privacy Policy | Terms of Service