英文:
How can I add an expression inside the $mail-> Body in phpMailer?
问题
以下是翻译好的部分:
"The phone number in a form isn't required. I would like to hide the label if the phone number field didn't collect any data.
Something like this in theory?
$mail->Body = "
<p><strong>Name: </strong> $full_name</p>
<p><strong>Email: </strong> $email</p>".
if ($phone !== ''){
echo '<p><strong>Phone: </strong> $phone</p>';
} else {
echo '';
} . "
<p><strong>Message: </strong> $message</p>
";
But is not the right syntax. How can I do this? p.s. a Ternary expression would even be cleaner, but it didn't work either"
希望这有帮助。如果您需要进一步的协助,请随时提问。
英文:
The phone number in a form isn't required. I would like to hide the label if the phone number field didn't collect any data.
Something like this in theory?
$mail->Body = "
<p><strong>Name: </strong> $full_name</p>
<p><strong>Email: </strong> $email</p>".
if ($phone !== ''){
echo '<p><strong>Phone: </strong> $phone</p>';
} else {
echo '';
} . "
<p><strong>Message: </strong> $message</p>
";
But is not the right syntax. How can I do this? p.s. a Ternary expression would even be cleaner, but it didn't work either
答案1
得分: 1
请不要回声,附加内容:
$mail->Body = "<p><strong>Name: </strong> $full_name</p>
<p><strong>Email: </strong> $email</p>";
if ($phone !== ''){
$mail->Body .= '<p><strong>Phone: </strong> $phone</p>';
}
$mail->Body .= '<p><strong>Message: </strong> $message</p>';
英文:
Don't echo, append content:
$mail->Body = "<p><strong>Name: </strong> $full_name</p>
<p><strong>Email: </strong> $email</p>";
if ($phone !== ''){
$mail->Body .= '<p><strong>Phone: </strong> $phone</p>';
}
$mail->Body .= '<p><strong>Message: </strong> $message</p>';
答案2
得分: 1
以下是翻译好的部分:
if( $phone )
$phone = "<p><strong>电话: </strong>$phone</p>";
$mail->Body = "<p><strong>姓名: </strong>$full_name</p>
<p><strong>电子邮件: </strong>$email</p>
$phone
<p><strong>信息: </strong>$message</p>";
英文:
Another alternative, which works better in some situations:
if( $phone )
$phone = "<p><strong>Phone: </strong>$phone</p>";
$mail->Body = "<p><strong>Name: </strong>$full_name</p>
<p><strong>Email: </strong>$email</p>
$phone
<p><strong>Message: </strong>$message</p>";
答案3
得分: 0
$string = "
姓名: $full_name
电子邮件: $email
".
($phone !== '' ? "
电话: $phone
" : '').
"
消息: $message
";
$mail->Body = $string;
英文:
$string = "
<p><strong>Name: </strong> $full_name</p>
<p><strong>Email: </strong> $email</p>".
($phone !== '' ? '<p><strong>Phone: </strong> $phone</p>' : '').
"<p><strong>Message: </strong> $message</p>";
$mail->Body = $string;
This is easier. Make the string first then append the string to the body of the mail.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论