【问题标题】:Sending <a> tag in body of email with Swift Mailer使用 Swift Mailer 在电子邮件正文中发送 <a> 标记
【发布时间】:2014-03-10 21:27:49
【问题描述】:

我是 Swift Mailer 的新手,想在 HTML 消息的正文中包含一个链接,链接回我的主页,但是,当我在消息中包含锚标记时,PHP 脚本不会运行(脚本会当我包含不同的元素(例如标题标签)时工作。不确定我可能会丢失什么。

PHP:

 <?php require_once '../Swift/lib/swift_required.php';

    // Create the Transport
    $transport = Swift_SmtpTransport::newInstance()
    ->setUsername('admin@mazihealth.com')
    ->setPassword('adminxxxx');

    // Create the Mailer using your created Transport
      $mailer = Swift_Mailer::newInstance($transport);
      $message = Swift_Message::newInstance();
      $message->setSubject('My subject');
      $message->setFrom('abc@mazihealth.com');
      $message->setTo('abc3@gmail.com');
      $cid = $message->embed(Swift_Image::fromPath('images/MaziFinal_email.jpg'));
      $message->setBody(
      '<html>' .
      ' <head></head>' .
      ' <body>' .
      ' This is my message' .
      '<a href="'http://www.mazihealth.com'">Sign in</a>'.
      ' </body>' .
      '</html>',
      'text/html' // Mark the content-type as HTML
      );

    // Send the message
      $result = $mailer->send($message);

      echo $message->toString();

      ?>

【问题讨论】:

    标签: php email swiftmailer


    【解决方案1】:

    您有语法错误。只需删除href 中的单引号:

    $message->setBody(
        '<html>' .
        ' <head></head>' .
        ' <body>' .
        ' This is my message' .
        '<a href="http://www.mazihealth.com">Sign in</a>'.
        ' </body>' .
        '</html>',
        'text/html' // Mark the content-type as HTML
    );
    

    此外,您可能会发现使用heredoc 语句来概述您的内容更容易。它使阅读/编辑大字符串更容易。这是一个例子:

    $body = <<<EOD
    <html>
        <head></head>
        <body>
            This is my message .
            <a href="http://www.mazihealth.com">Sign in</a>
        </body>
    </html>
    EOD;
    
    $message->setBody(
        $body,
        'text/html' // Mark the content-type as HTML
    );
    

    【讨论】:

    • 就是这样。不过有趣的是,要嵌入图像,我需要在图像 src 周围加上单引号。我想这让我失望了。
    • 感谢您对 heredoc 声明的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 2016-03-29
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    相关资源
    最近更新 更多