你能在 php 电子邮件中插入一个 if 语句吗?


Can you insert an if statement in php email

// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$to = 'your@email.com';
$body = HTML
$message
HTML;
$headers = "From: $email'r'n";
$headers .= "Content-type: text/html'r'n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: thanks.html');

问题是,无论天气与否,您都可以在html电子邮件正文中插入php if语句,或者将其添加到其中的最佳方法。 我正在创建一个详细的清单,提交后,需要通过电子邮件发送到电子邮件。 但我不想通过电子邮件发送完整的库存(包括空输入(,它应该只通过电子邮件发送具有 0 以外的字段。我在想:

if ($armChair> 0( { echo 'Arm Chairs: ' . $_POST['扶手椅']; } ]

但它似乎实际上不起作用...有什么想法吗?

这是一个

结构糟糕的问题。

是的,你绝对可以这样做,但你需要以一种理智的方式去做。我们无法判断您在此处发布的内容,因为您刚刚插入了定义正文的"HTML"。

$body = "Hi there,'r'n";
$body .= $armChair > 0 ? "Arm Chairs: ".$_POST['armCharis']."'r'n" : "";
$body.= "some more text";

如果您的意思是覆盖一段$message,是的,您也可以使用机器标签 {INVENTORY} 之类的东西来做到这一点......

$message = $armChair > 0 ? str_replace('{INVENTORY}', "Arm Chairs: ".$_POST['armCharis']."'r'n", $message) : "";

这当然需要字符串 {INVENTORY} 在您的 $message var 中的某个位置