下面是用php mail()作为html发送电子邮件的php代码:
<?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>!
的粗体和纯文本。我该怎么做?
我读过关于边界的书。这就是我要找的吗?但如果是,怎么用呢?我不明白。
最佳答案
我强烈建议您使用像phpmailer(https://github.com/PHPMailer/PHPMailer)这样的现有库,它可以做您需要的一切,甚至更多。
$mail = new PHPMailer;
$mail->isHTML(true);
$mail->Body = '<b>Html body</b> here.';
$mail->AltBody = 'Normal text here.';