错误:消息正文使用Php Mailer清空

时间:2014-06-17 13:28:22

标签: php phpmailer

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

这是我的代码:

   $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是空的并帮助我解决问题?

1 个答案:

答案 0 :(得分: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!";
}