【问题标题】:PHP HTML mail is not working as I wantedPHP HTML 邮件无法正常工作
【发布时间】:2012-07-29 05:24:41
【问题描述】:

我创建了重置密码页面,用于输入他的电子邮件,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();     

【问题讨论】:

  • 其他电子邮件提供商呢?
  • 我个人喜欢Zend_Mail。做我需要做的一切。
  • 我喜欢 phpmailer 的这种东西。它自动处理生成多部分电子邮件。如果您使用纯邮件(),那么您必须自己编写标题和分隔符:P(并且您确实需要执行多部分,因为您不想将 HTML 电子邮件发送给仅设置为获取纯文本的人)。

标签: php html gmail sendmail


【解决方案1】:

你应该像这样构造你的标题:

$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”。

Source (saganwebdesign)

【讨论】:

    【解决方案2】:

    试试这个功能。成功时返回 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 2018-12-25
      • 2011-09-07
      • 1970-01-01
      相关资源
      最近更新 更多