来自表单的变量$ message内容传递未发送

时间:2019-09-21 06:09:57

标签: php phpmailer

$ message中的变量未发送。它仅发送带有表格形式和标题的空白电子邮件,而没有从表格传递变量。请协助

<?php
if(isset($_POST['submit'])){
$subject = $_POST['subject'];
$mess = $_POST['mess'];
$email = $_POST['email'];
$mail = new PHPMailer();
// send from
$mail->addAddress('*****@gmail.com', 'title');
// send to 
$mail->setFrom('*****@gmail.com', 'title');
//subject
$mail->Subject = "Test email!";
$mail->isHTML(true);
$message ='

        <!DOCTYPE html>
<html>
<body>
<table >
    <tr>
        <td width="30%">Topic</td>
        <td> '.$_POST['subject'].'</td>
    </tr>
</table>
</body>
</html>
    ';

$mail->Body = ($message);

if (!$mail->send())
    echo "wrong";
else
    echo "Mail sent";
}
?>

这是发送变量的形式

<form method="post" action="$_SERVER["PHP_SELF"]" enctype="multipart/form-data">
<input class="form-control" name="subject" placeholder="Subject..."><br>
<input class="btn btn-primary" name="submit" type="submit" value="Send Email">
</form>

我的期望是,所有从上面的表单传递的变量都将发送到电子邮件

1 个答案:

答案 0 :(得分:0)

似乎您需要稍稍更改html,因为要在POST数组中显示的任何表单元素都必须具有name属性。如果您希望用户发布其电子邮件和消息,那么您需要为表单中的那些字段提供合适的字段。表单提交到同一页面时,您可以省略表单操作-此外,PHP_SELF是可欺骗的。

<?php

    if( isset( $_POST['submit'], $_POST['subject'], $_POST['mess'], $_POST['email'] ) ){

        $subject = $_POST['subject'];
        $mess = $_POST['mess'];
        $email = $_POST['email'];



        $mail = new PHPMailer();
        $mail->addAddress('*****@gmail.com', 'title');
        $mail->setFrom('*****@gmail.com', 'title');
        $mail->Subject = 'Test email!';
        $mail->isHTML(true);

        $message =sprintf('
            <!DOCTYPE html>
            <html>
                <head>
                    <title>email</title>
                </head>
                <body>
                <table >
                    <tr>
                        <td width='30%'>Topic</td>
                        <td>%s</td>
                    </tr>
                    <tr>
                        <td colspan=2>%s</td>
                    </tr>
                </table>
                </body>
            </html>',
            $subject,
            $mess
        );

        $mail->Body = ($message);

        if( !$mail->send() ) echo 'wrong';
        else echo 'Mail sent';
    }
?>





<form method='post'>
    <input type='text' name='subject' class='form-control' placeholder='Subject...' /><br />
    <input type='text' name='mess' class='form-control' placeholder='Message...' /><br />
    <input type='email' name='email' class='form-control' placeholder='username@example.com' /><br />
    <input class='btn btn-primary' name='submit' type='submit' value='Send Email'>
</form>