我正在尝试使用以下代码发送电子邮件: 我期望将锚标记呈现为链接,但它只是将锚链接显示为纯文本。如何将其呈现为html而不是纯文本?
<?php
// the message
$msg = '<a href="http://stackoverflow.com/">Click here to visit stackoverflow</a>';
// send email
mail("test@yahoo.com","My subject",$msg);
?>
答案 0 :(得分:4)
您可以在php.net上查询邮件功能文档,http://php.net/manual/en/function.mail.php
您的代码应包含标题:
<?php
$msg = '<a href="http://stackoverflow.com/">Click here to visit stackoverflow</a>';
// 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";
// send email
mail("test@yahoo.com","My subject", $msg, $headers);
从http://php.net/manual/en/function.mail.php获取的更详细的示例:
<?php
// multiple recipients
$to = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// 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 :(得分:-1)
没有定义content type
php不知道它是否是HTML邮件
所以define content-type
位于php代码的顶部,以便php使用HTML格式呈现它。
// 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";
答案 2 :(得分:-1)
我相信这可能会给你一个想法,你会想要什么。我的示例使用标题不仅告诉内容类型,这可能有助于理解邮件不应该在垃圾邮件文件夹中传递。
<?php
require_once "Mail.php";
require_once('Mail/mime.php');
$to = "test@yahoo.com";
$subject = "My Subject";
$header = "from: Some Test Name <test@yahoo.com> \r"."<br >";
$header .= "Content-Type: text/html; charset=ISO-8859-1 \r"."<br >";
$header .= "Return-Path: <test@yahoo.com> \r"."<br >"; //you can change it too
$header .= "X-Priority: 1 (Highest) \r"."<br >";
$header .= "X-MSMail-Priority: High \r"."<br >";
$header .= "Importance: High \r"."<br >";
$header .= "MIME-Version: 1.0 \r"."<br >";
//Making sure that message should deliver and should not go in the Spam folder.
$message = "<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
</head>
<body>
<a href="http://stackoverflow.com/">Click here to visit stackoverflow</a>
</body>
</html>"
$from = "Some Test Name <test@yahoo.com>";
$host = "smtp.yahoo.com"; //I am not sure about yahoo SMTP
$port = "587";
$username1 = "test@yahoo.com";
$password1 = "test1234"; //Password
$crlf = chr(10);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setHTMLBody($message);
$body = $mime->getMessageBody();
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username1, 'password' => $password1));
$mail = $smtp->send($to, $headers, $body );
?>