• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 686
  • Last Modified:

PHP If statement within HTML Email

How do I add PHP code into an HTML email that is sent out via PHP?

The following does not work:

			$to = 'ME@ME.com';
			
			$subject = 'MY SUBJECT';
			
			$headers = "From: ME\r\n";
			$headers .= "Reply-To: help@ME.com\r\n";
			$headers .= "MIME-Version: 1.0\r\n";
			$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
			$message =	'<html><body>';
			$message .= '<h1>Thank you for your purchase</h1>
							<table>
								<tr>
									<td>TD Image One ONE</td>
									<td>TD Image TWO </td>
								</tr>
								
								<tr>
									<td><img src="MY-IMAGE/GC-25.jpg"/></td>
									<td>';
									if ($item-name=="CI50") {
										echo '<img src="MY-IMAGEGC-50.jpg"/><br />'.$gon;
										} else {
											echo 'No Image Available';
										}
										echo '</td>
								</tr>
							</table>';
			$message .= '</html></body>';
				
				mail($to, $subject, $message, $headers);		
		}

Open in new window

0
rgranlund
Asked:
rgranlund
1 Solution
 
Frosty555Commented:
You need to understand what the code is doing. In the example above, you are appending strings to the variable $message, which contains the body of the email that you are going to send. Each line of code is appending more data onto the end of the string.

$somevar = "asdf";     // Sets the value of the variable to "asdf"
$somevar .= "this";     // The value is now "asdfthis"
$somevar .= "is";     // The value is now "asdfthisis"
$somevar .= "a";     // The value is now "asdfthisisa"
$somevar .= "test";     // The value is now "asdfthisisatest"

The IF statement you put into your code is just "echoing" data out to standard output and will have no effect on the message body.

You want to instead have it append contents to the $message variable, as all the other lines before and after it are doing.

e.g.:

								<tr>
									<td><img src="MY-IMAGE/GC-25.jpg"/></td>
									<td>';
									if ($item-name=="CI50") {
										$message .= '<img src="MY-IMAGEGC-50.jpg"/><br />'.$gon;
										} else {
											$message .=  'No Image Available';
										}
										$message .=  '</td>
								</tr>
							</table>';
			$message .= '</html></body>';
				
				mail($to, $subject, $message, $headers);
....

Open in new window

0
 
rgranlundAuthor Commented:
@Frosty  I guess If I had really thought about it, I would have come up with this.  However, thank you for pointing me in the correct and logical direction.

On another note, in an HTML email sent out via PHP where do I place the <style>? In the header?
0
 
Dave BaldwinFixer of ProblemsCommented:
Gmail blocks <style> tags completely and more block style <links>.  See here: http://www.campaignmonitor.com/css/  Inline styles are supported to varying degrees.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now