简体   繁体   English

错误:邮件正文为空,使用Php Mailer

[英]Error: Message body Empty Using Php Mailer

When a User submits a form I send an e-mail containing the form data. 当用户提交表单时,我会发送一封包含表单数据的电子邮件。

Here is my code: 这是我的代码:

   $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!";
    }

Can anyone tell me why $body is empty and help me fix the issue? 谁能告诉我为什么$body为空并帮助我解决问题?

There are various errors and omissions in your code. 您的代码中存在各种错误和遗漏。 Here's a working example based on it: 这是一个基于它的工作示例:

<?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!";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM