PHP HTML邮件没有按我的意愿工作


PHP HTML mail is not working as I wanted

我创建了重置密码页面,used在其中输入hes电子邮件,PHP向他发回重置密钥。邮件是有效的,但在我的gmail帐户中它是纯文本的。我希望它以HTML格式出现。

$subject = "Your password reset for {$config['site_name']}";
$message = "<html><body>";
$message .= "<p>Someone on" . $config['site_domain'] . "tried to reset your password.</p>";
$message .= "<p>Please click below link, if you want to reset your password.</p>";
$message .= "<p><a href='" . $config['site_url'] . "/forgot_password.php?key=" . $key . "'>" . $config['site_url'] . "/forgot_password.php?key=" . $key . "</a></p>";

$message .= "<p>Thank you,<br>The Admin - " . $config['site_url'] . " </p>";
$message .= "</body></html>";
// Create email headers             
// 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: Mary <mary@example.com>, Kelly <kelly@example.com>' . "'r'n";
$headers .= "From: " . $config['site_name'] . " <noreply@" . $config['site_domain'] . "> 'r'n";
$headers .= "X-Sender: <noreply@" . $config['site_domain'] . "> 'r'n";
$headers .= "Reply-To: <noreply@" . $config['site_domain'] . "> 'r'n";
mail($input['email'],$subject,$message,$headers);
//update pw_reset field into DATABASE
$stmt = $mysqli->prepare("UPDATE members SET pw_reset = ? WHERE email = ?");
$stmt->bind_param("ss", $key, $input['email']);
$stmt->execute();
$stmt->close();     

您应该这样构造您的头:

$headers = 'From: You <you@example.com>' . "'n"; 
$headers .= 'MIME-Version: 1.0' . "'n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n"; 

请注意,From位于MIME和Content之前,只有Content以"''r''n"结尾,其他仅为"''n"。

来源(saganwebdesign)

试试这个函数。成功时返回true

function sendMail($email, $subject, $message)
{
    $supportEmail = 'support@abc.com';
    $from = 'Test Application';
    $msg  = $message;
    $from = str_replace(' ', '-', $from);
    $frm  = $from.' <'.$supportEmail.'>';
    preg_match("<(.*)@(.*'..*)>", $frm, $match);
    ///////////////////Headers/////////////////
    $hdr='';
    $hdr.='MIME-Version: 1.0'."'n";
    $hdr.='content-type: text/html; charset=iso-8859-1'."'n";
    $hdr.="From: {$frm}'n";
    $hdr.="Reply-To: {$frm}'n";
    $hdr.="Message-ID: <".time()."@{$match[2]}>'n";
    $hdr.='X-Mailer: PHP v'.phpversion();
    $x=@mail($email, $subject, $msg, $hdr);
    if($x==0)
    {
        $email=str_replace('@',''@', $email);
        $hdr=str_replace('@',''@',$hdr);
        $x=@mail($email, $subject, $msg, $hdr);
    }
    return $x;
}