PHP mail()表单没有完成发送电子邮件

时间:2017-05-03 17:02:24

标签: php forms email

<?php

    ini_set("mail.log", "/tmp/mail.log");
    ini_set("mail.add_x_header", TRUE);

    define("TITLE", "Contact Us | MicroUrb");

    include('includes/header.php');

    /*
        NOTE:
        In the form in contact.php, the name text field has the name "name".
        If the user submits the form, the $_POST['name'] variable will be automatically created and will contain the text they typed into the field. The $_POST['email'] variable will contain whatever they typed into the email field. 

        PHP used in this script:

        pre_match()
        - Perform a regular expression match
        - http://ca2.php.net/preg_match

        $_POST
        - An associative array of variables passed to the current script via the HTTP POST method.
        - http://www.php.net/manual/en/reserved.variables.post.php

        trim()
        - Strip whitespace (or other characters) from the beginning and end of a string
        - http://www.php.net/manual/en/function.trim.php

        exit
        - output a message and terminate the current script
        - http://www.php.net/manual/en/function.exit.php

        die()
        - Equivalent to exit
        - http://ca1.php.net/manual/en/function.die.php

        wordwrap()
        - Wraps a string to a given number of characters
        - http://ca1.php.net/manual/en/function.wordwrap.php

        mail()
        - Send mail
        - http://ca1.php.net/manual/en/function.mail.php
    */

?>

    <div id="contact">
        <hr>
        <h1 class="contact">Get in touch with us!</h1>

        <?php 

            // Check for header injections
            function has_header_injection($str) {
                return preg_match("/[\r\n]/", $str);
            }

            if (isset($_POST['contact_submit'])) {
                $name   = trim($_POST['name']);
                $email  = trim($_POST['email']);
                $msg    = $_POST['message'];

                // Check to see if $name or $email have header injections
                if (has_header_injection($name) || has_header_injection($email)) {
                    die(); // If true, kill the script
                }

                if (!$name || !$email || !$msg) {
                    echo '<h4 class="error">All fields required.</h4><a href="contact.php" class="button block">Go back and try again</a>';
                    exit;
                }

                // Add the recipient email to a variable
                $to = "renaissance.scholar2012@gmail.com";

                // Create a subject
                $subject = "$name sent you a message via your contact form";

                // Construct message
                $message = "Name: $name\r\n";
                $message .= "Email: $email\r\n";
                $message .= "Message:\r\n$msg";

                // If the subscribed checkbox was checked
                if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe') {
                    // Add a new line to message variable
                    $message  .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
                }

                $message = wordwrap($message, 72);

                // Set mail headers into a variable
                $headers  = "MIME-Version: 1.0\r\n";
                $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
                $headers .= "From: $name <$email> \r\n";
                $headers .= "X-Priority: 1\r\n"; 
                $headers .= "X-MSMail-Priority: High\r\n";

                // Send the email
                mail($to, $subject, $message, $headers);
        ?>

        <!-- Show success message afte email has been sent -->
        <h5>Thanks for contacting MicroUrb!</h5>
        <p>Please allow 24 hours for a response.</p>
        <p><a href="index.php" class="button block">&laquo; Go to Home Page</a></p>

        <?php } else { ?>

        <form method="post" action="" id="contact-form">
            <label for="name">Your name</label>
            <input type="text" id="name" name="name">

            <label for="email">Your email</label>
            <input type="email" id="email" name="email">

            <label for="message">and your message</label>
            <textarea id="message" name="message"></textarea>

            <input type="checkbox" id="subscribe" name="name" value="Subscribe">
            <label for="">Subscribe to newsletter</label>

            <input type="submit" class="button next" name="contact_submit" value="Send Message">

        </form>

        <?php } ?>

        <hr>

    </div>


<?php include('includes/footer.php'); ?>

我尝试过创建一个简单的邮件表单。表单本身位于我的contact.php页面上。我想测试代码完美提交并发送电子邮件。请帮忙。

我知道有可能通过MAMP或MAMP Pro发送电子邮件不起作用,但我发现文档说你可以使它工作。我按照这个文档:

https://gist.github.com/zulhfreelancer/4663d11e413c76c6393fc135f72a52ce

它对我不起作用。我试图联系作者无济于事,我在这里有点紧张。

这是我到目前为止所做的尝试:

  1. 我已确保启用了错误报告并设置为报告所有错误,我在index.php文件中有该代码:

    <?php
    
    ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 'On');
    
    define("TITLE", "Home | MicroUrb");
    
    include('includes/header.php');
    
    ?>
    
     <div id="philosophy">
       <hr>
           <h1>MicroUrb's Philosophy</h1>
           <p>Here at MicroUrb we guarantee you organically grown produce, because we grew it ourselves and we are local. We're not pompous, we're proud. We're proud of our work, our quality, our environment and our love for fresh local produce and family.</p>
           <p>Thank you, from your local urban family farm.</p>
       <hr>
    </div><!-- philosophy -->
    
    <?php
    
    include('includes/footer.php');
    ?>
    
  2. 如果你看一下contact.php中的代码,你会看到正在调用mail()函数。

  3. 我检查了MAMP Pro日志和Postfix我没有任何东西因为Postfix似乎没有运行(这可能是问题吗?)对于apache日志我有这个错误:

    unknown: warning: /etc/postfix/main.cf, line 696: overriding earlier entry: inet_protocols=all
    sendmail: warning: /etc/postfix/main.cf, line 676: overriding earlier entry: inet_protocols=ipv4
    sendmail: warning: /etc/postfix/main.cf, line 696: overriding earlier entry: inet_protocols=all
    postdrop: warning: /etc/postfix/main.cf, line 696: overriding earlier entry: inet_protocols=all
    
  4. 但是这是由于我在上面分享的链接的第2步引起的,它指示我添加此内容:

    myorigin = live.com
    myhostname = smtp.live.com
    relayhost = smtp.live.com:587
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_sasl_security_options = noanonymous
    smtp_sasl_mechanism_filter = plain
    inet_protocols = ipv4
    smtp_use_tls = yes
    smtp_tls_security_level=encrypt
    tls_random_source=dev:/dev/urandom
    

    上面的第8行与inet_protocols = all冲突,所以我删除了上面的第8行并且问题消失了,但是无法发送邮件的原始问题仍然存在。

    1. 我没有使用错误抑制操作符。
    2. 我可能需要一些帮助来检查mail()是返回true还是false。
    3. 我检查了我尝试发送邮件的电子邮件帐户的垃圾邮件文件夹。
    4. 我相信我提供了所有邮件标题:

      // Set mail headers into a variable
                  $headers  = "MIME-Version: 1.0\r\n";
                  $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
                  $headers .= "From: $name <$email> \r\n";
                  $headers .= "X-Priority: 1\r\n"; 
                  $headers .= "X-MSMail-Priority: High\r\n";
      
    5. 我认为收件人价值是正确的:

       // Send the email
       mail($to, $subject, $message, $headers);
      
    6. 我尝试了多个电子邮件帐户,同样的问题。

    7. 我相信表单方法与代码匹配。
    8. 我的MAMP Pro上的Postfix不能解决问题吗?
    9. 我启用了PHP自定义mail.log。
    10. 我应该只是将其退出并使用不同的邮件程序或在PHP中开发我的联系表单的不同方式,这将不那么令人头疼吗?
    11. 所以我相信我的问题不是其他帖子的重复,因为如上所述,我已经完成了那个SO帖子的故障排除步骤,并没有解决我的问题。

      我只能猜测我的问题要么更具体,要让Postfix在我的MAMP Pro上工作。我没有检查mail()是否正确返回true或false,或者我可能需要使用PHPMailer或其他替代方法。

0 个答案:

没有答案