Reputation: 23
I currently have PHP sending an HTML email that lists every item from a form even if the quantity is 0. Now I want to change it to only list items that are being ordered (Qty>0). After looking all over the internet to to and find a way to embed a PHP if statement inside an HTML email, this is what I have come up with and it's still not working...
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];
$AV000100 = $_POST['AV000100'];
$AV000101 = $_POST['AV000101'];
$AV000102 = $_POST['AV000102'];
$comments = $_POST['comments'];
?>
<?php
$to = "[email protected]";
$subject = "Marketing Material Order from $name";
$message = "
<html>
<head></head>
<body>
<p>
Name: $name<br>
Email: $email<br>
Date Needed: $date
</p>
<?php if ($AV000100 != 0) {
$AV000100 - Brochures;
}
?>
<?php if ($AV000101 != 0) {
$AV000101 - Folders;
}
?>
<?php if ($AV000102 != 0) {
$AV000102 - Pens;
}
?>
<p>Comments: $comments</p>
</body>
</html>
";
$headers = "From: $email" . "\r\n" .
$headers = "Reply-To: $email" . "\r\n" .
$headers = 'MIME-Version: 1.0' . "\r\n" .
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
?>
How do I change the embedded PHP if statement to show this as the email body...
Name: John Doe Email: [email protected] Date Needed: Today 2 - Brochures 2 - Pens Comments: Thank you
Instead of...
Name: John Doe Email: [email protected] Date Needed: Today 2 - Brochures 0 - Folders 2 - Pens Comments: Thank you
FYI...the way I have the PHP if statement written above, none of the items show up. Any help would be greatly appreciated!
Upvotes: 0
Views: 175
Reputation: 14810
Try like this
$message = "
<html>
<head></head>
<body>
<p>
Name: $name<br>
Email:$email<br>
Date Needed:$date
</p>
...
...";
Each time you break the string and then concatenate the string with the php
results using .
operator.
Upvotes: 0
Reputation: 3930
You can append the message line by line if you want using the concatenating assignment operator (.=
), and put the if statements where you need them. Like so:
$message = "<html>";
$message .= "<head></head>";
$message .= "<body>";
$message .= "<p>";
$message .= "Name: $name<br>";
$message .= "Email: $email<br>";
$message .= "Date Needed: $date";
$message .= "</p>";
if ($AV000100 != 0) {
$message .= "$AV000100 - Brochures";
}
if ($AV000101 != 0) {
$message .= "$AV000101 - Folders";
}
if ($AV000102 != 0) {
$message .= "$AV000102 - Pens";
}
$message .= "<p>Comments: $comments</p>";
$message .= "</body>";
$message .= "</html>";
You can just send $message
as usual.
Here is the full code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];
$AV000100 = $_POST['AV000100'];
$AV000101 = $_POST['AV000101'];
$AV000102 = $_POST['AV000102'];
$comments = $_POST['comments'];
$to = "[email protected]";
$subject = "Marketing Material Order from $name";
$message = "<html>";
$message .= "<head></head>";
$message .= "<body>";
$message .= "<p>";
$message .= "Name: $name<br>";
$message .= "Email: $email<br>";
$message .= "Date Needed: $date";
$message .= "</p>";
if ($AV000100 != 0) {
$message .= "$AV000100 - Brochures";
}
if ($AV000101 != 0) {
$message .= "$AV000101 - Folders";
}
if ($AV000102 != 0) {
$message .= "$AV000102 - Pens";
}
$message .= "<p>Comments: $comments</p>";
$message .= "</body>";
$message .= "</html>";
$headers = "From: $email" . "\r\n" .
$headers = "Reply-To: $email" . "\r\n" .
$headers = 'MIME-Version: 1.0' . "\r\n" .
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
?>
Upvotes: 0
Reputation: 769
Here's what you want to do with what you have.
$message = "
<html>
<head></head>
<body>
<p>
Name: $name<br>
Email: $email<br>
Date Needed: $date
</p>
" . ($AV000100 != 0 ? $AV000100 . " - Brochures" : "") . "
" . ($AV000101 != 0 ? $AV000101 . " - Folders" : "") . "
" . ($AV000102 != 0 ? $AV000102 . " - Pens" : "") . "
<p>Comments: $comments</p>
</body>
</html>";
Upvotes: 0
Reputation: 6743
Not quite sure what you want - but if you want to use the result of evaluating the PHP code as the message body then you need to use output buffering (or a lot of inline ifs - output buffering is cleaner).
For example, if you put all your code the generate the message body in a file body.php then you could use:
ob_start();
include('body.php');
$message = ob_get_Clean();
You don't need to have the body code in a seperate file - just include it instead of the include statrement betwen the ob_start()
and ob_get_clean()
Upvotes: 1