簡體   English   中英

<a>使用Swift Mailer在電子郵件正文中</a>發送<a>標簽</a>

[英]Sending <a> tag in body of email with Swift Mailer

我是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();

      ?>

您有語法錯誤。 只需刪除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
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM