为什么php处理程序不发送电子邮件(在Nginx服务器上运行的站点)

时间:2018-06-11 02:26:12

标签: javascript php html nginx centos7

我有一个云托管服务帐户,我创建了一个7美元的虚拟机,我使用Nginx作为我的服务器。我一直在与我在互联网上找到的以下模板联系表单进行摔跤,试图通过客户端/服务器交互和捕获表单数据来构建我的知识。我遇到了错误,但我调试了大部分错误。目前,行为:

  1. 输入托管表格的网站的URI,该表单需要:姓名,电子邮件,消息。
  2. 提交该信息后,将显示一条成功的消息。
  3. 预期成果: 消息发送到设置中配置的电子邮件。

    实际结果: 电子邮件未发送。

    任何方向都值得赞赏,我已经被困了将近两天,而且此时可能会忽略一些显而易见的事情。

    以下是我正在使用的文件的重要部分:

    main.js,main.css,index.html,handler.php(放在这篇文章的html中):

    ...
    

    
    
    $(function() {
      function after_form_submitted(data) {
        if (data.result == 'success') {
          $('form#reused_form').hide();
          $('#success_message').show();
          $('#error_message').hide();
        } else {
          $('#error_message').append('<ul></ul>');
    
          jQuery.each(data.errors, function(key, val) {
            $('#error_message ul').append('<li>' + key + ':' + val + '</li>');
          });
          $('#success_message').hide();
          $('#error_message').show();
    
          //reverse the response on the button
          $('button[type="button"]', $form).each(function() {
            $btn = $(this);
            label = $btn.prop('orig_label');
            if (label) {
              $btn.prop('type', 'submit');
              $btn.text(label);
              $btn.prop('orig_label', '');
            }
          });
    
        } //else
      }
    
      $('#reused_form').submit(function(e) {
        e.preventDefault();
    
        $form = $(this);
        //show some response on the button
        $('button[type="submit"]', $form).each(function() {
          $btn = $(this);
          $btn.prop('type', 'button');
          $btn.prop('orig_label', $btn.text());
          $btn.text('Sending ...');
        });
    
    
        $.ajax({
          type: "POST",
          url: 'handler.php',
          data: $form.serialize(),
          success: after_form_submitted,
          dataType: 'json'
        });
    
      });
    });
    &#13;
    #form_container {
      background-image: url("images/paper-1914901_1280.jpg");
      background-size: 100% 100%;
      padding: 15px 15px;
      margin-top: 15px;
    }
    
    body {
      background-color: #F4EAD5;
    }
    
    #form_container input[type='text'],
    #form_container input[type='email'],
    #form_container textarea {
      background: rgba(255, 255, 255, 0.2);
    }
    &#13;
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Complete Contact Form Using HTML, CSS, Javascript And PHP - reusable form</title>
      <!-- Latest compiled and minified CSS -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <!-- Optional theme -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <link rel="stylesheet" href="form.css">
      <script src="form.js"></script>
    </head>
    
    <body>
      <div class="container">
        <div class="row">
          <div class="col-md-6 col-md-offset-3" id="form_container">
            <h2>Contact Us</h2>
            <p> Please send your message below. We will get back to you at the earliest! </p>
            <form role="form" method="post" id="reused_form">
              <div class="row">
                <div class="col-sm-12 form-group">
                  <label for="message"> Message:</label>
                  <textarea class="form-control" type="textarea" id="message" name="message" maxlength="6000" rows="7"></textarea>
                </div>
              </div>
              <div class="row">
                <div class="col-sm-6 form-group">
                  <label for="name"> Your Name:</label>
                  <input type="text" class="form-control" id="name" name="name" required>
                </div>
                <div class="col-sm-6 form-group">
                  <label for="email"> Email:</label>
                  <input type="email" class="form-control" id="email" name="email" required>
                </div>
              </div>
              <div class="row">
                <div class="col-sm-12 form-group">
                  <button type="submit" class="btn btn-lg btn-default pull-right">Send &rarr;</button>
                </div>
              </div>
            </form>
            <div id="success_message" style="width:100%; height:100%; display:none; ">
              <h3>Posted your message successfully!</h3>
            </div>
            <div id="error_message" style="width:100%; height:100%; display:none; ">
              <h3>Error</h3> Sorry there was an error sending your form. </div>
          </div>
        </div>
      </div>
    
    
      <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    /*
    Tested working with PHP5.4 and above (including PHP 7 )
    
     */
    require_once './vendor/autoload.php';
    
    use FormGuide\Handlx\FormHandler;
    
    
    $pp = new FormHandler(); 
    
    $validator = $pp->getValidator();
    $validator->fields(['name','email'])->areRequired()->maxLength(50);
    $validator->field('email')->isEmail();
    $validator->field('message')->maxLength(6000);
    
    
    
    
    $pp->sendEmailTo('someone@gmail.com'); // ← Your email here
    
    echo $pp->process($_POST);
    
    ?>
    
    
    </body>
    
    </html>
    &#13;
    &#13;
    &#13;

1 个答案:

答案 0 :(得分:0)

当你开始发送邮件时,事情变得棘手,因为电子邮件服务器配置很棘手。您的服务器是否设置为可以从shell以普通用户身份发送邮件?即mail -s test someuser@gmail.com(点击输入,键入消息,点击输入几次,点击ctrl + d然后关闭它)。如果没有正确设置邮件,它可能不会离开机器,或者如果没有,则接收服务器可能不接受它。

一旦您知道邮件服务器部分可以工作,那么您可以担心使用PHP发送邮件。像这样的东西Just Works发送纯文本邮件但是邮件服务器必须正确配置。

<?php

    if(isset($_POST['formSubmit'])){

        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
        $headers .= "From: no-reply <no-reply@example.com>\r\n";
        $headers .= "Reply-To: no-reply <no-reply@example.com>\r\n";
        $headers .= "X-Priority: 1\r\n";
        $headers .= "X-MSMail-Priority: High\r\n";
        $headers .= "X-Mailer: php-form2mail";

        $subject="Form submission";

        $message_body=
        "   Hello, this a message generated by a form submission.

        Be sure to wrap your lines at about 70 characters, just so it
        looks nice on peoples mail clients.  If you have a link the mail
        client will almost always make a plain URL clickable - test it

        http://www.google.com


        Here's what the form contained:
        ";

        foreach($_POST as $k=>$v){
            $message_body .= "\n".$k." : $v \n";
        }

        mail($to_address, $subject, $message_body, $headers);

        print("<h2>Thank you</h2>");
        print("<strong>You told us:</strong><br />");
        foreach($_POST as $k=>$v){
            print("\n<br />".$k." : $v <br />\n";
        }
    }
?>