【问题标题】:using php variables in a html email using swiftmailer使用 swiftmailer 在 html 电子邮件中使用 php 变量
【发布时间】:2025-12-27 00:20:14
【问题描述】:

我正在使用 swiftmailer 从我的网站发送电子邮件。我正在尝试获取要发送的 html 电子邮件,但我想将 php 变量传递给电子邮件。

这可能吗?如果可以,我哪里错了?

->setBody(
    '<html>' .
    ' <head></head>' .
        ' <body>' .
    '  Welcome to my site. To complete registration, please click activate below' .
    '  <a href="http://www.mysite.com/activate.php?=c' rawurlencode ($code)' ">activate  ' .
    ' </body>' .
    '</html>',
    'text/html'
);

【问题讨论】:

    标签: php html swiftmailer


    【解决方案1】:

    我不明白你为什么要使用这个

    <?php rawurlencode ($code);?>
    

    除非

    rawurlencode ($code)

    【讨论】:

    • 我只是在想,因为它是 html 代码,所以我必须删除 php 标签才能使用变量。显然不是。我已经更新了我的代码,但似乎无法解决它给我一个语法错误。
    • 当然你必须连接字符串,即 . rawurlencode ($code) .
    【解决方案2】:
    <pre>
    ->setBody(
        '<html>' .
        ' <head></head>' .
            ' <body>' .
        '  Welcome to my site. To complete registration, please click activate below' .
        '  <a href="http://www.mysite.com/activate.php?=c'. rawurlencode ($code) .' ">activate  ' .
        ' </body>' .
        '</html>',
        'text/html'
    );
    </pre>
    

    【讨论】: