简体   繁体   English

我需要` <html> `用于使用Swift发送HTML电子邮件

[英]Do I need `<html>` for sending HTML email using Swift

I want to send an HTML with swift. 我想快速发送HTML。 Do I need to create an html email including <html> and <head> , <body> tag, or will switfmailer do this automatically? 我是否需要创建一个包含<html><head><body>标记的html电子邮件,否则switfmailer会自动执行此操作吗?

For example, if I want to sent the html email with the content My <em>amazing</em> body is it then sufficent to use 例如,如果我想发送内容为“ My <em>amazing</em> body的html电子邮件,则足以使用

// Or set it after like this
$message->setBody('My <em>amazing</em> body', 'text/html');

or do I need to write 还是我需要写

// Set the body
$message->setBody(
'<html>' .
' <head></head>' .
' <body>' .
' My <em>amazing</em> body' . 
' </body>' .
'</html>',
  'text/html' // Mark the content-type as HTML
);

Whats the right way to do it? 什么是正确的方法?

If you want to send emails with html content, you have to 如果要发送包含html内容的电子邮件,则必须

  1. set the content type to text/html : 将内容类型设置为text/html

    $message->setBody($yourHtml, 'text/html');

  2. use html content which the clients tolerate and display properly. 使用客户端可以容忍并正确显示的html内容。

This might have not much to do with a properly formed html message. 这可能与格式正确的html消息无关。 Many email clients tend to not follow web standards. 许多电子邮件客户端倾向于不遵循Web标准。 Therefore I suggest using an online validator to check your html content against different, commonly used email clients. 因此,我建议使用在线验证器对照不同的常用电子邮件客户端检查您的html内容。 A few examples: 一些例子:

By the way, with swiftmailer you can even add both content types within one email, like this: 顺便说一句,使用swiftmailer,您甚至可以在一封电子邮件中添加两种内容类型,如下所示:

[...]
$message->setBody('My <em>html</em> body', 'text/html');
$message->addPart('My plain text', 'text/plain');
[...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM