php邮件:如何发送html?
下面的代码正确发送电子邮件,但针对正文。我需要在消息正文中显示 html,但无法实现。网络中的示例不会发送电子邮件:(
我怎样才能修复我的代码以发送正文中包含 html 的电子邮件?
非常感谢!
<?php
$to = '[email protected]';
$subject = 'I need to show html';
$from ='[email protected]';
$body = '<p style=color:red;>This text should be red</p>';
ini_set("sendmail_from", $from);
$headers = "From: " . $from . "\r\nReply-To: " . $from . "";
$headers .= "Content-type: text/html\r\n";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Sent</p>");
} else {
echo("<p>Error...</p>");
}
?>
The code below is sending an email correctly but for the body. I need to show html in the body of the message and I cannot make it. The examples in the web won't send the email :(
How can I fix my code to send the email with the html in the body?
Thanks a ton!
<?php
$to = '[email protected]';
$subject = 'I need to show html';
$from ='[email protected]';
$body = '<p style=color:red;>This text should be red</p>';
ini_set("sendmail_from", $from);
$headers = "From: " . $from . "\r\nReply-To: " . $from . "";
$headers .= "Content-type: text/html\r\n";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Sent</p>");
} else {
echo("<p>Error...</p>");
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将此标头用于邮件:
以及内容/正文:
使用内联 css 命令很重要,并建议在界面中使用表格。
...
在您的邮件正文中,您必须放置 带头部和主体的 HTML 代码
use this header for the mail:
and for the content/body:
it's important to use inline css commands and recommanded to use tables for the interface.
...
In your Mail-Body you than have to put HTML code with head and body
您查看过收到邮件的标头吗?它说
只需在此处添加另一个
\r\n
即可:Have you looked at the headers of the incoming mail? It says
Simply add another
\r\n
here:我建议您不要自己乱搞,而是使用网络上提供的众多免费课程之一来完成此操作。
我推荐: PHPMailer
I recommend rather than messing around with doing this yourself you use one of the many free classes available all over the web to do it.
I would recommend: PHPMailer
我发现这个效果很好!
来源
I found this works well!
Source
简单的回答:不要这样做。 HTML 电子邮件既邪恶又烦人。至少如果没有包含正确的纯文本版本。正确 = 与 HTML 版本中的信息相同,而不仅仅是关于获取另一个电子邮件客户端的愚蠢评论或指向 html 版本(如果在网络上可用)的链接。
如果您确实需要它: http://pear.php.net/package/Mail_Mime
Simple answer: Don't do it. HTML emails are evil and annoying. At least if there's no PROPER plaintext version included. Proper = same information like in the HTML version, not just a stupid comment about getting another email client or a link to the html version if it's available on the web.
If you really need it: http://pear.php.net/package/Mail_Mime