0

Hi, I've got this nice little sales tool/form written where I can drop email addresses into my webform and automatically send an HTML email, but I want to have the option to send a different html body using a drop-down option. For example, if I want to send a "general introduction" email, I can choose "general" from my drop-down and it will send that email, or if I want to send a "follow-up," it will send that email, etc. I am having trouble with the PHP call-to-action of which email to send. I already have it worked out so it will send only one type of email and that's by wrapping this php around the one html message. I think I am missing an ifelse command, and I also need to know call the different html emails (do I use "require_once"?). This is what I have so far.

Before anyone says it, I know there are already programs written like this that I can buy or services to which I can subscribe, but I want to write my own (with a little help, of course).

<?php
$from = $_POST['from'];
$to = trim($_POST['contacts']);
$to = $_POST['contacts'];
$subject = $_POST['subject'];
$random_hash = md5(date('r', time()));
$headers = "From: $from\r\nReply-To: $from";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
ob_start(); 
?>

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

//This is where I would need the body options

<?php 
$message = $body;
$body = $_POST['email_body'];
$email_body = $awards = ('/awards.html'); //this is where I lose it
$general = ('/general.html');
$digital = ('/digital.html');
$retail = ('/retail.html');
$followup = ('/followup.html');
?>

--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

Thanks for any help in advance!

GJ

2
Contributors
10
Replies
15
Views
6 Years
Discussion Span
Last Post by GreaseJunkie
0

You can use a switch statement to get the body of the text. If the body will be static, you can use file_get_contents to read in a text file into a string variable like this:

<?php
...
$type = $_POST['type'];
switch($type) {
	case 'intro':
			$body = file_get_contents('email/intro.txt', true);
		break;
	
	case 'followup':
			$body = file_get_contents('email/followup.txt');
		break;
	default:
		die('no valid message type selected.');		
}
...
?>

This approach is flexible since you can also create a subject line based on the type selected (or alter a user supplied subject line) etc. I don't use the post variable directly to fetch the file to avoid security issues with a doctored version of the form.

Wrap $body in the hash boundaries and you are set.

0

I'm getting a "failed to open stream" "no such file" error, but I'll figure it out. You are a genius! Thanks MadCoder!

0

This is what I've tried and it's not giving me the error codes anymore, but I am getting blank test emails, which means it's not fetching the files, right?

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<?php 
$message = $_POST['email_body'];
switch($message) {
case 'awards':
$message = file_get_contents('emails/awards.html', true);

break;

 
case 'digital':
$message = file_get_contents('emails/digital.html', true);

break;

default:
die('no valid message type selected.');
}

?>

--PHP-alt-<?php echo $random_hash; ?>--

Edited by GreaseJunkie: saw a possible error

0

Here's the full code, with descriptions of each action as I understand them.

<?php
//define the sender of the email
$from = $_POST['from'];
//define the receiver of the email
//cuts out all the non essential characters
$to = trim($_POST['contacts']);
//sends to trimmed receivers
$to = $_POST['contacts'];
//define the subject of the email from form
$subject = $_POST['subject'];
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $from\r\nReply-To: $from";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>

--PHP-alt-<?php echo $random_hash; ?> 
//defines the MIME type (this case html)
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<?php 
$message = $_POST['email_body'];
switch($message) {
case 'awards':
$message = file_get_contents('http://www...emails/awards.html', true);

break;

 
case 'digital':
$message = file_get_contents('http://www...emails/digital.html', true);

break;

default:
die('no valid message type selected.');
}

?>

--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
0

You are overwriting $message in line 50 with output buffered stuff. Lose the ob and try this.

After the switch you can prepent the hash stuff...

...
$message = '--PHP-alt-'.$random_hash.'--
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
'.$message.'
--PHP-alt-'.$random_hash.'--
';
// $message = ob_get_clean();  // DON'T NEED OB
$mail_sent = @mail( $to, $subject, $message, $headers );
...

Edited by madCoder: Clarified opening statement.

0

madCoder, I'm sure I am testing your nerves. But this is semi-working. I am receiving the emails, but in text format of my html code. Was the hash supposed to go before the switch or after. I didn't quite understand.

<?php
//define the receiver of the email
$from = $_POST['from'];
$to = trim($_POST['contacts']);
$to = $_POST['contacts'];
//define the subject of the email
$subject = $_POST['subject'];
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $from\r\nReply-To: $from";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.

$message = $_POST['email_body'];
switch($message) {
case 'awards':
$message = file_get_contents('http://www...com/emails/awards.html', true);

break;

 
case 'digital':
$message = file_get_contents('http://www...com/emails/digital.html', true);

break;

default:
die('no valid message type selected.');
}

$message = '--PHP-alt-'.$random_hash.'--
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
'.$message.'
--PHP-alt-'.$random_hash.'--
';

//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
0

I think there may be a missing carriage return or two. There needs to be a blank line between the header and content (two \r\n.)

Replace line 34-39 with this:

$message = "\r\n\r\n--PHP-alt-".$random_hash."--\r\nContent-Type: text/html; charset=\"iso-8859-1\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n".$message."\r\n\r\n--PHP-alt-".$random_hash."--\r\n\r\n";
0

Just to update you MC, I tried this and I put the hash code after the switch and it works to send different email bodies when selected, but I only get the full HTML including images on my Blackberry, no body on webmail at all and straight code output with my email client. I am trying a different encoding to see if that works. It's still taking a while to process test emails through the server. I have a feeling the 7bit encoding is the issue, so I am trying 64bit. I am going to mix the tests between this and utf-8 to see if I can come to a uniform output across all platforms. Thanks again for your help! Genius.

0

OK!! I got it figured out. The only thing I need to add is to BCC all the recipients and to add a function to INSERT INTO my localhost db. I've taken code from simple forms I've built and simply added the functions into the beginning of the php file, but it's not adding to the selected db table. Anyway, the hard part is done, thanks to you again, madCoder. Here's the final script (I got rid of the final random hash at the end of the file after the switch and it's still processing properly and not adding the random number to the actual body of the email and I added the extra content type into the $headers. See below.

<?php
$from = $_POST['from'];
$to = trim($_POST['contacts']);
$to = $_POST['contacts'];
$subject = $_POST['subject'];
$random_hash = md5(date('r', time()));
$headers = "From: $from\r\nReply-To: $from\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";

$message = $_POST['email_body'];
switch($message) {
case 'awards':
$message = file_get_contents('http://www.website.com/awards.html', true);

break;
case 'digital':
$message = file_get_contents('http://www.website.com/digital.html');

break;

default:
die('no valid message type selected.');
}

$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
0

Just a note to anyone that might want to do this type of thing: Your HTML cannot have DOCTYPE and CSS in <head> element doesn't process, the way I tried it. So just

<html>
<body>
<table><!--inline css works-->
</table>
</body>
</html>

The table is important for your email message, as it is uniform across 99% of all email clients and web-mail. Try to keep it to about 600px wide and the body left justified - Outlook doesn't do "center" so I hear (I'm on Mac). And one more thing: the html has to be error-free or the PHP will not process it and will not kick back an error so you will not know, of course. Oh, and <ul>, <li> didn't work for me either. Still trying to figure out what PHP likes and doesn't with regard to proper display or delivery. Maybe it's the character set - utf-8? 8859-1? If anyone has any suggestions, please! Otherwise, I've marked this as "solved", thanks to madCoder. Maybe I should start a new thread for "PHP and HTML."

L8tr.

GS

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.