从php文档发送HTML电子邮件

时间:2011-09-10 11:03:18

标签: php html-email

所以我正在收集用户的电子邮件地址并将其提交到数据库 - 但我真正想做的还是向他们提交HTML电子邮件。

我创建了电子邮件的html(带内联css,实际上是1页),它看起来像这样: enter image description here

然后我接受了这段代码并在php中写了电子邮件提交:

ini_set("sendmail_from", "emailer@benjaminpotter.org");

    $to = $email;
    $subject = "Ben Potter Web Design - Newsletter Form Submission";   

    $emailthing = '    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form Submission - Newsletter Update</title>
</head>
<style type="text/css">
body {
    background-color:#e8faff;   
}

p {
    font-size:15px;
    font-family:Georgia, "Times New Roman", Times, serif;
    color:#141414;
    font-style:italic;
}

#mainbox{
    background-image:url(http://www.benjaminpotter.org/Images/emailtotheuser.jpg);
    width:345px;
    height:404px;   
    margin-left:auto;
    margin-right:auto;
    display:block;
    margin-top:50px;
}

#mainbox #textholder{
    width:279px;
    height:300px;
    display:block;
    position:absolute;
    margin-top:80px;
    margin-left:30px;   
}

#special{
    color:#72C0EF;
    text-align:center !important;
    font-style:normal;
}
</style>
<body>
<div id="mainbox">
<div id="textholder">
<p>
Thanks for signing up!<br />
I currently have you under the email:<br />
<span id="special">' . $email . '</span>
<br />
<br />
You will be receiving tri-monthly / <br />
quarterly emails from me, updating you<br />
on my latest works.<br />
<br />
Thank you for your intrest in <strong>Ben Potter
Web Design</strong>, you will receive the first newsletter soon.<br />
<br />
<br />
Yours Sincerely,
<br />
<strong>Ben Potter</strong>
</p>
</div>
</div>
</body>
</html>';  

$Rconfucious = "MIME-Version: 1.0\r\n";   
$Rconfucious .= "Content-type: text/html; charset=iso-8859-1\r\n";        
$Rconfucious .= 'From: Ben Potter Web Design 2011 <emailer@benjaminpotter.org>' . "\r\n";

@mail($to, $subject, $emailthing, $Rconfucious); 

但问题在于它是这样的:

enter image description here

那么如何让它正常工作? 此外,只是旁注 - 电子邮件总是直接发送到垃圾邮件,我不知道如何更改...如果直接在那里用户将不会看到电子邮件。

感谢您的帮助!

3 个答案:

答案 0 :(得分:4)

您的电子邮件正确以HTML格式发送。但是,电子邮件客户端中的CSS / HTML支持类似于我们1998年的情况。

例如,

Gmail不支持<style>标记,这就是您的电子邮件呈现方式的原因。我建议你看一下the following chart,看看每个电子邮件客户端支持的内容,并相应地定制你的HTML模板。

所以,基本上,你的电子邮件必须这样写:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form Submission - Newsletter Update</title>
</head>
<body bgcolor="#72c0ef">
<div style="background-image:url(http://www.benjaminpotter.org/Images/emailtotheuser.jpg); width:345px; height:404px; margin-left:auto; margin-right:auto; display:block; margin-top:50px;">
<div style="width:279px; height:300px; display:block; position:absolute; margin-top:80px; margin-left:30px;">
<p style="font-size:15px; font-family:Georgia, \'Times New Roman\', Times, serif; color:#141414; font-style:italic;">
Thanks for signing up!<br />
I currently have you under the email:<br />
<span class="color:#72C0EF; text-align:center !important; font-style:normal;">' . $email . '</span>
<br />
<br />
You will be receiving tri-monthly / <br />
quarterly emails from me, updating you<br />
on my latest works.<br />
<br />
Thank you for your intrest in <strong>Ben Potter
Web Design</strong>, you will receive the first newsletter soon.<br />
<br />
<br />
Yours Sincerely,
<br />
<strong>Ben Potter</strong>
</p>
</div>
</div>
</body>
</html>

答案 1 :(得分:1)

我建议使用电子邮件库。 SwiftMailer已经存在了一段时间,而且功能非常强大且非常强大。

创建html消息的文档位于:http://swiftmailer.org/docs/messages.html

例如,这会创建一个支持旧版客户端的HTML和纯文本的电子邮件:

//Pass it as a parameter when you create the message
$message = Swift_Message::newInstance('Subject here', 'My amazing body');

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

//Add alternative parts with addPart()
$message->addPart('My amazing body in plain text', 'text/plain');

关于发送垃圾邮件的电子邮件,我建议您阅读So You'd Like To Send Some Email (Through Code)。关键是要正确配置服务器以发送电子邮件或使用第三方提供商,如postmarkappsendgrid

希望有所帮助。

<强>更新

上述内容旨在改善代码中的电子邮件处理。也许它没有解决Gmail不支持标签的事实,但我建议使用电子邮件库。

答案 2 :(得分:0)

电子邮件中的HTML作为另一个文件发送。您应该传递纯文本电子邮件(对于那些不使用带有html查看器的花哨邮件客户端的人)并且需要使用base64和一些电子邮件构建规则的mime类型text / html的附加文件。要查看它的外观(在gmail中)打开任何html消息,然后找到原始功能(在消息上方的回复按钮旁边的“v”菜单中)。

在最简单的情况下(没有明文)你可以试试这个: mail($ to,$ subject,$ message,“From:$ from \ nContent-Type:text / html; charset = iso-8859-1”);

为了方便某些读者附加明文或附加文件(和图像),请查看: http://www.php.net/manual/en/function.mail.php#105661(“发送多附件电子邮件”评论)。

有一个结合明文和html的例子: http://www.php.net/manual/en/function.mail.php#83491(评论“2008年5月28日01:55”日期)