2014-06-17 45 views
1

当用户提交表单时,我发送一封包含表单数据的电子邮件。错误:邮件正文空使用PHP邮件程序

这里是我的代码:

$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('[email protected]', '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是空的,帮我解决这个问题?

+0

看到,因为$体与'preg_replace'初始化,我会检查未造成问题 – ElendilTheTall

+0

为什么你试图取代'[]'什么也没有? '$ message'后面还有一个大括号,是否在if语句中?如果是这样,它是什么?如果不删除它。 – Styphon

+0

我们如何知道,你没有显示任何代码对'$ body'变量有任何影响。控制。 – RiggsFolly

回答

1

代码中存在各种错误和遗漏。下面是基于其工作的例子:

<?php 
require 'PHPMailerAutoload.php'; 
$status = 'status'; 
$others = '[email protected]'; 
$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('[email protected]', '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!"; 
} 
+1

非常感谢sr。 – user3703097