PhpMailer发送邮件但邮件无法正常工作

时间:2017-01-19 01:48:00

标签: php html phpmailer

这是我的HTML代码

<form method="post" action="PHPMailer.php" enctype="text/plain">
    <div class="row 50%">
        <div class="6u 12u(mobile)"><input name="name" type="text" placeholder="Name" /></div>
        <div class="6u 12u(mobile)"><input name="email" type="text" placeholder="Email" /></div>
    </div>
    <div class="row 50%">
        <div class="12u"><input name="messageInput" placeholder="Message"></textarea></div>
    </div>
    <div class="row">
        <div class="12u">
            <ul class="actions">
                <li><input type="reset" class="button alt" value="Clear Form" /></li>
                <li><input name="submit" type="submit" class="button" value="Send Message" /></li>
            </ul>
        </div>
    </div>
</form>

这是我的php代码

<?php
if(isset($_POST['submit'])) {

    require_once 'xxxxxxxxxxx\phpMailerAutoload.php';

    $mail = new PHPMailer;
    $mail->SMTPDebug = 2; 
    $mail->isSMTP();                                 
    $mail->Host = 'smtp.gmail.com'; 
    $mail->SMTPAuth = true;    
    $mail->Username = 'xxxxxxxx@gmail.com'; 
    $mail->Password = 'xxxxxxxxxx';                      
    $mail->SMTPSecure = 'tls';    
    $mail->Port = 587;                                    

    $mail->setFrom('xxxx@gmail.com', 'Mailer');
    $mail->addAddress('xxxx@gmail.com', 'Mailer'); 
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    $mail->addAttachment('/var/tmp/file.tar.gz');       
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');  
    $mail->isHTML(true);                                 

    $mail->Subject = 'Here is the subject';
    $mail->Body    = '$message';
    $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';
    }
}
?>

当我删除&#34; if((__ POST [&#39;提交&#39;]&#34;)时,我确实收到了邮件。似乎无法弄清楚Ii&#39我错过了。

我试图从表单中检索邮件并将其作为$ message发送,但是当我添加$ message = $ _POST [&#39; messageInput&#39;];时,它一直告诉我未识别的索引。我很新,很乐意得到任何帮助。谢谢!

修改* 删除了enctype并且它有效!**

1 个答案:

答案 0 :(得分:1)

删除enctype="text/plain">

后,

帖子将有效

标记中enctype的有效值为:

application/x-www-form-urlencoded
multipart/form-data

对于更全面的回答,请包括Barmar的评论:

$mail->Body = '$message';

应该是

$mail->Body = $message;

因为变量不会在单引号内扩展

但你可以使用

$mail->Body =$_POST['messageInput'];

无需为其创建新变量