2014-06-17 44 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