使用 Swift 发送 HTML 电子邮件是否需要 `<html>`
Do I need `<html>` for sending HTML email using Swift
我想用 swift 发送一个 HTML。我是否需要创建包含 <html>
和 <head>
、<body>
标签的 html
电子邮件,还是 switfmailer 会自动执行此操作?
例如,如果我想发送内容为My <em>amazing</em> body
的html电子邮件,那么使用
就足够了吗?
// Or set it after like this
$message->setBody('My <em>amazing</em> body', 'text/html');
还是需要写
// Set the body
$message->setBody(
'<html>' .
' <head></head>' .
' <body>' .
' My <em>amazing</em> body' .
' </body>' .
'</html>',
'text/html' // Mark the content-type as HTML
);
正确的做法是什么?
如果您想发送包含 html 内容的电子邮件,您必须
设置内容类型为text/html
:
$message->setBody($yourHtml, 'text/html');
使用 html 客户端可以容忍并正确显示的内容。
这可能与格式正确的 html 消息没有太大关系。许多电子邮件客户端往往不遵循网络标准。因此,我建议使用在线验证器根据不同的常用电子邮件客户端检查您的 html 内容。几个例子:
- https://www.google.com/webmasters/markup-tester/
- https://www.w3schools.com/js/tryit.asp?filename=tryjs_form_validate_email
- http://emaillint.com/
顺便说一下,使用 swiftmailer,您甚至可以在一封电子邮件中添加两种内容类型,如下所示:
[...]
$message->setBody('My <em>html</em> body', 'text/html');
$message->addPart('My plain text', 'text/plain');
[...]
我想用 swift 发送一个 HTML。我是否需要创建包含 <html>
和 <head>
、<body>
标签的 html
电子邮件,还是 switfmailer 会自动执行此操作?
例如,如果我想发送内容为My <em>amazing</em> body
的html电子邮件,那么使用
// Or set it after like this
$message->setBody('My <em>amazing</em> body', 'text/html');
还是需要写
// Set the body
$message->setBody(
'<html>' .
' <head></head>' .
' <body>' .
' My <em>amazing</em> body' .
' </body>' .
'</html>',
'text/html' // Mark the content-type as HTML
);
正确的做法是什么?
如果您想发送包含 html 内容的电子邮件,您必须
设置内容类型为
text/html
:$message->setBody($yourHtml, 'text/html');
使用 html 客户端可以容忍并正确显示的内容。
这可能与格式正确的 html 消息没有太大关系。许多电子邮件客户端往往不遵循网络标准。因此,我建议使用在线验证器根据不同的常用电子邮件客户端检查您的 html 内容。几个例子:
- https://www.google.com/webmasters/markup-tester/
- https://www.w3schools.com/js/tryit.asp?filename=tryjs_form_validate_email
- http://emaillint.com/
顺便说一下,使用 swiftmailer,您甚至可以在一封电子邮件中添加两种内容类型,如下所示:
[...]
$message->setBody('My <em>html</em> body', 'text/html');
$message->addPart('My plain text', 'text/plain');
[...]