【问题标题】:How to show a php variable inside html tags?如何在 html 标签中显示 php 变量?
【发布时间】:2023-01-10 23:46:12
【问题描述】:

我正在使用 phpmailer 通过电子邮件发送 otp,在正文部分在电子邮件中,我试图在 $otp 标签中显示一个 php 变量 $otp 但它只显示 $otp 而不是值

//value of $otp
$ot=rand(100000,999999);
$otp=strval($ot);

的变量。我尝试使用以下代码。

    $mail->Body    = 
    '<h2 style="background: #00466a;margin: 0 auto;width: max-content;padding: 0 10px;color: #fff;border-radius: 4px;">$otp</h2>
    ';

但这只会在邮件中返回 $otp 而不是它的价值

【问题讨论】:

  • 并使用此代码...? $mail-&gt;Body = '&lt;h2 style="background: #00466a;margin: 0 auto;width: max-content;padding: 0 10px;color: #fff;border-radius: 4px;"&gt;'.$otp.'&lt;/h2&gt;';

标签: php html phpmailer


【解决方案1】:

如果要在字符串中显示 php 变量,则需要使用 '" 关闭字符串,具体取决于您使用的是什么,然后在点后使用 .,您可以编写 php.ini 。

所以它看起来像'string' . $variable. 'string';

$mail->Body = '<h2 style="background: #00466a;margin: 0 auto;width: max-content;padding: 0 10px;color: #fff;border-radius: 4px;">'
    . $otp . '</h2>';

【讨论】:

    【解决方案2】:

    把你的邮件正文像这样

    $mail->Body  =  "<h2 style='background: #00466a;margin: 0 auto;width: max-content;padding: 0 10px;color: #fff;border-radius: 4px;'>
       $otp
    </h2>";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      相关资源
      最近更新 更多