2016-02-27 57 views
4

我到处都找过这个问题,而我对这个发现是增加以下内容:添加风格的PHP的HTML电子邮件

$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

我想送通讯类型的电子邮件,所以造型真的很重要。我看到的所有视频都只是制作HTML表单,所以我真的没有那么做。我想在我的电子邮件中设置内容的样式。

我现在有这个权利:

$to = $newsletter_email; 
     $subject = 'Thank you for subscribing'; 
     $message = ' 
      <html> 
      <head> 
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
       <title></title> 
      <style> 
       #email-wrap { 
       background: #151515; 
       color: #FFF; 
       } 
      </style> 
      </head> 
      <body> 
       <div id="email-wrap"> 
       <p>Hi,</p><br> 
       <p>Thank you.</p><br> 
       <p>Thank you,</p> 
       <p>Administration</p> 
       </div> 
      </body> 
      </html> 
       '; 

       $from = "[email protected]"; 
       //$Bcc = "[email protected]"; 

       // To send HTML mail, the Content-type header must be set 
       $headers = 'MIME-Version: 1.0' . "\r\n"; 
       $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

       // Additional headers 
       $headers .= 'To: ' .$to. "\r\n"; 
       $headers .= 'From: ' .$from. "\r\n"; 
      // $headers .= 'Bcc: '.$Bcc. "\r\n"; 

       // Send the email 
       mail($to,$subject,$message,$headers); 

我试图从这个消息变量取出风格,把这个文件转换为HTML风格的文件时,PHP之外:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Newsletter</title> 
<style> 
#email-wrap { 
    background: #151515; 
    color: #FFF; 
} 
</style> 
</head> 

电子邮件实际发送,但我不知道如何添加样式。我究竟做错了什么??

  $email_from = "[email protected]"; 

      $full_name = 'Company Name'; 
      //$from_mail = $full_name.'<'.$email_from.'>'; 
      $from = $from_mail; 

      //$from = "[email protected]"; 
      //$Bcc = "[email protected]"; 

      // To send HTML mail, the Content-type header must be set 
      $headers .= "From: ".$full_name." <".$email_from.">\r\n"; 
      and $headers .= "Return-Path: ".$full_name." <".$email_from.">\r\n"; 
      /*$headers = "" . 
         "Reply-To:" . $from . "\r\n" . 
         "X-Mailer: PHP/" . phpversion();*/ 
      $headers = 'MIME-Version: 1.0' . "\r\n"; 
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

      // Additional headers 
      $headers .= 'To: ' .$to. "\r\n"; 
      $headers .= 'From: ' .$from_email. "\r\n"; 
     // $headers .= 'Bcc: '.$Bcc. "\r\n"; 

      // Send the email 
      mail($to,$subject,$message,$headers); 

enter image description here

+1

电子邮件支持内联css.use内嵌css的邮件设计。 – meet

+0

请注意,您的代码(以及所有发布的答案)很容易遭受标头注入攻击(可能是其他攻击),并且不会正确编码电子邮件内容。不要推出自己的电子邮件代码,你会做错的,因为所有这些都证明了这一点。使用一个像[PHPMailer](https://github.com/PHPMailer/PHPMailer)这样的库来标记这个问题。 – Synchro

+0

您的字符集应该同意 - 您在HTML中设置UTF-8,但在邮件标题中设置ISO-8859-1;你不能同时拥有两个 - 一旦你有非ASCII文本就会中断 – Synchro

回答

3

您需要使用内联样式来获得它的工作原理上的电子邮件

$to = $newsletter_email; 
    $subject = 'Thank you for subscribing'; 
    $message = ' 
     <html> 
     <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
      <title></title> 
     </head> 
     <body> 
      <div id="email-wrap" style='background: #151515;color: #FFF;'> 
      <p>Hi,</p><br> 
      <p>Thank you.</p><br> 
      <p>Thank you,</p> 
      <p>Administration</p> 
      </div> 
     </body> 
     </html> 
      '; 

      $from = "[email protected]"; 
      //$Bcc = "[email protected]"; 

      // To send HTML mail, the Content-type header must be set 
      $headers = 'MIME-Version: 1.0' . "\r\n"; 
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

      // Additional headers 
      $headers .= 'To: ' .$to. "\r\n"; 
      $headers .= 'From: ' .$from. "\r\n"; 
     // $headers .= 'Bcc: '.$Bcc. "\r\n"; 

      // Send the email 
      mail($to,$subject,$message,$headers); 

对于你的问题的第二部分,你可以使用这样的事情

$to = '[email protected]'; 
$email_from = "[email protected]"; 

$full_name = 'Best Buy'; 
$from_mail = $full_name.'<'.$email_from.'>'; 
$from = $from_mail; 
$headers = "" . 
      "Reply-To:" . $from . "\r\n" . 
      "X-Mailer: PHP/" . phpversion(); 
$headers .= 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
$headers .= 'From: ' . $from_email . "\r\n";  
mail($to,$subject,$message,$headers); 
+0

好吧。谢谢!! – Ralph

+0

您是否知道为什么我的名字在发送时作为简讯发布?我认为这是因为我的头。所以我试着做这个'$ headers。='发件人:公司''\ r \ n“;'并且它打破了代码 – Ralph

+0

@Ralph你的意思是它显示为'newsletter'而不是'newsletter @ example.com' –

0

事实上,您发送的电子邮件将以不同的方式打开,通过电子邮件电子邮件客户端将安装在计算机上,该客户端将集成HTML渲染引擎propriéaitre或在线电子邮件客户端,将您的电子邮件放在一个相当特殊的链接中,并取消一些HTML属性。 Campaign Monitor提供的表格可以让您快速了解受损程度,并且我们注意到,根据电子邮件客户端的规定,规则完全不同。
链接:https://www.campaignmonitor.com/css/
但是,有一些工具,例如inline-gulp-css,可以将您的HTML样式变成“在线”。
链接:https://www.npmjs.com/package/gulp-inline-css

-1

TRY

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title></title> 


</head> 
<body> 
       <div style="background:#151515; color: #FFF;"> 
       <p>Hi,</p><br> 
       <p>Thank you.</p><br> 
       <p>Thank you,</p> 
       <p>Administration</p> 
       </div> 
</body> 
</html> 
0

只要设法使它像模板。

下面是一点点的例子可能会有所帮助。

创建my_template.html文件&在该文件中添加以下代码。

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Newsletter</title> 
<style> 
#email-wrap { 
    background: #151515; 
    color: #FFF; 
} 
</style> 
</head> 
<body> 
<p>Hi {{USERNAME}}</p> 
<p>Please click on below link </p><br> 
{{LINK}} 
</body> 
</html> 

现在写发送电子邮件功能,你想要的。并按照以下步骤操作:

1)读取最近创建的HTML文件。

$html = file_get_contents('my_template.html'); 

2)更换变量

$link = "<a href='LINK YOU WANR TO PLACE'>Click Here </a>"; 
$html = str_replace("{{USERNAME}}",$username,$html); 
$html = str_replace("{{LINK}}",$link,$html); 

3)最后发送电子邮件。

  $from = "[email protected]"; 
      $headers .= 'To: ' .$to. "\r\n"; 
      $headers .= 'From: ' .$from. "\r\n"; 
      $headers .= 'Bcc: '.$Bcc. "\r\n"; 

      // Send the email 
      mail($to,$subject,$html,$headers);