发送电子邮件时出错:错误无法开机自检/assets/php/form.php

时间:2019-06-26 12:54:29

标签: javascript php forms

我在发送电子邮件时遇到问题。 我收到此错误

rectangles: Array<Array<LatLngLiteral>> = [[ {lat: 44.841225, lng: -0.580036}, {lat: 44.842236, lng: -0.64696}, {lat: 44.805615, lng: -0.63084} ] , [ {lat: 44.819868, lng: -0.582811}, {lat: 44.853709, lng: -0.483573}, {lat: 44.80696, lng: -0.53299}, {lat: 44.80696, lng: -0.629078} ] ];

我的文件结构如下所示 dist -资产 --php --- form.php index.html

在表格中,我给出了这样的路径<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Error</title> </head> <body> <pre>Cannot POST /assets/php/form.php</pre> </body> </html>

我使用服务器gulp connect php,这是我在gulp文件中的任务

<form class="fs-13 text-uppercase" id="form" method="POST" action="assets/php/form.php">

这是我的js文件

// Configure the browserSync task
gulp.task('browserSync', () => {
  $.connectPhp.server({}, () => {
    browserSync.init({
      server: {
        baseDir: PATHS.dist,
        proxy: '127.0.0.1:8000',
      }
    });
  });
});


// Dev task
gulp.task('dev', ['browserSync', 'vendor', 'assets', 'html', 'sass', 
  gulp.watch(PATHS.src + '/assets/php/*.php', ['php', browserSync.reload]);
});

和php

  $('#form').submit(function(event) {
    event.preventDefault();
    var form = $(this);
    var formData = form.serialize();
    $.ajax({
      type: 'POST',
      url: form.attr('action'),
      data: formData
    }).done(function(response) {
        formMessages.text(response);
        $('input[name=name]').val('');
        $('input[name=email]').val('');

      }).fail(function(data) {
          if (data.responseText !== '') {
            $('.form-status').text(data.responseText);
          } else {
            $('.form-status').text('ERROR');
          }
      });
    });

来自

<?php
  if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $name = strip_tags(trim($_POST["name"]));
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);


    if (empty($name) OR empty($email)) {
      http_response_code(400);
      echo "Oops! There was a problem with your submission. Please complete the form and try again.";
      exit;
    }

    $recipient = "email@gamil.com";
    $subject = "Email";
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n";
    $eamil_headers = "From: $name";


    if (mail_utf8($recipient, $name, $subject, $email, $email_content, $eamil_headers)) {
      http_response_code(200);
      echo "Thank You! Your message has been sent.";
    } else {
      http_response_code(500);
      echo "Oops! Something went wrong and we couldn't send your message.";
    }
  } else {
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
  }

  function mail_utf8($recipient, $name, $subject = '(No subject)', $email, $message = '' ) {
    $name = "=?UTF-8?B?".base64_encode($name)."?=";
    $subject = "=?UTF-8?B?".base64_encode($subject)."?=";

    $headers = "From: $name <$email>\r\n".
                "MIME-Version: 1.0" . "\r\n" .
                "Content-type: text/html; charset=UTF-8" . "\r\n";

    return mail($recipient, $subject, $message, $headers);
  }
?>

我不知道问题出在哪里以及为什么它不发送邮件

1 个答案:

答案 0 :(得分:0)

您的HTML和服务器位于不同的端口上。

您有

<form id="form" method="POST" action="assets/php/form.php">

它将转到http://localhost:3000/assets/php/form.php

将此操作更改为:

<form id="form" method="POST" action="http://localhost:8000/assets/php/form.php">

从外观上看,它是您的gulp服务器地址。

但是,在部署时,必须考虑到这一点。