【问题标题】:How can I add HTML formatting to 'Swift Mail tutorial based' PHP email?如何将 HTML 格式添加到“基于 Swift Mail 教程”的 PHP 电子邮件?
【发布时间】:2010-04-29 15:22:59
【问题描述】:

我为客户开发了一个竞赛页面,他们希望客户收到的电子邮件不仅仅是文本。我使用的教程仅在“发送正文消息”中提供了简单的文本。我需要添加 html 以感谢客户进入,并在此电子邮件中介绍图像。

代码是:

//send the welcome letter
function send_email($info){

    //format each email
    $body = format_email($info,'html');
    $body_plain_txt = format_email($info,'txt');

    //setup the mailer
    $transport = Swift_MailTransport::newInstance();
    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance();
    $message ->setSubject('Thanks for entering the competition');
    $message ->setFrom(array('info@examplemail.com' => 'FromEmailExample'));
    $message ->setTo(array($info['email'] => $info['name']));

    $message ->setBody('Thanks for entering the competition, we will be in touch if you are a lucky winner.');

    $result = $mailer->send($message);

    return $result;

}

这个function.php工作表正在工作,客户正在收到他们的电子邮件,我只需要更改

('感谢您参加比赛, 如果您很幸运,我们会与您联系 赢家。')

改为使用 HTML...

如果可以,请提供一个示例,说明如何将 HTML 集成到此功能中。

【问题讨论】:

    标签: php html email


    【解决方案1】:

    您也可以将 'text/html' 添加到 setBody (ref):

    ->setBody($this->renderView('YouBundleName:Default:email.html.twig'), 'text/html');
    

    【讨论】:

      【解决方案2】:
      $message = Swift_Message::newInstance();
      $message->setContentType('text=html');
      

      【讨论】:

      • 嗨,盖伦,感谢您的快速回复,我不确定应该在哪里插入这个?我应该删除任何行并用您的建议替换它们吗?
      【解决方案3】:

      会保留相同的文本还是应该以某种方式对其进行样式设置? 在 html 中编辑您的电子邮件需要内联 css 样式,例如:

      ('<p style="font-size:1.2em; color:#f0f0f0;">Thanks for entering the competition, we will be in touch if you are a lucky winner.</p>')
      

      如果您需要一张桌子,只需添加:

      ('<table style="font-size:1.2em; color:#f0f0f0;"><tr><td>Thanks for entering the competition, we will be in touch if you are a lucky winner.</td></tr></table>')
      

      或者让它更简单

      $message_body'
      <table style="font-size:1.2em; color:#f0f0f0;">
      <tr>
      <td>Thanks for entering the competition, we will be in touch if you are a lucky winner.</td>
      </tr>
      </table>
      ';
      
      $message ->setBody($message_body);
      

      我知道当我需要发送 html 电子邮件时,我需要将 content-type 设置为 html,我相信您在以下行中这样做了

      $body = format_email($info,'html');
      

      我希望这就是您想要的。如果不告诉我

      【讨论】:

      • 嗨 Krike,感谢您回复我,我已经尝试过这种在函数中输入表格代码的方法,基本上我收到的电子邮件会显示标签,而不是使用标签设置文本样式??你知道解决这个问题的任何其他方法吗?
      • 没关系,我搞定了。非常感谢您的帮助 Krike。 :-)
      • 很高兴你可以让它工作,我认为内容类型已经设置为 html 但我猜它没有看到 galens 回复。
      猜你喜欢
      • 2016-06-10
      • 2013-03-20
      • 1970-01-01
      • 2017-11-22
      • 2010-12-09
      • 2011-05-23
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多