PHPMailer中的邮件正文为空,根本无法弄清楚出了什么问题 [英] Message body empty in PHPMailer can't figure what is wrong at all

查看:74
本文介绍了PHPMailer中的邮件正文为空,根本无法弄清楚出了什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出问题所在,但无法指出.我对PHP还是很陌生,因此它可能是拼写错误或经验丰富的人会立即看到的很小的东西.我的网站上有联系表格,我希望人们能够向我发送电子邮件.这是代码:

I have been trying to figure out what is wrong but just can't point the finger on it. I am still very new to PHP so it may be a misspell or something really small that someone better experienced would see right away.I have a contact form on my website and I want people to be able to send me emails. Here's the code:

<?php
require_once 'libs/phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = 2;
$mail->Mailer = "smtp";
$mail->Host = "smtp.gmail.com";
$mail->Username = "****@****.com";
$mail->Password = "*****";
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->FromName = $name;
$mail->AddAddress("*******.com", "*******");
$mail->Body = $body;

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$company = $_POST['company'];
$body = "La Personne qui nous contacte est: " . $name . " qui travaille pour: " . $company . " le sujet du message etant: " . $subject . " et le message est: " . $message . " nous pouvons la rejoindre par courriel: " . $email . " ou encore par telephone: " . $phone;

$nameErr = $emailErr = $messageErr = "";
$name = $email = $message = "";

if($_SERVER['REQUEST_METHOD'] == "POST"){
    if(empty($_POST['name'])){
        $nameErr = "Veuillez entrer votre nom.";
    }else{
        $name = test_input($_POST['name']);
        if(!preg_match("/^[a-zA-Z ]*$/",$name)){
            $nameErr = "Seulement des lettres et espaces sont permis";
        }
    }
    if(empty($_POST['email'])){
        $emailErr = "Addresse courriel requise!";
    }else{
        $email = test_input($_POST['email']);
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            $emailErr = "L'addresse courriel n'est pas valide!";
        }
    }
    if(empty($_POST['message'])){
        $messageErr = "* Message Obligatoire!";
    }else{
        $message = test_input($_POST['message']);
    }
}
function test_input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

if(isset($_POST['sendmail'])){
if(!$mail->Send()) {
    echo 'Message was not sent.';
    echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent.';
}}

?>

基本上,我已经检查了$ body值,它会回显它应有的值.但是当我点击'sendmail'按钮时,它回显:邮件未发送.Mailer错误:邮件正文为空.就在我添加验证部分之前,一切都运行良好,只是我可以发送一个空表格.如有需要,请填写以下表格:

Basically i have checked the $body value and it echoes as it should. but when i hit the 'sendmail' button it echoes out: Message was not sent.Mailer error: Message body empty. Right before i added the validation part, everything was running fine, except that i could send an empty form. if ever it is needed here is the form:

    <form method="post" style="margin-top: 25px;" role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <div class="form-group">
            <label for="email">Adresse Courriel: <span class="error">* <?php echo $emailErr;?></span></label>
            <input type="email" class="form-control" name="email" id="email">
        </div>
        <div class="form-group">
            <label for="name">Votre Nom: <span class="error">* <?php echo $nameErr;?></span></label>
            <input type="text" class="form-control" name="name" id="name">
        </div>
        <div class="form-group">
            <label for="phone">Votre Telephone:</label>
            <input type="tel" class="form-control" name="phone" id="phone">
        </div>
        <div class="form-group">
            <label for="company">L'entreprise:</label>
            <input type="text" class="form-control" name="company" id="company">
        </div>
        <div class="form-group">
            <label for="subject">Le Sujet du Message:</label>
            <input type="text" class="form-control" name="subject" id="subject">
        </div>
        <div class="form-group">
            <label for="message">Votre Message: <span class="error"><?php echo $messageErr;?></span></label>
            <textarea name="message" class="form-control" id="message" cols="30" rows="5"></textarea>
        </div>
        <input style="margin-bottom: 10px;" id="sendmail" type="submit" class="btn btn-primary" name="sendmail" value="Envoyer!">
    </form>

我感谢任何愿意花几分钟时间检查一下并给我一些建议的人!请注意,我当然不是PHP专业人员,可能有很多noob错误甚至语法错误,我不是要受审判和判刑的地方,我需要比我更好的人的帮助!

I thank anyone who will take a couple minutes checking this out and give me some advice! Please note, I am certainly not a PHP professional, there can be many noob errors and even bad syntax, I am not here to be judged and sentenced, I need help from people better than me!

推荐答案

$mail->Body = $body;

上一行应该是在将内容添加到$body之后.所以您的代码应该看起来像这样,

Above line should be after adding content to $body. So Your code should look something like this,

$body = "La Personne qui nous contacte est: " . $name . " qui travaille pour: " . $company . " le sujet du message etant: " . $subject . " et le message est: " . $message . " nous pouvons la rejoindre par courriel: " . $email . " ou encore par telephone: " . $phone;

$mail->Body = $body;

您正在使用$body变量,然后再向其中添加内容.

You were using $body variable before adding content into it.

这篇关于PHPMailer中的邮件正文为空,根本无法弄清楚出了什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆