当用户提交表单时,我发送包含表单数据的电子邮件。
这是我的代码:
$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!";
}
我试过调试代码$ mail-&gt; msgHTML($ body); //没有发送HTML
我从网址寻找帮助:PHPMailer Mailer Error: Message body empty。和PHP Mailer Class issue :Message body empty但他们都没有帮助。
我也尝试过var_dump($ body),我得到了:string(0)&#34;&#34;
有谁可以告诉我为什么$body
是空的并帮助我解决问题?
答案 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!";
}