简体   繁体   English

从php文档发送HTML电子邮件

[英]Sending an HTML email, from php document

So I am in the process of gathering a user's email address and submitting it to a database - but what I would really like to do is also submit an HTML email to them. 因此,我正在收集用户的电子邮件地址并将其提交给数据库-但是,我真正想做的就是向他们提交HTML电子邮件。

I have created the html of the email (with inline css, it is literally 1 page) and it looks like this: 我已经创建了电子邮件的html(使用内联css,实际上是1页),它看起来像这样: 在此处输入图片说明

So then I took this code and wrote the email submittingness in php: 因此,然后我接受了这段代码,并在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); 

But the problem is that it comes through like this: 但是问题在于它是通过这样的方式来实现的:

在此处输入图片说明

So how do I get this to work properly? 那么如何使它正常工作? Also, just a side note - the emails always go straight to junk mail and I don't know how to change this... the user won't see the email if it goes straight there. 另外,还有一点说明-电子邮件总是直接变成垃圾邮件,我不知道该如何更改...如果直接进入那里,用户将看不到电子邮件。

Thanks for the help! 谢谢您的帮助!

Your email is correctly being sent as HTML. 您的电子邮件已正确发送为HTML。 However, CSS/HTML support in email clients is akin to the situation we had in 1998. 但是,电子邮件客户端中的CSS / HTML支持类似于1998年的情况。

Gmail for example doesn't support <style> tags which is why your email is rendering the way it is. 例如,Gmail不支持<style>标记,这就是为什么您的电子邮件以这种方式呈现的原因。 I suggest you take a look at the following chart in order to see what each email client supports and to tailor your HTML template accordingly. 我建议您查看以下图表 ,以了解每个电子邮件客户端支持什么并相应地定制HTML模板。

So, essentially, your email has to be written as such: 因此,基本上,您的电子邮件必须这样写:

<!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>

I would recommend using an email library. 我建议使用电子邮件库。 SwiftMailer has been around for awhile and it's very well featured and robust. SwiftMailer已经存在了一段时间,并且功能强大且功能强大。

The documentation for creating html messages is here: http://swiftmailer.org/docs/messages.html 用于创建html消息的文档在这里: http : //swiftmailer.org/docs/messages.html

For example, this creates an email message that supports both HTML and plain text for legacy clients: 例如,这将创建同时支持旧客户端的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');

Regarding emails going to spam, I would recommend reading So You'd Like To Send Some Email (Through Code) . 关于将电子邮件发送给垃圾邮件,我建议您阅读“您想发送一些电子邮件(通过代码)” The key is to either configure your server correctly for sending email or use a third party provider like postmarkapp or sendgrid 关键是要正确或者配置服务器发送电子邮件或使用第三方提供商像postmarkappsendgrid

Hope that helps. 希望能有所帮助。

UPDATE: 更新:

The above was meant as a suggestion to improve email handling in your code. 以上是对改善代码中电子邮件处理的建议。 Perhaps it didn't address the fact that Gmail doesn't support tags, but I stand by recommending the use of an email library. 也许它不能解决Gmail不支持标签的事实,但是我建议您使用电子邮件库。

HTML in emails is send as another file. 电子邮件中的HTML作为另一个文件发送。 You should pass plain text email (for those who dont use fancy mail clients with html viewer) and need an additional file with mime type text/html using base64 and some email building rules. 您应该传递纯文本电子邮件(对于那些不使用带有html查看器的奇特邮件客户端的电子邮件),并且需要使用base64和一些电子邮件构建规则的mime类型text / html的其他文件。 To see how it looks (in gmail) open any html message and then find see orginal function (in "v" menu next to reply button above message). 要查看其外观(在gmail中),请打开任何html消息,然后找到原始功能(在消息上方的回复按钮旁边的“ v”菜单中)。

In simplest case (without plaintext) you could try this: mail($to, $subject, $message, "From: $from\\nContent-Type: text/html; charset=iso-8859-1"); 在最简单的情况下(无明文),您可以尝试以下操作:mail($ to,$ subject,$ message,“发件人:$ from \\ nContent-Type:text / html; charset = iso-8859-1”);

For attaching also plaintext for some readers convenience or attaching files (and images) look there: http://www.php.net/manual/en/function.mail.php#105661 ("Send Multi attachment email" comment). 对于某些读者来说,也可以附加明文或附加文件(和图像),请参见http : //www.php.net/manual/en/function.mail.php#105661 (“发送多附件电子邮件”注释)。

There's an example of combining both plaintext and html: http://www.php.net/manual/en/function.mail.php#83491 (comment with "28-May-2008 01:55" date) 有一个将纯文本和html结合使用的示例: http : //www.php.net/manual/zh/function.mail.php#83491 (注释日期为“ 2008年5月28日01:55”)

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

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