无法使用PHP邮件程序将表单数据发送到邮件

时间:2017-01-22 09:41:41

标签: php arrays phpmailer

我需要以表格格式将表单数据发送到邮件。为此,我使用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> $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 个答案:

答案 0 :(得分:1)

$assign = is_loaded(); $this->_base_classes =& $assign; - 此变量从表单获取数据数组。 $Favorite_Drink = $_POST['Favorite_Drink']; - 如果你想要打印数组作为文本,你应该使用像这样的内容(',',$ Favorite_Drink)