【问题标题】:Php mail: how to send html?php邮件:如何发送html?
【发布时间】:2011-06-21 07:10:04
【问题描述】:

下面的代码可以正确发送电子邮件,但要发送正文。我需要在邮件正文中显示 html,但我无法做到。网络中的示例不会发送电子邮件:(

如何修复我的代码以发送正文中包含 html 的电子邮件?

非常感谢!

<?php

$to = 'mymail@mail.com';

$subject = 'I need to show html'; 

$from ='example@example.com'; 

$body = '<p style=color:red;>This text should be red</p>';

ini_set("sendmail_from", $from);

$headers = "From: " . $from . "\r\nReply-To: " . $from . "";
  $headers .= "Content-type: text/html\r\n"; 
if (mail($to, $subject, $body, $headers)) {

  echo("<p>Sent</p>");
 } else {
  echo("<p>Error...</p>");
 }

?>

【问题讨论】:

  • +1 for question 我有类似的问题,但这个帖子回答了它(从各个角度)

标签: php html email message sendmail


【解决方案1】:

使用此邮件头:

 $header  = "MIME-Version: 1.0\r\n";
 $header .= "Content-type: text/html; charset: utf8\r\n";

对于内容/正文:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
... ... ...

使用内联 css 命令很重要,建议使用表作为界面。

...

在您的邮件正文中,您不必输入HTML code with head and body

【讨论】:

  • 谢谢你,但标题中有错误,正文中没有 html :(
  • 也将您的线路更改为此。并关闭您打开的所有标签。并使用&lt;body&gt; 标签。 $headers .= "From: " . $from . "\r\nReply-To: " . $from . "";
  • 抱歉,我没听懂:$headers = "MIME-Version: 1.0\r\n"; $headers .= "内容类型:文本/html;字符集:utf8\r\n";发送带有文本的电子邮件阅读什么是伟大的。但是在 from 字段中显示 www-data .. 我该如何解决这个问题?一旦完成,它就会结束。非常感谢
  • @helle 你的意思是使用表格比使用 div 更好?
  • @Sam 在邮件的情况下:是的。
【解决方案2】:

您是否查看过传入邮件的标题?它说

回复:example@example.com内容类型:text/html

只需在此处添加另一个\r\n

Reply-To: " . $from . "\r\n";

【讨论】:

    【解决方案3】:

    我建议您使用网络上提供的众多免费课程之一,而不是自己搞砸。

    我会推荐:PHPMailer

    【讨论】:

      【解决方案4】:

      我发现这很好用!

      Source

      <?php
      //define the receiver of the email
      $to = 'youraddress@example.com';
      //define the subject of the email
      $subject = 'Test HTML email'; 
      //create a boundary string. It must be unique 
      //so we use the MD5 algorithm to generate a random hash
      $random_hash = md5(date('r', time())); 
      //define the headers we want passed. Note that they are separated with \r\n
      $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
      //add boundary string and mime type specification
      $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
      //define the body of the message.
      ob_start(); //Turn on output buffering
      ?>
      --PHP-alt-<?php echo $random_hash; ?>  
      Content-Type: text/plain; charset="iso-8859-1" 
      Content-Transfer-Encoding: 7bit
      
      Hello World!!! 
      This is simple text email message. 
      
      --PHP-alt-<?php echo $random_hash; ?>  
      Content-Type: text/html; charset="iso-8859-1" 
      Content-Transfer-Encoding: 7bit
      
      <h2>Hello World!</h2>
      <p>This is something with <b>HTML</b> formatting.</p> 
      
      --PHP-alt-<?php echo $random_hash; ?>--
      <?
      //copy current buffer contents into $message variable and delete current output buffer
      $message = ob_get_clean();
      //send the email
      $mail_sent = @mail( $to, $subject, $message, $headers );
      //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
      echo $mail_sent ? "Mail sent" : "Mail failed";
      ?>
      

      【讨论】:

      • 也找到了,但在我的情况下没有发送,为什么?
      • 这依赖于邮件命令。该命令存在于大多数 *NIX 系统中。
      【解决方案5】:

      简单的答案:不要这样做。 HTML 电子邮件既邪恶又烦人。至少如果没有包含正确的纯文本版本。正确 = 与 HTML 版本中的信息相同,而不仅仅是关于获取另一个电子邮件客户端的愚蠢评论或指向 html 版本的链接(如果它在网络上可用)。

      如果你真的需要它:http://pear.php.net/package/Mail_Mime

      【讨论】:

      • 如果被滥用,它们可能会很烦人。不过,如果仔细使用,它们是相当不错的。在我看来,最好的做法是同时发送纯文本和纯文本/html 正文,以便用户可以选择他想看到的。
      猜你喜欢
      • 1970-01-01
      • 2012-10-31
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多