PHP mail();以纯文本和HTML格式发送电子邮件

时间:2017-02-10 12:34:30

标签: php html email boundary

我有以下 PHP 代码,用于发送电子邮件 PHP mail()为HTML

<?php
    $to = "test@example.com";

    $subject = "This is the subject";

    $headers = "From: Me<test@exapmle.com>\r\n";
    $headers .= "Return-Path: test@exapmle.com\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>!

我该怎么做?

我读到了边界。这就是我要找的东西吗?但如果是这样,如何使用它?我不明白。

1 个答案:

答案 0 :(得分: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.';
相关问题