我需要以表格格式将表单数据发送到邮件。为此我正在使用PHPMailer。当我试图显示用户选择的值时,它只显示为数组。有人可以帮我找到这段代码中的错误吗?
以下是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 = 'd11288@gmail.com'; // 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('a11288@gmail.com', 'User');
$mail->addAddress('al@gmail.com', 'Joe User'); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('a11288@gmail.com', 'Information');
$mail->addCC('a11288@gmail.com');
//$mail->addBCC('bcc@arshad.com');
$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> echo implode(" ",$Favorite_Drink)."<br>";</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';
}
}
}
答案 0 :(得分:3)
PHP语句不在here-doc中执行。因此它不会执行echo
或调用implode()
函数。它只是扩展变量,因此当您使用$Favorite_Drink
时,它会扩展为Array
。
您应该先将implode()
的结果放入变量中。
$drinks = implode(" ", $Favorite_Drink);
$mail->Body = <<<EOF
<html><body>
<br>
<table rules="all" style="border-color: #666;" cellpadding="10">
<tr style='background: #eee;'>
<td>Favorite_Drink </td>
<td> $drinks <br></td>
</tr>
</table>
</body>
</html>
EOF;
答案 1 :(得分:-1)
<input type="checkbox" name="Favorite_Drink['checkBox']" value="Vodka">
现在可以使用此索引获取值。将其应用于所有复选框