如何在phpMailer的`$mail->Body`中添加表达式?

huangapple go评论68阅读模式
英文:

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-&gt;Body = &quot;
  &lt;p&gt;&lt;strong&gt;Name: &lt;/strong&gt; $full_name&lt;/p&gt; 
  &lt;p&gt;&lt;strong&gt;Email: &lt;/strong&gt; $email&lt;/p&gt;&quot;. 
  if ($phone !== &#39;&#39;){
    echo &#39;&lt;p&gt;&lt;strong&gt;Phone: &lt;/strong&gt; $phone&lt;/p&gt;&#39;;
  } else {
    echo &#39;&#39;;
  } . &quot;
  &lt;p&gt;&lt;strong&gt;Message: &lt;/strong&gt; $message&lt;/p&gt; 
&quot;;

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-&gt;Body = &quot;&lt;p&gt;&lt;strong&gt;Name: &lt;/strong&gt; $full_name&lt;/p&gt; 
  &lt;p&gt;&lt;strong&gt;Email: &lt;/strong&gt; $email&lt;/p&gt;&quot;;
 
  if ($phone !== &#39;&#39;){
    $mail-&gt;Body .= &#39;&lt;p&gt;&lt;strong&gt;Phone: &lt;/strong&gt; $phone&lt;/p&gt;&#39;;
  }

  $mail-&gt;Body .= &#39;&lt;p&gt;&lt;strong&gt;Message: &lt;/strong&gt; $message&lt;/p&gt;&#39;;

答案2

得分: 1

以下是翻译好的部分:

if( $phone )
    $phone = "&lt;p&gt;&lt;strong&gt;电话: &lt;/strong&gt;$phone&lt;/p&gt;";
$mail-&gt;Body = "&lt;p&gt;&lt;strong&gt;姓名: &lt;/strong&gt;$full_name&lt;/p&gt; 
  &lt;p&gt;&lt;strong&gt;电子邮件: &lt;/strong&gt;$email&lt;/p&gt;
  $phone
  &lt;p&gt;&lt;strong&gt;信息: &lt;/strong&gt;$message&lt;/p&gt;";
英文:

Another alternative, which works better in some situations:

if( $phone )
    $phone = &quot;&lt;p&gt;&lt;strong&gt;Phone: &lt;/strong&gt;$phone&lt;/p&gt;&quot;;
$mail-&gt;Body = &quot;&lt;p&gt;&lt;strong&gt;Name: &lt;/strong&gt;$full_name&lt;/p&gt; 
  &lt;p&gt;&lt;strong&gt;Email: &lt;/strong&gt;$email&lt;/p&gt;
  $phone
  &lt;p&gt;&lt;strong&gt;Message: &lt;/strong&gt;$message&lt;/p&gt;&quot;;

答案3

得分: 0

$string = "

姓名: $full_name

电子邮件: $email

".
($phone !== '' ? "

电话: $phone

" : '').
"

消息: $message

";
$mail->Body = $string;

英文:
$string = &quot;
  &lt;p&gt;&lt;strong&gt;Name: &lt;/strong&gt; $full_name&lt;/p&gt; 
  &lt;p&gt;&lt;strong&gt;Email: &lt;/strong&gt; $email&lt;/p&gt;&quot;. 
  ($phone !== &#39;&#39; ? &#39;&lt;p&gt;&lt;strong&gt;Phone: &lt;/strong&gt; $phone&lt;/p&gt;&#39; : &#39;&#39;).
&quot;&lt;p&gt;&lt;strong&gt;Message: &lt;/strong&gt; $message&lt;/p&gt;&quot;;
$mail-&gt;Body = $string;

This is easier. Make the string first then append the string to the body of the mail.

huangapple
  • 本文由 发表于 2023年2月10日 03:31:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定