PHP邮件没有向我的电子邮件地址发送电子邮件

时间:2014-05-30 23:49:21

标签: php html

我一直在四处寻找,似乎无法找到如何使这项工作 - 它只是没有发送电子邮件到我的地址。这是我的HTML表单:

<form id="contactMe" name="contact" method="post" novalidate="novalidate">
    <fieldset>
        <label for="name" id="name">Name<span class="required">*</span></label>
            <input type="text" name="name" id="name" size="30" value="" required="">
            <label for="email" id="email">Email<span class="required">*</span></label>
            <input type="text" name="email" id="email" size="30" value="" required="">
            <label for="phone" id="phone">Phone</label>
            <input type="text" name="phone" id="phone" size="30" value="">
            <label for="Message" id="message">Message<span class="required">*</span></label>
            <textarea name="message" id="message" required=""></textarea>
            <label for="Answer" id="answer">Name the house pet that says “<i>woof</i>“<span class="required">*</span></label>
            <input type="text" name="answer" value="" required=""></br>
            <input id="submit" type="submit" name="submit" value="Send">
        </fieldset>
        <div id="success">
            <span class="green textcenter">
            <p>Your message was sent successfully! I will be in touch as soon as I can.</p>
            </span>
        </div> <!-- closes success div -->
        <div id="error">
            <span><p>Something went wrong, try refreshing and submitting the form again.</p></span>
        </div> <!--close error div-->
    </form>

这是我的PHP保存为mailer.php:

<?php

    $to = "someone@gmail.com";
    $from = $_REQUEST['email'];
    $name = $_REQUEST['name'];
    $headers = "From: $from";
    $subject = "You have a message from your.";

    $fields = array();
    $fields{"name"} = "name";
    $fields{"email"} = "email";
    $fields{"phone"} = "phone";
    $fields{"message"} = "message";

    $body = "Here is what was sent:\n\n"; foreach($fields as $a => $b){   $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

    mail("maytee.kneitz@gmail.com",$subject,$message,"From: $from\n");
    echo 'Mail sent';

?>

这是我第一次使用邮件/联系表单,很抱歉,如果这是一个明显的问题。我似乎无法找到它。任何指导将不胜感激。

我的脚本中有验证(不在此处发布)。

1 个答案:

答案 0 :(得分:1)

您没有定义表单操作。试试这个:

<form id="contactMe" name="contact" method="post" action="mailer.php" novalidate="novalidate">

默认情况下,除非另有说明,否则表单将提交到其当前位置。在您的情况下,将其指向您的邮件脚本所在的位置