2016-02-27 54 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);