简体   繁体   English

使用 HTML/CSS 发送 PHP 电子邮件

[英]Sending PHP emails with HTML/CSS

I'm trying to send emails with PHP.我正在尝试用 PHP 发送电子邮件。 I would like to add some style with CSS (no matter if inline, internal or with external file).我想用 CSS 添加一些样式(无论是内联、内部还是外部文件)。

I've tried PHPmailer , but it fails to recognize some elements (such as body ), media queries ( @media ) and declarations ( max-width , inline-block , etc...).我试过PHPmailer ,但它无法识别某些元素(例如body )、媒体查询( @media )和声明( max-widthinline-block等...)。 I'm now trying to use SwiftMailer , but online documentation doesn't mention anything about styling.我现在正在尝试使用SwiftMailer ,但在线文档没有提及任何关于样式的内容。

Just for sake of clarity, here's the snippet I would like to use: JsFiddle为了清楚起见,这是我想使用的片段: JsFiddle

Any ideas to send PHP emails with working HTML/CSS?使用有效的 HTML/CSS 发送 PHP 电子邮件有什么想法吗?

You could use a CSS inliner tool like http://templates.mailchimp.com/resources/inline-css/ to convert your 'normal' template (made like a normal HTML page) to an email-friendly template.您可以使用像http://templates.mailchimp.com/resources/inline-css/这样的 CSS 内联工具将您的“普通”模板(像普通 HTML 页面一样制作)转换为电子邮件友好模板。 This is needed because mail clients doesn't recognise separate elements.这是必需的,因为邮件客户端无法识别单独的元素。 I currently use a template based on the ones made available by Zurb, http://zurb.com/ink/我目前使用的模板基于 Zurb 提供的模板,http: //zurb.com/ink/

I'm trying to send emails with PHP.我正在尝试使用 PHP 发送电子邮件。 I would like to add some style with CSS (no matter if inline, internal or with external file).我想用 CSS 添加一些样式(无论是内联、内部还是外部文件)。

I've tried PHPmailer , but it fails to recognize some elements (such as body ), media queries ( @media ) and declarations ( max-width , inline-block , etc...).我尝试过PHPmailer ,但它无法识别某些元素(例如body )、媒体查询( @media )和声明( max-widthinline-block等...)。 I'm now trying to use SwiftMailer , but online documentation doesn't mention anything about styling.我现在正在尝试使用SwiftMailer ,但在线文档没有提到任何关于样式的内容。

Just for sake of clarity, here's the snippet I would like to use: JsFiddle为了清楚起见,这是我想使用的片段: JsFiddle

Any ideas to send PHP emails with working HTML/CSS?任何使用 HTML/CSS 发送 PHP 电子邮件的想法?

It also depends on the receiver's email client.它还取决于收件人的电子邮件客户端。 Not all clients accept all css.并非所有客户都接受所有 css。 Use a litmus test of some sort to check with different e-mail clients which css you can use.使用某种试金石来检查不同的电子邮件客户端可以使用哪些 css。 Tables often work with email clients, a lot of them don't accept divs I believe.表格通常与电子邮件客户端一起使用,我相信其中很多都不接受 div。

I use PHPMailer to send emails with PHP, and it never fails to recognize these elements (for me anyway).我使用PHPMailer通过 PHP 发送电子邮件,并且它总是能够识别这些元素(无论如何对我来说)。

To use it, i create a fonction:为了使用它,我创建了一个函数:

function sendMail($name, $from, $message, $to, $subject)
{
   $mail = new PHPMailer();
   $body = "<html><head></head><body style=\"background-color: #F2F1F0;\"><p>".$message."</p></body></html>";
   $mail->SetFrom($from, $name);    
   $mail->AddReplyTo($from, $name);

   // Only if you want to send an attachment, send a variable $object to the function
   //$mail->AddAttachment($object);

   $mail->Subject    = $subject;    
   $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test    
   $mail->MsgHTML($body);      
   $mail->AddAddress($to, $name);

   if(!$mail->Send()) {
       $result = $mail->ErrorInfo;
   }
   else
   {
       $result = "Mail sent successfully";
   }
   return $result;
}

And an exemple to use it:以及一个使用它的例子:

// Preparation of the message
$message = '<p class="img"><img src="http://mywebsite.com/img/myImage.png" width="220px"></p>';
$message .= '<p class="txt">Hello Mr Dupond !</p>';
$message .= '<p class="txt">We are glad to have you among us</p>';
$message .= '<p class="txt">Se you soon on our Website !</p>';
$message = utf8_decode($message);

// variables needed
$name = "Mr Oktopuss";
$from = "contact@oktopuss.eu";
$to = "contactAddress@domain.ext";
$subject = "The subject of this mail !";

// Send the mail and receive the result
$result = sendMail($name, $from, $message, $to, $subject);
echo $result;

Maybe that could be help you也许这对你有帮助

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

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