php不从联系表单发送电子邮件

时间:2016-09-17 14:13:39

标签: javascript php jquery ajax email

我正在尝试在php中设置一个简单的电子邮件功能。通过查看解决同一问题的其他问题,我似乎正在做同样的答案。但是,当我尝试发送电子邮件时,它不会发送。

HTML

<form id="contactMeForm">
    <div class="formField">
        <label for="senderName">Name:</label>
        <input type="text" id="senderName" class="textField" placeholder="Your name" />
    </div>
    <div class="formField">
        <label for="senderEmail">Email:</label>
        <input type="text" id="senderEmail" class="textField" placeholder="Your email" />
    </div>
    <div class="formField">
        <label for="contactReason">Contact reason:</label>
        <select id="contactReason">
            <option></option>
            <option>I'd like a quote for a website.</option>
        </select>
    </div>
    <div class="formField" id="message">
        <div id="senderPhone">
            <label for="returnPhone">Contact number:</label>
            <input type="text" id="returnPhone" class="textField" placeholder="Best phone number to reach you at." />
        </div>
        <label for="senderMessage">Message:</label>
        <textarea id="senderMessage"></textarea>
    </div>

JS

$.ajax({
    type: "POST",
    url: "scripts/email.php",
    data: {name: senderName, email: senderEmail, reason: contactReason, phone: returnPhone, message: senderMessage},
    dataType: "json",
    success: function(response){
        contactMeForm.style.display = "none";
        formButtons.style.display = "block";
        confirmMessage.style.display = "block";
        buttons[1].style.display = "none";
        contactMeHeader[0].textContent = "Your message has been sent.";
    }
})

PHP

<?php
    $name = $_POST["name"];
    $email = $_POST["email"];
    $reason = $_POST["reason"];
    $phone = $_POST["phone"];
    $headers = "From: " .$email ."\r\n";
    $message = $_POST["message"];

    mail("robbyt15@gmail.com", $reason, $message, $headers);
    echo json_encode(array($name, $email, $reason, $phone, $message));
?>

的php.ini

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 25

我在Windows机器上运行带有EasyPHP的Apache服务器。我也没有收到任何错误消息。

1 个答案:

答案 0 :(得分:2)

我猜这些字段都没有填充。您需要将name属性添加到每个字段,这就是PHP在$_POST数组中寻找的内容。

<input type="text" id="senderEmail" class="textField" placeholder="Your email" name="email" />