PHP mail() - 如何在电子邮件中放置 html 链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15711700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:48:49  来源:igfitidea点击:

PHP mail() - How to put an html link in an email?

htmlphp

提问by jordan

I'm sending an email via PHP's mail() function. In the message I set a link that looks like this:

我正在通过 PHP 的 mail() 函数发送电子邮件。在消息中,我设置了一个链接,如下所示:

$message = "<a href='". $link. "'>" .$title. "</a>\n\n";

However, when the email is received, the email body shows the html code instead of the title as a hyperlink. I'm not very familiar with html emails. How could I achieve what I am trying to get?

但是,当收到电子邮件时,电子邮件正文将 html 代码而不是标题显示为超链接。我对 html 电子邮件不是很熟悉。我怎样才能实现我想要的?

回答by Jean

Try to add an header, so that the mail client doesn't believe it is plain text:

尝试添加标题,以便邮件客户端不相信它是纯文本:

$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

See PHP mail function manual:

PHP邮件功能手册

Example #4 Sending HTML email:

Example #4 发送 HTML 电子邮件

<?php
// multiple recipients
$to  = '[email protected]' . ', '; // note the comma
$to .= '[email protected]';

// 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 <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

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

回答by Adrian

Please see the documentation for PHP mail() at http://php.net/manual/en/function.mail.php

请参阅http://php.net/manual/en/function.mail.php 上的PHP mail() 文档

You need to specify a Content Type of HTML in your function.

您需要在函数中指定 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 <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

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

Although it's generally recommended to avoid using mail() alone.

尽管通常建议避免单独使用 mail() 。

You should consider using PHPMailer, for example.

例如,您应该考虑使用 PHPMailer。

回答by dragonmantank

You have to tell the e-mail client that there is an HTML portion to the e-mail. I'd recommend something like Swiftmailerto do the work instead of doing everything yourself.

您必须告诉电子邮件客户端电子邮件有 HTML 部分。我建议使用Swiftmailer 之类的工具来完成这项工作,而不是自己做所有事情。