【问题标题】:Error: Message body Empty Using Php Mailer错误:使用 PHP Mailer 的邮件正文为空
【发布时间】:2014-08-07 13:03:11
【问题描述】:

当用户提交表单时,我会发送一封包含表单数据的电子邮件。

这是我的代码:

   $message = '
   <html>
   <head>
   <title>ES Html Report</title>
   </head>
   <body>
    <table>
    <tr>
      <th>Project Name</th>
      <th>TODo</th>
      <th>Priority</th>
      <th>Due on</th>
      <th>Assignee</th>
      <th>Created</th>
      <th>Updated</th>
      <th>Completed</th>
      <th>Assignee Status</th>
     <th>Status</th>
     </tr>

     <tr>
        <td>'.$todolists_store_row['Projects_name'].'</td>
        <td>'.$todolists_store_row['Name'].' </td>
        <td>'.$todolists_store_row['priority'].'</td>
        <td>'.$todolists_store_row['Due_on'].'</td> 
        <td>'.$todolists_store_row['assignee_name'].'</td> 
         <td>'.$todolists_store_row['Created_at'].'</td> 
        <td>'.$todolists_store_row['Modified_at'].'</td> 
        <td>'.$todolists_store_row['Completed_at'].'</td> 
         <td>'.$todolists_store_row['Assignee_status'].'</td> 
         <td>'.$status.'</td> 
    </tr>
  </table>
</body>
  </html>
    ';

    }

   $mail = new PHPMailer();
   $mail->isSendmail();

   $mail->setFrom('a5@gmail.com', 'First Last');

    $mail->addAddress($others, 'John Doe');

      $body= preg_replace('/\[\]/','',$message);
     $mail->IsHTML(true);
     $mail->Body =$body;
    var_dump($body);

   $mail->AltBody = 'This is a plain-text message body';

    //send the message, check for errors
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }

谁能告诉我为什么$body 是空的并帮我解决这个问题?

【问题讨论】:

  • 看到 $body 是用 preg_replace 初始化的,我会检查这不会导致问题
  • 您为什么要尝试将[] 替换为空? $message 之后还有一个右大括号,它在 if 语句中吗?如果是这样,那是什么?如果不删除它。
  • 我们怎么知道,您没有显示任何对名为$body 的变量有任何影响的代码。抓紧。

标签: php phpmailer


【解决方案1】:

您的代码中存在各种错误和遗漏。这是一个基于它的工作示例:

<?php
require 'PHPMailerAutoload.php';
$status = 'status';
$others = 'user@example.com';
$todolists_store_row = array(
    'Projects_name' => 'Projects_name',
    'Name' => 'Name',
    'priority' => 'priority',
    'Due_on' => 'Due_on',
    'assignee_name' => 'assignee_name',
    'Created_at' => 'Created_at',
    'Modified_at' => 'Modified_at',
    'Completed_at' => 'Completed_at',
    'Assignee_status' => 'Assignee_status'
);
$message = '
   <html>
   <head>
   <title>ES Html Report</title>
   </head>
   <body>
    <table>
    <tr>
      <th>Project Name</th>
      <th>TODo</th>
      <th>Priority</th>
      <th>Due on</th>
      <th>Assignee</th>
      <th>Created</th>
      <th>Updated</th>
      <th>Completed</th>
      <th>Assignee Status</th>
     <th>Status</th>
     </tr>

     <tr>
        <td>'.$todolists_store_row['Projects_name'].'</td>
        <td>'.$todolists_store_row['Name'].' </td>
        <td>'.$todolists_store_row['priority'].'</td>
        <td>'.$todolists_store_row['Due_on'].'</td>
        <td>'.$todolists_store_row['assignee_name'].'</td>
         <td>'.$todolists_store_row['Created_at'].'</td>
        <td>'.$todolists_store_row['Modified_at'].'</td>
        <td>'.$todolists_store_row['Completed_at'].'</td>
         <td>'.$todolists_store_row['Assignee_status'].'</td>
         <td>'.$status.'</td>
    </tr>
  </table>
</body>
  </html>
    ';

$mail = new PHPMailer();
$mail->isSendmail();

$mail->Subject = 'Subject';
$mail->setFrom('a5@gmail.com', 'First Last');

$mail->addAddress($others, 'John Doe');

$mail->IsHTML(true);
$mail->Body = preg_replace('/\[\]/','',$message);
$mail->AltBody = 'This is a plain-text message body';

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    相关资源
    最近更新 更多