2012-07-29 66 views
0

我已经创建了重置密码页,在哪里使用进入hes电子邮件,PHP发送给他一个重置密钥。邮件的作品,但它在我的Gmail帐户中的纯文本。我希望它能用HTML。PHP的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 <[email protected]>, Kelly <[email protected]>' . "\r\n"; 
$headers .= "From: " . $config['site_name'] . " <[email protected]" . $config['site_domain'] . "> \r\n"; 
$headers .= "X-Sender: <[email protected]" . $config['site_domain'] . "> \r\n"; 
$headers .= "Reply-To: <[email protected]" . $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();  
+1

与其他电子邮件供应商有什么关系? – 2012-07-29 05:28:07

+0

我个人喜欢'Zend_Mail'。做我需要做的一切。 – 2012-07-29 05:50:12

+0

我喜欢phpmailer这种事情。它自动处理生成多部分电子邮件消息。如果您使用普通邮件(),那么您必须自己编写标题和分隔符:P(并且您确实需要执行多部分内容,因为您不想将HTML电子邮件发送给仅设置为获取纯文本的人)。 – octern 2012-07-29 06:01:25

回答

1

你应该组织你的标题是这样的:

$headers = 'From: You <[email protected]>' . "\n"; 
$headers .= 'MIME-Version: 1.0' . "\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

注意,从是MIME之前和内容,只有内容与“\ r \ n”结束,其他的都只是“\ N”。

Source(saganwebdesign)

0

试试这个功能。成功返回true

function sendMail($email, $subject, $message) 
{ 
    $supportEmail = '[email protected]'; 
    $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(); 
    [email protected]($email, $subject, $msg, $hdr); 
    if($x==0) 
    { 
     $email=str_replace('@','\@', $email); 
     $hdr=str_replace('@','\@',$hdr); 
     [email protected]($email, $subject, $msg, $hdr); 
    } 
    return $x; 
}