Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

email form issue 1

Status
Not open for further replies.

VAMick

Programmer
Nov 18, 2005
64
US
Ok,I've got a dynamically created form that gets emailed to clients as HTML. PHP/Mysql creates it. Worked fine for everyone until we changed hosts and went from PHP4 to PHP5. Now, it works fine on my Mac and one of my clients PC's, but the rest of his staff receive it as raw html code instead of a formatted page in their email. Client and staff have same email settings. Any idea what might be causing this and how to resolve? Thanks.

 
make sure that you are setting the content-type headers correctly. some clients may be inferring them rather than have them expressed.

i have posted some code for sending multipart email in this forum.

 
Looks like I was def leaving some code out of the headers. Took me a while to find that posting, but was well worth it. Thanks.
 
jpadie, I have worked for hours on the code using your post as a basis and it only got worse. Now it doesn't send the message at all, just a blank email, with the correct To: address, but it sends it 4 times.

This is my old code, that works on my Mac email, but not the other clients. It also sends the email twice for some reason. could you look at my code and tell me WTF I'm doing wrong?!?!

Code:
<?PHP

if(@$_REQUEST['form'] == "" || @$_REQUEST['registrarid'] == ""){
	echo "<p>This page cannot be called directly.</p>";
	exit();
}
if(@$_REQUEST['step'] != "two"){
	ob_start();
	if($_REQUEST['form'] == "agreement"){
		include("trainingagreement.php");
		$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
                 }elseif($_REQUEST['form'] == "pdfagreement"){
		include("trainingagreement.php");
		$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
	}elseif($_REQUEST['form'] == "confirmation"){
		include("trainingconfirm.php");
		$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
	}elseif($_REQUEST['form'] == "pdfconfirmation"){
		include("trainingconfirm.php");
		$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
	}elseif($_REQUEST['form'] == "rescheduledconfirmation"){
		include("rescheduledconfirm.php");
		$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
	}elseif($_REQUEST['form'] == "pdfrescheduledconfirmation"){
		include("rescheduledconfirm.php");
		$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
	}elseif($_REQUEST['form'] == "reminderconfirmation"){
		include("reminderconfirm.php");
		$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
	}elseif($_REQUEST['form'] == "pdfreminderconfirmation"){
		include("reminderconfirm.php");
		$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];	
	}elseif($_REQUEST['form'] == "confirmation2"){
		include("trainingconfirm2.php");
		$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
	}elseif($_REQUEST['form'] == "po"){
		include("purchaseorder.php");
		$to = $vendor['vendoremail'];
		}elseif($_REQUEST['form'] == "survey"){
		include("studentsurvey.php");
		$to = $student_contact['email'];
	}elseif($_REQUEST['form'] == "invoice"){
		include("invoice.php");
		$to = $row_Recordset1['billemail']; //change this
	}
	ob_end_clean();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	   "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]

<html>
<head>
	<title>Send <?PHP echo $_REQUEST['form']; ?></title>
</head>

<body>

<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="registrarid" value="<?PHP echo $_REQUEST['registrarid']; ?>">
<input type="hidden" name="form" value="<?PHP echo $_REQUEST['form']; ?>">
<input type="hidden" name="step" value="two">
<p>
	Send <?PHP echo $_REQUEST['form']; ?> to: <input type="text" name="send_to" value="<?PHP echo $to; ?>" size="50"> (Example: email@yourdomain.com, email2@yourdomain.com)<br>
	<input type="submit" name="submit_button" value="Send <?PHP echo $_REQUEST['form']; ?>">
</p>
</form>

</body>
</html>

<?PHP
	exit();
}elseif(@$_REQUEST['step'] == "two"){
	ob_start();
	
	if($_REQUEST['form'] == "agreement"){
		include("trainingagreement.php");
    }elseif($_REQUEST['form'] == "pdfagreement"){
		include("trainingagreement.php");
	}elseif($_REQUEST['form'] == "confirmation"){
		include("trainingconfirm.php");
	}elseif($_REQUEST['form'] == "pdfconfirmation"){
		include("trainingconfirm.php");
	}elseif($_REQUEST['form'] == "rescheduledconfirmation"){
		include("rescheduledconfirm.php");
	}elseif($_REQUEST['form'] == "pdfrescheduledconfirmation"){
		include("rescheduledconfirm.php");
	}elseif($_REQUEST['form'] == "reminderconfirmation"){
		include("reminderconfirm.php");
	}elseif($_REQUEST['form'] == "pdfreminderconfirmation"){
		include("reminderconfirm.php");
	}elseif($_REQUEST['form'] == "confirmation2"){
		include("trainingconfirm2.php");	
	}elseif($_REQUEST['form'] == "po"){
		include("purchaseorder.php");
	}elseif($_REQUEST['form'] == "survey"){
		include("studentsurvey.php");	
	}elseif($_REQUEST['form'] == "invoice"){
		include("invoice.php");
	}
	
	$message=ob_get_contents();
	ob_end_clean();
	$success = "Email sent successfully!";
	$failure = "Failed to send email!!!";
	if($_REQUEST['form'] == "agreement"){
		$success = "Agreement sent successfully.";
		$failure = "Failed to send agreement email!!";
		$from = "registrar@domain.com";
		$subject = "Training Agreement - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
	}elseif($_REQUEST['form'] == "confirmation"){
		$success = "Confirmation sent successfully.";
		$failure = "Failed to send confirmation email!!";
		$from = "registrar@domain.com";
		$subject = "Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
	}elseif($_REQUEST['form'] == "rescheduledconfirmation"){
		$success = "Rescheduled Confirmation sent successfully.";
		$failure = "Failed to send rescheduled confirmation email!!";
		$from = "registrar@domain.com";
		$subject = "Rescheduled Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
	}elseif($_REQUEST['form'] == "reminderconfirmation"){
		$success = "Reminder Confirmation sent successfully.";
		$failure = "Failed to send reminder confirmation email!!";
		$from = "registrar@domain.com";
		$subject = "Reminder Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
	}elseif($_REQUEST['form'] == "confirmation2"){
		$success = "Confirmation sent successfully.";
		$failure = "Failed to send confirmation email!!";
		$from = "registrar@domain.com";
		$subject = "Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";	
	}elseif($_REQUEST['form'] == "po"){
		$success = "Purchase order sent successfully.";
		$failure = "Failed to send purchase order email!!";
		$from = $stbrep['stb_email'];
		$subject = "Purchase Order - " . $student_contact['lastname'] . " - " . $vendor['Company_Name'] . "";
	}elseif($_REQUEST['form'] == "survey"){
		$success = "Survey sent successfully.";
		$failure = "Failed to send survey email!!";
		$from = $stbrep['stb_email'];
		$subject = "Survey - " . $student_contact['lastname'] . " - " . $vendor['Company_Name'] . "";
	}elseif($_REQUEST['form'] == "invoice"){
		$success = "Invoice sent successfully.";
		$failure = "Failed to send invoice email!!";
		$from = $row_Recordset1['stb_email'];
		$subject = "Invoice - " . $row_Recordset1['invoicenumber'] . " - " . $row_Recordset1['studentlast'] . " - " . $row_Recordset1['coursecode'] . " - " . $row_Recordset1['coursename'] . "";
	}

	if ($_REQUEST['form'] == 'pdfconfirmation') {
		$from = "registrar@domain.com";
		$subject = "Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
		$message = 'Attached you will find your confirmation and other pertinent details to your upcoming training event.  If you have any questions regarding your enrollment, please call us toll free at 1.888.555.2121.<br /><br />
 		Thank you,<br /><br />
		Education Services<br />
		Company Name';
		$success = "PDF confirmation sent successfully.";
		
		include 'createpdf.php';
		
		$results = true;
	}
	elseif ($_REQUEST['form'] == 'pdfrescheduledconfirmation') {
		$from = "registrar@domain.com";
		$subject = "Reschedule Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
		$message = 'Attached you will find your reschedule confirmation and other pertinent details to your upcoming training event.  If you have any questions regarding your enrollment, please call us toll free at 1.888.555.2121.<br /><br />
 		Thank you,<br /><br />
		Education Services<br />
		Company Name';
		$success = "PDF Reschedule confirmation sent successfully.";
		
		include 'createpdfreschedule.php';
		
		$results = true;
	}
elseif($_REQUEST['form'] == "pdfagreement"){
                                $success = "PDF Agreement sent successfully.";
		$failure = "Failed to send agreement email!!";
		$from = "registrar@domain.com";
		$subject = "Training Agreement - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
                                $message = 'Attached you will find your agreement and other pertinent details to your upcoming training event.  If you have any questions regarding your enrollment, please call us toll free at 1.888.555.2121.<br /><br />
 		Thank you,<br /><br />
		Education Services<br />
		Company Name';
		include 'createpdfagreement.php';
		
		$results = true;
	}
	elseif ($_REQUEST['form'] == 'pdfreminderconfirmation') {
		$from = "registrar@domain.com";
		$subject = "Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
		$message = 'Attached you will find your reminder confirmation and other pertinent details to your upcoming training event.  If you have any questions regarding your enrollment, please call us toll free at 1.888.555.2121.<br /><br />
 		Thank you,<br /><br />
		Education Services<br />
		Company Name';
		$success = "PDF Reminder confirmation sent successfully.";
		
		include 'createpdfreminder.php';
		
		$results = true;
	}
	else {
		
		$send_to = $_POST['send_to'];
		$message = str_replace("../invoice.css", "[URL unfurl="true"]http://www.domain.com/invoice.css",[/URL] $message);
		$message = preg_replace("/\.?\.?\/images\/logo_hi-res_sm.gif/", "[URL unfurl="true"]http://www.domain.com/images/logo_hi-res_sm.gif",[/URL] $message);
		//$message = str_replace("/images/logo_hi-res_sm.gif", "[URL unfurl="true"]http://www.domain.com/images/logo_hi-res_sm.gif",[/URL] $message);
		$message = str_replace("/images/gray_dot.gif", "[URL unfurl="true"]http://www.domain.com/images/gray_dot.gif",[/URL] $message);
		$message = str_replace("/images/shared_dot_clear.gif", "[URL unfurl="true"]http://www.domain.com/images/shared_dot_clear.gif",[/URL] $message);
		
		// To send HTML mail, the Content-type header must be set
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$headers .= 'To: '.$send_to;
		// Additional headers
		$headers .= "\r\n";
		$headers .= 'From: ' .$from. "\r\n";
		$results = @mail($send_to, $subject, $message, $headers);
	}
	if($results){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	   "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]

<html>
<head>
	<title>Message Sent</title>
</head>

<body>

<p><a href="dashboard-new.php">Dashboard</a> | <a href="registrar_new.php?registrarid=<?PHP echo $_REQUEST['registrarid']; ?>">Registrar</a></p>
<p><?PHP echo $success; ?></p>
<p>Message sent to: <?PHP echo $send_to; ?></p>
<p>The following message was sent:</p>
<hr>
<?PHP echo $message; ?>

</body>
</html>

<?PHP
	}else{
		echo $failure;
	}
	exit();
}
?>
<p>This page cannot be called directly.</p>
 
tracking your code through (albeit in brief), it seems that you only send a mail in the event that all the various conditions in your if ... elseif... clause fail.

and in this segment you are using the $send_to variable without any cleansing (which leaves you open to be an email remailer) and you are not specifying the $from variable. Without this it is likely not to work.

I'm not sure why my code would cause 4 emails to be sent (unless it was the variant for mass-mailing that I published). if you provide the thread reference or the code, i can take a look.
 
looks good (!) this code does not send four emails. just one (unless you deliberately send 4)!
 
I dropped my headers and message code and used yours. First time it didn't send anything but a blank email.

then i used part of my code, part of yours. 4 emails.

At this point, i'm getting frustrated and have no clue how to get this to work right. What's crazy is it worked just fine on the old hosting box, running php4, but now on the new box, running php5...it sends raw html to most people, but appears fine in my email, running on a mac. go figure.

so, any advice or perhaps show me what's wrong in my code?

thanks for the help.
 
as i said, it is the content headers and the fact that you are not providing a non-html alternative. but your code is either not complete or i'm very surprised that it works the way you intended.

i'm assuming that the mail transfer agent has stayed the same between the transition from php4 to 5?

change this
Code:
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: '.$send_to;
// Additional headers
$headers .= "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$results = @mail($send_to, $subject, $message, $headers);
to
Code:
$sep = "\n";
$boundary = md5(uniqid("",true));
$b1 = "b1---$boundary";
$headers  = "From: $from$sep";
$headers .= "To: $send_to$sep";
$headers .= "Return-Path: $from$sep";
$headers .= "MIME-Version: 1.0$sep";
$headers .= "Content-Type: multipart/alternative; boundary=\"$b1\"$sep";
$headers .= "$sep";  
 
//now set up the message
$messageHeader = "--$b1$sep";
//default message
$defaultmessage = "Go get yourself an html mail client";
$messageHeader .= $defaultmessage.$sep;
$messageHeader .= "$sep";  
//html message
$messageHeader .= "--$b1$sep";
$messageHeader .= 'Content-Type: text/html; charset="iso-8859-1"'.$sep;
$messageHeader .= "Content-Transfer-Encoding: 8bit$sep";
$messageHeader .= "$sep";
$messageHeader .= $message;
$messageHeader .= "$sep";
$messageHeader .= "--$b1--";
$messageHeader .= "$sep";

$results = @mail($send_to, $subject, $messageHeader, $headers);
 
That worked great, except it still sends two emails. Thoughts on that?

Btw, was it the fact that my code didn't specify $messsageHeader that it was failing?

And thank you!!
 
on the second question, no. it was that some of your email clients may have needed more gentle handling. this is provided by the encoding and the multipart nature (providing a default).

on the first point, i suspect that this can only be caused by your $send_to variable being malformed for the email code. It should be in the ordinary form of firstname.lastname@example.com. can you echo it out before the mail call?

if it is still happening, add a timestamp or unique id to the message itself so you can determine if the MTA is sending two emails for some crazy reason.

and if it is still still happening, echo out the whole mail thing before sending

echo "<pre>" . $headers . $messageHeader . "</pre>";

 
This has been great info and a big help to me. I thank you for taking the time to explain it and rewrite my code.

I'll give those last suggestions a try to see if they remedy the double emailing issue. At least I'm sending out two quality emails now! haha.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top