我目前有PHP发送HTML电子邮件,即使数量为0,也会列出表单中的每个项目。现在我想将其更改为仅列出正在订购的项目(数量> 0)。在浏览了整个互联网并找到在HTML电子邮件中嵌入PHP if语句的方法之后,这就是我想出来的,它仍然没有用......
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];
$AV000100 = $_POST['AV000100'];
$AV000101 = $_POST['AV000101'];
$AV000102 = $_POST['AV000102'];
$comments = $_POST['comments'];
?>
<?php
$to = "me@company.com";
$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);
?>
如何更改嵌入式PHP if语句以将其显示为电子邮件正文...
姓名:John Doe 电子邮件:jon@mail.com 需要的日期:今天 2 - 小册子 2 - 钢笔 评论:谢谢
而不是......
姓名:John Doe 电子邮件:jon@mail.com 需要的日期:今天 2 - 小册子 0 - 文件夹 2 - 钢笔 评论:谢谢
仅供参考...我上面写的PHP if语句没有显示任何项目。任何帮助将不胜感激!
答案 0 :(得分:1)
不太确定你想要什么 - 但如果你想使用评估PHP代码的结果作为消息体,那么你需要使用输出缓冲(或许多内联ifs - 输出缓冲更清晰)。
例如,如果您将所有代码生成在body.php文件中生成消息正文,那么您可以使用:
ob_start();
include('body.php');
$message = ob_get_Clean();
您不需要在单独的文件中包含正文代码 - 只需包含它而不是ob_start()
和ob_get_clean()
答案 1 :(得分:0)
试试这个
$message = "
<html>
<head></head>
<body>
<p>
Name: $name<br>
Email:$email<br>
Date Needed:$date
</p>
...
...";
每次断开字符串,然后使用php
运算符将字符串与.
结果连接起来。
答案 2 :(得分:0)
这就是你想要做的事情。
$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>";
答案 3 :(得分:0)
如果要使用连接赋值运算符(.=
),可以逐行追加消息,并将if语句放在需要它们的位置。像这样:
$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>";
您可以像往常一样发送$message
。
以下是完整代码:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];
$AV000100 = $_POST['AV000100'];
$AV000101 = $_POST['AV000101'];
$AV000102 = $_POST['AV000102'];
$comments = $_POST['comments'];
$to = "me@company.com";
$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);
?>