2017-02-10 83 views
2

我为()发送电子邮件与PHP邮件作为HTML以下PHP代码PHP mail();发送电子邮件作为纯文本和HTML一个

<?php 
    $to = "[email protected]"; 

    $subject = "This is the subject"; 

    $headers = "From: Me<[email protected]>\r\n"; 
    $headers .= "Return-Path: [email protected]\r\n"; 
    $headers .= "MIME-Version: 1.0\r\n"; 
    $headers .= "Content-Type: text/html; charset=utf-8\r\n"; 
    $headers .= "X-Priority: 1\r\n"; 

    $message = "This is my <b>message</b>!"; 

    mail($to,$subject,$message,$headers); 
?> 

但我要支持纯文本和HTML。 HTML用于粗体字体和纯文本等内容,用于在非HTML支持电子邮件客户端中显示This is my message!而不是This is my <b>message</b>!

我该怎么做?

我读了关于边界。那是我在找什么?但如果是这样,如何使用它?我不明白。

+0

是的,边界是你所需要的部分。 你可以看到如何做你希望在这里做一个完整的例子和说明:http://stackoverflow.com/questions/10267840/sending-an-email-with-php-plain-text-and -html。 –

+0

@BenShoval我已经找到了这个,但我不明白。 – David

+0

@David:你不明白什么? – Reeno

回答

1

我强烈建议你使用现有的库一样的PHPMailer(https://github.com/PHPMailer/PHPMailer),它可以做你需要的一切,甚至更多。

$mail = new PHPMailer; 
$mail->isHTML(true); 
$mail->Body = '<b>Html body</b> here.'; 
$mail->AltBody = 'Normal text here.'; 
+0

我不喜欢图书馆。 – David