为什么html没有显示在我使用php mail()函数发送的电子邮件中?

时间:2012-04-08 03:35:07

标签: php html email

我使用php mail()函数发送了电子邮件确认消息,但它没有处理html标签。知道为什么吗?

   $content="Dear $name, <br /><br />Thank you for registering at NAME. Before we can activate your account one last step must be taken to confirm your email address .";
            $content.="<br /><br />Please note - you must complete this last step. You will only need to visit this URL once to confirm your email address.<br /><br />To complete your registration,";
            $content.=" please visit this URL:<br /><a href='$link1' target='_blank'> $link1 </a><br /><br /><br />";
            $content.="If you are still having problems signing up please contact a member of our support staff at&nbsp;<a href='mailto:email@gmail.com'>email</a> or contact our <a href='$link2'>customer care</a>. <br /><br />";
            $content.="All the best,<br />NAME<br />";
            mail($custArray['email'],"Please Confirm Your Email",$content);

4 个答案:

答案 0 :(得分:3)

您可能需要为html电子邮件添加标头。这是php网站(http://php.net/manual/en/function.mail.php)的例子:

// 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: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

答案 1 :(得分:3)

你需要在标题中指明这是一个html电子邮件,因此它将被解释为html而不是纯文本。

http://php.net/manual/en/function.mail.php

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$content="Dear $name, <br /><br />Thank you for registering at NAME. Before we can activate your account one last step must be taken to confirm your email address .";
$content.="<br /><br />Please note - you must complete this last step. You will only need to visit this URL once to confirm your email address.<br /><br />To complete your registration,";
$content.=" please visit this URL:<br /><a href='$link1' target='_blank'> $link1 </a><br /><br /><br />";
$content.="If you are still having problems signing up please contact a member of our support staff at&nbsp;<a href='mailto:email@gmail.com'>email</a> or contact our <a href='$link2'>customer care</a>. <br /><br />";
$content.="All the best,<br />NAME<br />";
mail($custArray['email'],"Please Confirm Your Email",$content, $headers);

答案 2 :(得分:1)

您可以考虑使用外部库来处理HTML电子邮件。我过去成功使用过http://swiftmailer.org/

答案 3 :(得分:0)

$headers  = "From: $email\r\n" . "Reply-To: $email\r\n" . "CC: $cc\r\n";

$message  = "$logmsg \r\n\n";
$message .= "Name : $name \r\n\n";
$message .= "Email : $email \r\n\n";
$message .= "Phone No : $phone \r\n\n";