2017-01-22 72 views
0

我需要以表格格式將表單數據發送到郵件。爲此,我使用PHPMailer。當我試圖顯示用戶選擇的值時,它只顯示爲數組。有人能幫我找出這段代碼有什麼問題嗎?無法使用PHP郵件發送表單數據到郵件

下面是HTML

<input type="checkbox" name="Favorite_Drink[]" value="Vodka">Vodka 
<input type="checkbox" name="Favorite_Drink[]" value="Vodka">Vodka 
<input type="checkbox" name="Favorite_Drink[]" value="Bear">Bear 

的部分這裏是PHP部分

$Favorite_Drink = $_POST['Favorite_Drink']; 
$mail = new PHPMailer; 

//$mail->SMTPDebug = 3;        // Enable verbose debug output 

$mail->isSMTP();          // Set mailer to use SMTP 
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
$mail->Username = '[email protected]';     // SMTP username 
$mail->Password = 'pass';       // SMTP password 
$mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 587;         // TCP port to connect to 

$mail->setFrom('[email protected]', 'User'); 
$mail->addAddress('[email protected]', 'Joe User');  // Add a recipient 
//$mail->addAddress('[email protected]');    // Name is optional 
$mail->addReplyTo('[email protected]', 'Information'); 
$mail->addCC('[email protected]'); 
//$mail->addBCC('[email protected]'); 
$mail->Subject = 'Here is the subject'; 
//$mail->Body = "Name of patient is "; 
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 
$mail->addAttachment('/var/tmp/file.tar.gz');   // Add attachments 
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name 
$mail->isHTML(true); 
$mail->Subject = ' Test'; 
$mail->Body = <<<EOF 
<html><body> 

    <br> 
    <table rules="all" style="border-color: #666;" cellpadding="10"> 
     <tr style='background: #eee;'> 
      <td>Favorite_Drink </td> 
      <td> $Favorite_Drink</td> 
     </tr> 
</table> 
</body> 
</html> 
EOF; 

      //Altbody is used to display plain text message for those email viewers which are not HTML compatible 

$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent'; 
} 
     } 
} 

回答

1

$Favorite_Drink = $_POST['Favorite_Drink']; - 這個變量中獲取數據的陣列從形式。 <td> $Favorite_Drink</td> - 如果你想打印數組作爲文本,你應該使用像這樣的implode(',',$ Favorite_Drink)

+0

對不起,它不工作! – phpLover

+0

這個答案是正確的 - 你不能直接打印一個數組,你需要處理它的每個元素。我認爲phpLover需要努力一點。 – Synchro