如何修复PHP联系人表格不发送电子邮件?

时间:2019-03-15 13:46:53

标签: php contact-form

我正在尝试联系表单,以便我的客户可以向我发送消息,但是当我按send时,一切正常,它表示联系表单已发送,但是我的邮件中什么也没有收到。我使用gmail,我也可以使用自己的域邮件。谁能帮我解决这个问题?谢谢。

我有运行linux ubuntu 18.04的服务器和apache Web服务器。

这是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>

0 个答案:

没有答案