【问题标题】:how to send html email using php如何使用 php 发送 html 电子邮件
【发布时间】:2014-08-19 05:29:14
【问题描述】:

我想用

//ALL HTML MUST BE LEFT ALLIGNED.
$php_var="variable value";
$body = <<<EmailBody
<html>
<body>
$php_var
</body>
</html>
EmailBody; //EmailBody will not show in Email.
$headers = 'From: info@mydomain.com' . "\r\n" .
    'Reply-To: info@mydomain.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
$subject="Test HTML Email";
$body="test email from mydomain";
$to="aminulsumon@gmail.com";
mail($to,$subject,$body,$headers); //$header type should be html

非常感谢任何帮助。

【问题讨论】:

  • EmailBody; 之前和之后不应有任何内容 - IF - EmailBody; //EmailBody will not show in Email. 是您实际代码的一部分,删除 //EmailBody will not show in Email. 包括结尾之间的空格EmailBody;// - 阅读 heredoc 上的文档 php.net/manual/en/…
  • 您必须使用 'content-type: text/html' 在电子邮件中发送 html
  • 如果 EmailBody 前后都没有,那么我应该在哪里编写代码来发送电子邮件?请您粘贴完整的代码作为一个可以正常工作的示例。
  • 意思是EmailBody;(结束标识符),就是该行应该有的所有内容,之后没有其他内容;你有//EmailBody will not show in Email.。使用error reporting,会抛出一个错误,例如Unexpected end of file 或类似的错误。
  • 我猜我的解释是深蹲。我教,我只是不“丢弃”代码。

标签: php email html-email


【解决方案1】:

EmailBody; 之前和/或之后不应有任何内容;它是结束标识符。

使用这个:

//ALL HTML MUST BE LEFT ALLIGNED.
$php_var="variable value";
$body = <<<EmailBody
<html>
<body>
$php_var
</body>
</html>
EmailBody;
$headers = 'From: info@mydomain.com' . "\r\n" .
    'Reply-To: info@mydomain.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
$subject="Test HTML Email";
$body="test email from mydomain";
$to="aminulsumon@gmail.com";
mail($to,$subject,$body,$headers); //$header type should be html

在结束标识符之后有一些东西(空格、文本等),将导致以下错误:

解析错误:语法错误,第 X 行的...(文件路径)中的文件意外结束

是否设置了错误报告

在你打开&lt;?php标签之前添加这个:

error_reporting(E_ALL);
ini_set('display_errors', 1);

【讨论】:

    【解决方案2】:

    去掉末尾的注释:

    EmailBody; //EmailBody will not show in Email.
    

    所以它只是:

    EmailBody;
    

    另外你定义了两次$body,所以取出来:

    $body="test email from mydomain";
    

    【讨论】:

      【解决方案3】:

      这是一个在电子邮件中发送 html 的基本示例 -

      $to = 'abcd@gmail.com';
      $from = "sender@gmail.com"; // sender
      
      $subject = "Test email";
      
      $message = '<html><body>';
      $message .= "<p>This is The Email Address</p><br><span class='nonLink'>responder@example.com</span>";
      $message .= '<br/><a href="http://www.yourwebsite.com/verify.php?email='.$from.'&hash='.$hash.'">click here to complete your registration</a>';
      $message .= '</body></html>';
      
      $headers  = "MIME-Version: 1.0\r\n";
      $headers .= "Content-type: text/html; charset: utf8\r\n";
      // Additional headers
      $headers .= "From: <$from>" . "\r\n";
      
      // Mail it
      if (mail($to, $subject, $message, $headers)){
          echo "email sent successfully";
      }
      

      【讨论】:

      • OP 声明 - “我想使用 &lt;&lt;&lt; 在 php 中发送 html 电子邮件” - 那是 heredoc 语法;你错过了重点。
      猜你喜欢
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 2015-02-23
      • 2011-03-04
      相关资源
      最近更新 更多