Как исправить php контактную форму, не отправляя письмо? - PullRequest
0 голосов
/ 15 марта 2019

Я пытаюсь заполнить контактную форму, чтобы мои клиенты могли отправить мне сообщение, но когда я нажимаю отправить, все в порядке, там написано, что контактная форма отправлена, но я ничего не получаю в своей почте.Я использую Gmail, я тоже могу использовать почту своего домена.Может кто-нибудь помочь мне это исправить?Спасибо.

У меня есть сервер, работающий с Linux Ubuntu 18.04, с веб-сервером Apache.

Это файл contact.php.Я вижу все в порядке.

Contact.php:

<?php
/*
 *  CONFIGURE EVERYTHING HERE
 */

// an email address that will be in the From field of the email.
$from = 'Contact form';

// an email address that will receive the email with the output of the form
$sendTo = 'mymail@gmail.com';

// subject of the email
$subject = 'New message from contact form';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); 

// message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';

// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';

/*
 *  LET'S DO THE SENDING
 */

// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);

try
{

    if(count($_POST) == 0) throw new \Exception('Form is empty');

    $emailText = "You have a new message from your contact form\n=============================\n";

    foreach ($_POST as $key => $value) {
        // If the field exists in the $fields array, include it in the email 
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    // All the neccessary headers for the email.
    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );

    // Send email
    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}


// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
// else just display the message
else {
    echo $responseArray['message'];
}

Этот код находится в index.html, один раздел.Contact.html:

<section class="contact-1">
  <div class="container">
    <div class="row align-items-center">
      <div class="col-lg-6 col-md-12">
        <img class="img-center" src="images/banner/06.png" alt="">
      </div>
      <div class="col-lg-6 col-md-12 md-mt-5">
        <div class="section-title">
          <div class="title-effect title-effect-2">
            <div class="ellipse"></div> <i class="la la-info"></i>
          </div>
          <h2>Свяжитесь с нами</h2>
          <p>Свяжитесь с нами и расскажите, чем мы можем вам помочь. Заполните все поля, и мы скоро свяжемся с вами.</p>
        </div>
        <form id="contact-form" method="post" action="php/contact.php">
          <div class="messages"></div>
          <div class="row">
            <div class="col-md-6">
              <div class="form-group">
                <input id="form_name" type="text" name="name" class="form-control" placeholder="Имя" required="required" data-error="Заполните это поле">
                <div class="help-block with-errors"></div>
              </div>
            </div>
            <div class="col-md-6">
              <div class="form-group">
                <input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Фамилия" required="required" data-error="Заполните это поле">
                <div class="help-block with-errors"></div>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-md-6">
              <div class="form-group">
                <input id="form_email" type="email" name="email" class="form-control" placeholder="Электронная почта" required="required" data-error="Заполните это поле">
                <div class="help-block with-errors"></div>
              </div>
            </div>
            <div class="col-md-6">
              <div class="form-group">
                <input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Телефон" required="required" data-error="Заполните это поле">
                <div class="help-block with-errors"></div>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-md-12">
              <div class="form-group">
                <textarea id="form_message" name="message" class="form-control" placeholder="Сообщение" rows="4" required="required" data-error="Заполните это поле"></textarea>
                <div class="help-block with-errors"></div>
              </div>
            </div>
            <div class="col-md-12 mt-2">
              <button class="btn btn-theme btn-circle" data-text="Send"><span>О</span><span>т</span><span>п</span><span>р</span><span>а</span><span>в</span><span>и</span><span>т</span><span>ь</span>
              </button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</section>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...