【问题标题】:Include html in email在电子邮件中包含 html
【发布时间】:2011-06-22 10:27:21
【问题描述】:

我需要在 PHP 中包含一些 HTML 内容,例如在这样的消息中添加 <a href="#">link</a>

<?php
$to = $themail;
$subject = "Expiration d'une annonce";
$body = "Hey,\n\n";                     
// I need to include a link here in the body like <a href ="http://www.www.com"> Link </a>
mail($to, $subject, $body)
?>

有什么想法吗?

【问题讨论】:

    标签: php html email href


    【解决方案1】:

    这是非常基本的:mail()

    设置正确的标题(来自 php.net)

    // 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";
    
    // Mail it
    mail($to, $subject, $message, $headers);
    

    您的 $message 现在可能包含 HTML。对于复杂的 html/电子邮件,建议查看一些包,例如 PEAR Mailer 类。

    【讨论】:

    • 这太简单了。不设置纯文本替代是提高垃圾邮件分数的好方法。
    • 解析错误:语法错误,第 25 行 /home/mysite/public_html/mysite/test.php 中的意外 T_IF
    • @David 同意了,但是这个问题也太简单了!
    • @Visa 我的代码直接取自 mail 上的 PHP.net 页面 - 好好阅读它和 cmets。
    【解决方案2】:

    php.net/mail有很多例子

    <?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);
    ?>
    

    我还发现这篇文章很有用:

    PHP: Sending Email (Text/HTML/Attachments)

    【讨论】:

    • 这太简单了。不设置纯文本替代是提高垃圾邮件分数的好方法。
    • @David,谢谢,这是一个直接来自 php.net 的示例,作为回答“简单”问题的陈述点!
    【解决方案3】:

    我建议使用 PHPMailer,易于使用,处理所有必要的标头,轻松发送附件,多个收件人等。

    http://phpmailer.worxware.com/index.php?pg=phpmailer

    【讨论】:

    • 这支持多部分电子邮件。 +1 是迄今为止唯一一个指出解决方案的人。
    【解决方案4】:

    我不明白。您需要像这样回显到 html 吗?

    echo '<a href ="http://www.www.com"> Link </a>';
    

    或者你需要这样做:

    $body .= '<a href ="http://www.www.com"> Link </a>';
    

    你到底想做什么?

    如果您尝试通过 mail() 发送 HTML 数据,则需要设置一些标题

        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=utf8' . "\r\n";
        mail($to, $subject, $body, $headers);
    

    更多信息 - 查看http://php.net/manual/en/function.mail.php 示例 4

    【讨论】:

    • 这太简单了。不设置纯文本替代是提高垃圾邮件分数的好方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 2013-04-01
    • 2012-12-24
    • 1970-01-01
    • 2012-03-13
    • 2018-05-05
    • 2012-05-03
    相关资源
    最近更新 更多