PHPMailer电子邮件输入字段隐藏

时间:2014-05-22 01:30:56

标签: php forms gmail phpmailer

有人可以告诉我为什么在邮件从gmail到我的电子邮件habitodigital@hotmail.com后,我没有从表单输入中收到$ email字段?

一切正常,但我看不到电子邮件地址能够接听客户。

<?php

$name = trim($_POST['name']);
$email = $_POST['email'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];

$site_owners_email = 'habitodigital@hotmail.com'; // Replace this with your own email address
$site_owners_name = 'Hábito Digital'; // replace with your name

$error = "";

if (strlen($name) < 2) {
    $error['name'] = "Please enter your name";  
}

if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
    $error['email'] = "Please enter a valid email address"; 
}

if (strlen($comments) < 3) {
    $error['comments'] = "Please leave a comment.";
}

if (!$error) {

    require_once('PHPMailer-master/class.phpmailer.php');
    $mail = new PHPMailer();

    $mail->isSMTP();

    $mail->From = $email;
    $mail->FromName = $name;
    $mail->Subject = $subject;
    $mail->AddAddress($site_owners_email, $site_owners_name);
    $mail->Body = $comments;

    // GMAIL STUFF

    $mail->Mailer = "smtp";
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 587;
    $mail->SMTPSecure = "tls"; 


    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->Username = "ramone@gmail.com"; // SMTP username
    $mail->Password = "***"; // SMTP password

    $mail->Send();

    echo "<div class='alert alert-success'><i class='icon-flag'></i> Congratulations, " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </div>";

} # end if no error
else {

    $response = (isset($error['name'])) ? "<div class='alert alert-error'><i class='icon-warning-sign'></i>  " . $error['name'] . "</div> \n" : null;
    $response .= (isset($error['email'])) ? "<div class='alert alert-error'><i class='icon-warning-sign'></i> " . $error['email'] . "</div> \n" : null;
    $response .= (isset($error['comments'])) ? "<div class='alert alert-error'><i class='icon-warning-sign'></i> " . $error['comments'] . "</div>" : null;

    echo $response;
} # end if there was an error sending

?>

我有这个js文件来处理表单发送消息:

jQuery(function($) {
// These first three lines of code compensate for Javascript being turned on and off. 
// It simply changes the submit input field from a type of "submit" to a type of "button".

var paraTag = $('input#submit').parent('div');
$(paraTag).children('input').remove();
$(paraTag).append('<input type="button" name="submit" id="submit" value="Enviar">');

$('#contact-form input#submit').click(function() {
    $('#contact-form').append('<img src="images/loader.gif" class="loaderIcon" alt="Loading..." />');

    var name = $('input#name').val();
    var email = $('input#email').val();
    var subject = $('input#subject').val();
    var comments = $('textarea#comments').val();

    $.ajax({
        type: 'post',
        url: 'sendEmail.php',
        data: 'name=' + name + '&email=' + email + '&subject=' + subject + '&comments=' + comments,

        success: function(results) {
            $('#contact-form img.loaderIcon').fadeOut(1000);
            $('#response').html(results);
        }
    }); // end ajax
   });
});

1 个答案:

答案 0 :(得分:0)

当您使用gmail发送邮件时,我认为您无法更改from地址; gmail只允许你自己的地址,如果你使用别的东西,他们会用你的地址替换它。

您可以尝试添加回复地址:

 $mail->AddReplyTo($email, $name);

但如果这也不起作用,则必须在邮件正文中发送地址。

作为旁注,您应该在将值发送到服务器时对其进行编码,以确保&=等字符不会破坏您的查询字符串。你可以使用encodeURIComponent(),甚至更容易,使用类似的东西:

data: $('form').serialize(),

如果您要发送的字段位于表单中。