Jump to content

if statements in html email


davidcriniti

Recommended Posts

Hi,

 

I've got a breakfast order form on a page on my site. On the confirmation page it lists only the items that have been ordered, and their prices.

 

However, the same page sends a confirmation email and I'm unable to use the same if statements in the body of the email. Currently, I have to list all items.

 

How can I incorporate if / else statements in the body of the email?

 

 




$to = "$emailaddress\n";
$subject = "C2K Breakfast Order";
$headers = "From: $emailaddress\n";

$message = 

"<html>
<body>Thanks for placing your order on the C2K breakfast page $firstname.\n
<h1>Your breakfast id is: $id</h1>

<strong>French toast:</strong> $french_toast<br/>
<strong>Bacon, eggs and tomato on toast:</strong> $bacon_eggs_tom_toast<br/>
<strong>Sausages, eggs and tomato on toast:</strong> $sausage_eggs_tom_toast<br/>
<strong>Bacon and egg roll:</strong> $bacon_egg_roll<br/>
<strong>Big breakfast:</strong> $big_breakfast<br/>
<strong>Pancakes, ice cream and maple sauce:</strong> $pancakes<br/>
<strong>Raisin toast:</strong> $raisin_toast<br/>
<strong>Cinnamon toast:</strong> $cinnamon_toast<br/>
<strong>Cereal and fruit:</strong> $cereal<br/>
<strong>Total:</strong> $$total<br/>
<strong>Your comments:</strong> $comments<br/>




</body>
</html>
";


// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

if (preg_match(' /[\r\n,;\'"]/ ', $_POST['emailaddress'])) {
  exit('Invalid Email Address');
}
else {
mail($to,$subject,$message,$headers);
}


 

I basically want to say something along the lines of

 


if (
$french_toast=="Yes" )
echo "<strong>French toast:</strong> $french_toast<br/>";


 

 

Any advice?

 

Link to comment
https://forums.phpfreaks.com/topic/249414-if-statements-in-html-email/
Share on other sites

The simplest solution would be

$message = "

Thanks for placing your order on the C2K breakfast page $firstname.\n
Your breakfast id is: $id

";

if ($french_toast == "Yes") $message .= "French toast
";
// etc.

$message .= "
Total: \$$total

Your comments: $comments




";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.