duan0821 2014-05-26 10:12
浏览 38
已采纳

PHPMailer电子邮件发送,但表单中没有值

I have no idea how much work I've lost because of this... but my form doesn't work correctly.

While I get the emails, they are completely blank. My code follows:

<span class="errmsg"></span>

<label for="contact-name">Name</label>
<input type="text" id="contact-name" name="contact-name" required>

<label for="contact-email">E-mail address</label>
<input type="email" id="contact-email" name="contact-email" required>

<label for="contact-subject">Subject</label>
<input type="text" id="contact-subject" name="contact-subject" required>

<label for="contact-message">Message</label>
<textarea id="contact-message" name="contact-message" required></textarea>

<div class="button" type="submit" id="contact-submit" name="contact-submit">Submit</div>

Rather than go for a classic <form>...</form> construct, I've opted to use an ajax post to send the data to PHP:

$("#contact-submit").click(function() {
    // validateForm() just checks that required fields have values
    if (validateForm()) {
        $.ajax({
            type: 'POST',
            url: '/contact.php',
            data: { name: $('#contact-name').val(),
                    from: $('#contact-email').val(),
                    subject: $('#contact-subject').val(),
                    message: $('#contact-message').val()
            }, // end data
            success: function clearFields() {
                $('#contact-name').val('');
                $('#contact-email').val('');
                $('#contact-subject').val('');
                $('#contact-message').val('');
                $('.errmsg').text('Your email was sent successfully.');
                $('.errmsg').css('color', '#389320');
            } // end success
        }); // end ajax
    }
    else
    {
        var errmsg = "Your email could not be sent.<br />";
        errmsg += "Please ensure that you've filled in all the fields.";
        $(".errmsg").html(errmsg);
        $(".errmsg").css("color", "#ff0000");
    }
}); // end click

This (should) post the form data to the PHP file below which sends off the email:

<?php
    error_reporting(E_ALL);
    require_once("class.phpmailer.php");
    include("class.smtp.php");

    $email = new PHPMailer();

    try
    {
        //
        // Set server details for send
        //
        $email->IsSMTP();
        $email->Host = "mail.this.com";
        $email->Port = 25;
        $email->SMTPAuth = true;
        $email->Username = "info@this.com";
        $email->Password = "p455W0rd";

        //
        // Send mail from the contact form
        //
        $to = "info@this.com";
        $from = $_POST["contact-email"];
        $name = $_POST["contact-name"];
        $subject = "From web: ".$_POST["contact-subject"];
        $message = $_POST["contact-message"];

        $body = "<p>Hi Ortund,</p>";
        $body .= "<p>You have received a new query on your website.</p><p>Please see below:</p>";
        $body .= "<p>";
        $body .= str_replace("
", "<br />", $message);
        $body .= "</p>";
        $body .= "</body></html>";

        $email->SetFrom($from, $name);
        $email->AddReplyTo($from, $name);
        $email->AddAddress($to, $to);
        $email->Subject = $subject;
        $email->Body = $body;
        $email->IsHTML(true);

        $email->Send();

        session_start();
        $_SESSION["mailresult"] = "success";
        http_redirect("http://www.this.com");
    }
    catch (Exception $e)
    {
        $_SESSION["mailresult"] = "failed";
        $_SESSION["mailerror"] = $e->getMessage();
    }

?>

There's a couple of layers of checks to return a message back to the user as to what happened, but none work and this is also a major problem for me...

Right now I'd be happy to get the email form data into the email, though I can't understand why it isn't there...

If anyone can help me out here, I'd really appreciate it.

EDIT

Here's an example of an email I'm getting now:

From web:
Root user:
Sent: Mon 2014/05/26 12:24 PM
To: info@this.com

Hi Ortund,

You have received a new query on your website.

Please see below:

  • 写回答

1条回答 默认 最新

  • douya5331 2014-05-26 10:26
    关注

    data are posted using this names

      data: { name: $('#contact-name').val(),
              from: $('#contact-email').val(),
              subject: $('#contact-subject').val(),
              message: $('#contact-message').val()
            },
    

    but retrieved with different name here

        $from = $_POST["contact-email"];
        $name = $_POST["contact-name"];
        $subject = "From web: ".$_POST["contact-subject"];
        $message = $_POST["contact-message"];
    

    so use like this

            $from = $_POST["from"];
            $name = $_POST["name"];
            $subject = "From web: ".$_POST["subject"];
            $message = $_POST["message"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?
  • ¥50 cocos2d-x lua 在mac上接入lua protobuf?