Page 1 of 1

Sending HTML Mail Using Mail() Function and Mail Template

Posted: Sun Aug 17, 2003 10:36 pm
by hismightiness
**BEWARE - Beginner at work here! :D **

I am using the Mail() function in attempts to send mail via a form on a web page (same page as the form by using a custom function). The function worked just fine until I tried to add more information to it and attempted to create an HTML-formatted e-mail.

Now, the visitor comes to the form and fills in the required field with JavaScript validator to ensure that all required fields get filled in correctly.

Once the visitor gets all the required info done, they click submit. Now, I have an if statement checking for a querystring which will only be there if the form is posted and tell the page to process, displaying a confirmation message. Otherwise the form will simply be displayed again with no fields filled in.

The tricky thing here that I am trying to do is use a template, "email_temp.tpl", and fill in the places where information is entered to customize the e-mail.

The form submits as intended and I am getting the confirmation display, but something is going wrong before the Mail() function gets called, because I am not getting the e-mails. Here is the code:

Code: Select all

function send_mail(){
	$file_name="email_temp.tpl";
	$send_to="info@domainname.com";
	$headers_to_write=return_all_headers();
	$subject="General Inquiry Submission";
	$email_body=implode("",file($file_name));
	$email_body=str_replace("[>email_subject<]",trim($subject),$email_body);
	$email_body=str_replace("[>subject<]",trim($cmbInquiry),$email_body);
	$email_body=str_replace("[>header_info<]",$headers_to_write,$email_body);
	$email_body=str_replace("[>name<]",trim($txtName),$email_body);
	$email_body=str_replace("[>email<]",trim($txtEMail),$email_body);
	$email_body=str_replace("[>phone1<]",trim($txtDaytimePhone),$email_body);
	$email_body=str_replace("[>phone2<]",trim($txtEveningPhone),$email_body);
	$email_body=str_replace("[>address<]",trim($txtAddress),$email_body);
	$email_body=str_replace("[>address2<]",trim($txtAddress2),$email_body);
	$email_body=str_replace("[>city<]",trim($txtCity),$email_body);
	$email_body=str_replace("[>state<]",trim($cmbState),$email_body);
	$email_body=str_replace("[>zip<]",trim($txtZipCode),$email_body);
	$email_body=str_replace("[>country<]",trim($txtCountry),$email_body);
	$email_body=str_replace("[>how<]",trim($cmbReferredBy),$email_body);
	$email_body=str_replace("[>contact<]",trim($cmbPreferredContact),$email_body);
	$email_body=str_replace("[>comments<]",trim($txtComments),$email_body);
	$email_body=addslashes($email_body);
	$email_body=chunk_split(base64_encode($email_body))."\n \n";
	$email_headers ="MIME-Version: 1.0\r \n";
	$email_headers.="To: StrohlSiteDesign.com<$send_to>\r \n";
	$email_headers.="From: $txtName<$txtEMail>\r \n";
	$email_headers.="Reply-To: $txtName<$txtEMail>\r \n";
	$email_headers.="X-Priority: 1\r \n";
	$email_headers.="X-MSMail-Priority: High\r \n";
	$email_headers.="X-Mailer: PHP Mailer\r \n";
	$email_headers.="Content-Type: text/html; charset="iso-8859-1"\r \n";
	$email_headers.="Content-Transfer-Encoding: base64\r \n \n";
	if (mail($send_to,$subject,stripslashes($email_body),$email_headers)) {
		return 1;
	}else{
		return 0;
	}
}
function return_all_headers() {
   $headers = array();
  while (list($key, $value) = each ($_SERVER)) {
       if (strncmp($key, "HTTP_", 5) == 0) {
           $key = strtr(ucwords(strtolower(strtr(substr($key, 5), "_", " "))), " ", "-");
           $headers[$key] = $value;
       }
   }
   return $headers;
}
I should add that no errors are received when running this page.

Any help is greatly appreciated... It is still appreciated if you tell me that I need to start over with a new way of doing what I want to do. Hehehe...

Posted: Fri Aug 22, 2003 2:12 pm
by hismightiness
:idea: Well, I got everything working except getting the e-mail generated correctly. My e-mail server is recognizing something hitting it, because I am receiving my autoresponse as expected, but the e-mail isn't there when I check the account. Can anyone shed any light as to hwo this is happening. The code above is virtually the same.

Is the method I am using incorrect? What may be wrong with it? Should I just create a static e-mail and simply insert the values as one would a normal page? Can someone please help me? :wink:

Posted: Fri Aug 22, 2003 3:20 pm
by tsg
Could this line have something to do with it? Wondering because you have StrohlSiteDesign.com in there too ...

Code: Select all

email_headers.="To: StrohlSiteDesign.com<$send_to>\r \n";

Posted: Sun Aug 24, 2003 9:06 am
by AVATAr
why dont you echo the mail and see what are you generating

Posted: Mon Aug 25, 2003 9:21 pm
by hismightiness
I have been working on this off and on since my last posting. Free time has been nill.

However, thank you for the suggestions. I tried both. I changed the convention of the "To" Header and I attempted to echo the e-mail.

All attempts to echo the e-mail have proved useless. I cannot get it to show. However, I was finally able to get my e-mail to get caught by my "Spam Assassin". After making a couple adjustments to my template and script, I finally received the e-mail in my inbox, but the colors and HTML formatting is off. Is this due to the encoding?

Here is what I have ended up with:

Code: Select all

<?php
function return_all_headers() {
	$headers = array();
	while (list($key, $value) = each ($_SERVER)) {
		if (strncmp($key, "HTTP_", 5) == 0) {
			$key = strtr(ucwords(strtolower(strtr(substr($key, 5), "_", " "))), " ", "-");
			$headers[$key] = $value;
		}
	}
	return $headers;
}

function send_mail(){
	// Retrieve the form post values...
	global $HTTP_POST_VARS;
	// Assign all needed variable values...
	$file_name='contact/email_temp.tpl';
	$send_to="someone@domainname.com";
	$headers_to_write=return_all_headers();
	$subject="General Inquiry Submission";
	// Form values
	$cmbInquiry=$HTTP_POST_VARS["cmbInquiry"];
	$txtName=$HTTP_POST_VARS["txtName"];
	$txtEMail=$HTTP_POST_VARS["txtEMail"];
	$txtDaytimePhone=$HTTP_POST_VARS["txtDaytimePhone"];
	$txtEveningPhone=$HTTP_POST_VARS["txtEveningPhone"];
	$txtAddress=$HTTP_POST_VARS["txtAddress"];
	$txtAddress2=$HTTP_POST_VARS["txtAddress2"];
	$txtCity=$HTTP_POST_VARS["txtCity"];
	$cmbState=$HTTP_POST_VARS["cmbState"];
	$txtZipCode=$HTTP_POST_VARS["txtZipCode"];
	$txtCountry=$HTTP_POST_VARS["txtCountry"];
	$cmbReferredBy=$HTTP_POST_VARS["cmbReferredBy"];
	$cmbPreferredContact=$HTTP_POST_VARS["cmbPreferredContact"];
	$txtComments=$HTTP_POST_VARS["txtComments"];
	// Parse the values and create workable data...
	// Create the e-mail body
	$email_body=implode('',file($file_name,1));
	$email_body=str_replace("[>email_subject<]",trim($subject),$email_body);
	$email_body=str_replace("[>subject<]",trim($cmbInquiry),$email_body);
	$email_body=str_replace("[>header_info<]",$headers_to_write,$email_body);
	$email_body=str_replace("[>name<]",trim($txtName),$email_body);
	$email_body=str_replace("[>email<]",trim($txtEMail),$email_body);
	$email_body=str_replace("[>phone1<]",trim($txtDaytimePhone),$email_body);
	$email_body=str_replace("[>phone2<]",trim($txtEveningPhone),$email_body);
	$email_body=str_replace("[>address<]",trim($txtAddress),$email_body);
	$email_body=str_replace("[>address2<]",trim($txtAddress2),$email_body);
	$email_body=str_replace("[>city<]",trim($txtCity),$email_body);
	$email_body=str_replace("[>state<]",trim($cmbState),$email_body);
	$email_body=str_replace("[>zip<]",trim($txtZipCode),$email_body);
	$email_body=str_replace("[>country<]",trim($txtCountry),$email_body);
	$email_body=str_replace("[>how<]",trim($cmbReferredBy),$email_body);
	$email_body=str_replace("[>contact<]",trim($cmbPreferredContact),$email_body);
	$email_body=str_replace("[>comments<]",trim($txtComments),$email_body);
	// Finish Formatting the e-mail body
	$email_body=addslashes($email_body);
	$email_body=chunk_split(base64_encode($email_body))."\n \n";
	// Create the e-mail headers
	$email_headers ="MIME-Version: 1.0\r \n";
	$email_headers.="To: Something Here <$send_to>\r \n";
	$email_headers.="From: $txtName <$txtEMail>\r \n";
	$email_headers.="Reply-To: $txtName <$txtEMail>\r \n";
	$email_headers.="X-Priority: 1\r \n";
	$email_headers.="X-Mailer: PHP Mailer\r \n";
	$email_headers.="Content-Type: text/html; charset="iso-8859-1"\r \n";
	$email_headers.="Content-Transfer-Encoding: base64\r \n \n";
	if (mail($send_to,$subject,stripslashes($email_body),$email_headers)) {
		return 1; // The mail function worked
	}else{
		return 0; // The mail function failed
	}
}?>
Is there anyway to return an actual HTML formatted e-mail? Have I chosen the wrong encoding option(s)? Like I said the formatting was off. The colors were not anywhere near the right ones (almost like looking at the negative instead of the picture), and the table formatting wasn't right (I saw borders where there shouldn't have been any).