我正在尝试通过PHP发送电子邮件,其中正文依赖于几个if语句。
我知道你不能在mail()函数中调用if语句,所以我想知道是否可以在多个变量中声明正文项,然后将它们调用到mail()体中
到目前为止我所拥有的内容如下,但我确信你可以猜到,它不起作用,真的很感激一些反馈。
if (!empty($phone)) {$email_phone = echo 'Phone Number: ' $phone \r\n};
mail('email@domain.com', 'New Visitor Information', "Hello Pastor,\r\n\r\nWe had a new visitor on " . $visit_date . "\r\n\r\nTheir Details:\r\n\r\nName: " . $first_name . " " . $last_name . "\r\n" . $email_phone . " ");
答案 0 :(得分:2)
试试这个
<?php
if (!empty($phone)) {$email_phone = "Phone Number: ".$phone; };
mail('user@domain.com', 'New Visitor Information', "Hello Pastor Steve,\r\n\r\nWe had a new visitor on " . $visit_date . "\r\n\r\nTheir Details:\r\n\r\nName: " . $first_name . " " . $last_name . "\r\n" . $email_phone . " ");
?>
答案 1 :(得分:2)
if (!empty($phone)){
$email_phone = "Phone Number: " . $phone . "\r\n";
$email = "email@domain.com";
$subject = "New Visitor Information";
$body = "Hello Pastor,\r\n\r\nWe had a new visitor on " . $visit_date . "\r\n\r\nTheir Details:\r\n\r\nName: " . $first_name . " " . $last_name . "\r\n" . $email_phone . ";
mail($email, $subject, $body)
};
这样,只有有电话号码值才会发送邮件。
答案 2 :(得分:1)
首先建立身体,然后发送它:
$body = 'Hello, world';
if ($some_condition) {
$body .= " blah blah blah";
}
if ($other_condition) {
$body .= "blah blah blah";
}
mail(....);
答案 3 :(得分:1)
我总是这样做:
$mailTo = "steve@domain.com";
$subject = "New Visitor Information";
$message=array();
$message[] = "Hello Pastor Steve,";
$message[] = "";
$message[] = "We had a new visitor on " . $visit_date;
$message[] = "";
$message[] = "Their Details:";
$message[] = "";
$message[] = "Name: " . $first_name . " " . $last_name;
$message[] = "";
if (!empty($phone)) {
$message[] = "Phone Number: " . $phone;
}
$message = implode("\r\n", $message);
mail($mailTo, $subject, $message);
我使用数组来连接一些字符串。这比使用点运算符连接字符串要快得多。因此它的格式更清晰,因此您可以更清楚地发现空行。现在,您可以看到如何使用if子句和变量。