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

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

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?

  1. $mail->Body = "
  2. <p><strong>Name: </strong> $full_name</p>
  3. <p><strong>Email: </strong> $email</p>".
  4. if ($phone !== ''){
  5. echo '<p><strong>Phone: </strong> $phone</p>';
  6. } else {
  7. echo '';
  8. } . "
  9. <p><strong>Message: </strong> $message</p>
  10. ";

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?

  1. $mail-&gt;Body = &quot;
  2. &lt;p&gt;&lt;strong&gt;Name: &lt;/strong&gt; $full_name&lt;/p&gt;
  3. &lt;p&gt;&lt;strong&gt;Email: &lt;/strong&gt; $email&lt;/p&gt;&quot;.
  4. if ($phone !== &#39;&#39;){
  5. echo &#39;&lt;p&gt;&lt;strong&gt;Phone: &lt;/strong&gt; $phone&lt;/p&gt;&#39;;
  6. } else {
  7. echo &#39;&#39;;
  8. } . &quot;
  9. &lt;p&gt;&lt;strong&gt;Message: &lt;/strong&gt; $message&lt;/p&gt;
  10. &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

请不要回声,附加内容:

  1. $mail->Body = "<p><strong>Name: </strong> $full_name</p>
  2. <p><strong>Email: </strong> $email</p>";
  3. if ($phone !== ''){
  4. $mail->Body .= '<p><strong>Phone: </strong> $phone</p>';
  5. }
  6. $mail->Body .= '<p><strong>Message: </strong> $message</p>';
英文:

Don't echo, append content:

  1. $mail-&gt;Body = &quot;&lt;p&gt;&lt;strong&gt;Name: &lt;/strong&gt; $full_name&lt;/p&gt;
  2. &lt;p&gt;&lt;strong&gt;Email: &lt;/strong&gt; $email&lt;/p&gt;&quot;;
  3. if ($phone !== &#39;&#39;){
  4. $mail-&gt;Body .= &#39;&lt;p&gt;&lt;strong&gt;Phone: &lt;/strong&gt; $phone&lt;/p&gt;&#39;;
  5. }
  6. $mail-&gt;Body .= &#39;&lt;p&gt;&lt;strong&gt;Message: &lt;/strong&gt; $message&lt;/p&gt;&#39;;

答案2

得分: 1

以下是翻译好的部分:

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

Another alternative, which works better in some situations:

  1. if( $phone )
  2. $phone = &quot;&lt;p&gt;&lt;strong&gt;Phone: &lt;/strong&gt;$phone&lt;/p&gt;&quot;;
  3. $mail-&gt;Body = &quot;&lt;p&gt;&lt;strong&gt;Name: &lt;/strong&gt;$full_name&lt;/p&gt;
  4. &lt;p&gt;&lt;strong&gt;Email: &lt;/strong&gt;$email&lt;/p&gt;
  5. $phone
  6. &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;

英文:
  1. $string = &quot;
  2. &lt;p&gt;&lt;strong&gt;Name: &lt;/strong&gt; $full_name&lt;/p&gt;
  3. &lt;p&gt;&lt;strong&gt;Email: &lt;/strong&gt; $email&lt;/p&gt;&quot;.
  4. ($phone !== &#39;&#39; ? &#39;&lt;p&gt;&lt;strong&gt;Phone: &lt;/strong&gt; $phone&lt;/p&gt;&#39; : &#39;&#39;).
  5. &quot;&lt;p&gt;&lt;strong&gt;Message: &lt;/strong&gt; $message&lt;/p&gt;&quot;;
  6. $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:

确定