【问题标题】:PHPMailer body message empty errorPHPMailer 正文消息为空错误
【发布时间】:2016-07-05 18:37:02
【问题描述】:

使用教程我粘贴并填写了以下 PHPMailer php 文件并制作了以下 html 表单。但是,发送后出现错误“无法发送邮件。邮件程序错误:邮件正文为空”。请帮忙。

<?php
require 'phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               

$mail->isSMTP();                                      
$mail->Host = 'smtp.gmail.com';  
$mail->SMTPAuth = true;                               
$mail->Username = 'xxx@gmail.com';                 
$mail->Password = 'xxx';                           
$mail->SMTPSecure = 'tls';                            
$mail->Port = 587;                                    

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('xxx@gmail.com', 'Joe User');     
$mail->addReplyTo('info@example.com', 'Information');

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



$mail->Subject = 'Here is the subject';
$mail->From = $email;
$mail->FromName = $name;
$mail->Body    = $message;



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

?>

这是表格

<form name="contactform" method="post" action="email.php">

    <div class="input-text-container clear">
    <input type="text" name="name" id="name" placeholder="Your Name"   
     class="input-text input-text-left">

    <input type="text" name="email" id="email" placeholder="Your E-mail" 
    class="input-text input-text-right">
    </div>

    <textarea type="text" name="message" id="message" placeholder="Your     
    Message" class="textarea"></textarea>

    <button type="submit" value="submit Form" class="submit">SUBMIT</button>

</form>

【问题讨论】:

    标签: phpmailer


    【解决方案1】:

    您的脚本似乎依赖于可用作全局变量的请求参数。但这是一种非常罕见(且不安全)的做法。

    使用$_POST['message'] 代替$message 并且消息正文应包含来自表单的数据。对其他参数执行相同操作。

    【讨论】:

    • 谢谢,我对所有参数都做了,现在我可以看到正文和名称,但是响应的电子邮件没有显示在任何地方,你知道这方面可能是什么修好了吗?
    • 目前您同时使用属性(FromFromName)和设置它们的方法(setFrom())。一个覆盖另一个,所以地址from@example.com 将不再出现在您的电子邮件中。
    • 对我的困惑感到抱歉。那个from@example.com 好像根本不需要,因为我需要知道e-mail,就是表格里写的。如果我删除 setFrom(),它仍然不会显示电子邮件是什么,它已写入 ID 为“email”的输入单元格
    • 无论您如何设置发件人地址,都不要使用提交者的地址作为发件人地址 - 这是伪造的,会导致您无法通过 SPF 检查。将您自己的地址放入 From 并使用 addReplyTo() 添加提交者的地址作为回复。
    • @Huselius 将其附加到邮件正文怎么样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 2016-03-03
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多