0

I've been using https://github.com/jonmbake/bootstrap3-contact-form as a quick way to build the form, but modified it slightly to fit the design which we wanted. This uses

getenv('THINGNAME');

inside of .htaccess to set the email, password, hostname, etc.

The form is currently live and is showing as having no errors in the console but when I check the mail account after sending test messages there is no new mail, even in the spam folders.

Here a direct link to the page http://mtmlegal.co.uk/contact.html

Thinking it may be due to that git having a outdated version of PHPmailer that's causing the problem, I updated the necessary files and it didn't seem to make any discernible difference.

I'm aware of a similar issue 1, but that instance is not for phpmailer, so I believe this case is different enough to warrant its own question.

I also quickly checked the logs to see if they were possibly being bounced by the server to find that the server reports deliveries were a success Delivery Tracker but there's still nothing in mailbox mailbox

Here's the sendmail php which pushes to PHPMailer:

    <?php
  /**
   * Sets error header and json error message response.
   *
   * @param  String $messsage error message of response
   * @return void
   */
  function errorResponse ($messsage) {
    header('HTTP/1.1 500 Internal Server Error');
    die(json_encode(array('message' => $messsage)));
  }

  /**
   * Pulls posted values for all fields in $fields_req array.
   * If a required field does not have a value, an error response is given.
   */
  function constructMessageBody () {
    $fields_req =  array("name" => true, "email" => true, "message" => true);
    $message_body = "";
    foreach ($fields_req as $name => $required) {
      $postedValue = $_POST[$name];
      if ($required && empty($postedValue)) {
        errorResponse("$name is empty.");
      } else {
        $message_body .= ucfirst($name) . ":  " . $postedValue . "\n";
      }
    }
    return $message_body;
  }

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

  //do Captcha check, make sure the submitter is not a robot:)...
  $url = 'https://www.google.com/recaptcha/api/siteverify';
  $opts = array('http' =>
    array(
      'method'  => 'POST',
      'header'  => 'Content-type: application/x-www-form-urlencoded',
      'content' => http_build_query(array('secret' => getenv('RECAPTCHA_SECRET_KEY'), 'response' => $_POST["g-recaptcha-response"]))
    )
  );
  $context  = stream_context_create($opts);
  $result = json_decode(file_get_contents($url, false, $context, -1, 40000));

  if (!$result->success) {
    errorResponse('reCAPTCHA checked failed! Error codes: ' . join(', ', $result->{"error-codes"}));
  }
  //attempt to send email
  $messageBody = constructMessageBody();
  require 'php_mailer/PHPMailerAutoload.php';
  $mail = new PHPMailer;
  $mail->CharSet = 'UTF-8';
  $mail->isSMTP();
  $mail->Host = getEnv('FEEDBACK_HOSTNAME');
  if (!getenv('FEEDBACK_SKIP_AUTH')) {
    $mail->SMTPAuth = true;
    $mail->Username = getenv('FEEDBACK_EMAIL');
    $mail->Password = getenv('FEEDBACK_PASSWORD');
  }
  if (getenv('FEEDBACK_ENCRYPTION') == 'TLS') {
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;
  } elseif (getenv('FEEDBACK_ENCRYPTION') == 'SSL') {
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;
  }

  $mail->Sender = getenv('FEEDBACK_EMAIL');
  $mail->setFrom($_POST['email'], $_POST['name']);
  $mail->addAddress(getenv('FEEDBACK_EMAIL'));

  $mail->Subject = $_POST['reason'];
  $mail->Body  = $messageBody;


  //try to send the message
  if($mail->send()) {
    echo json_encode(array('message' => 'Your message was successfully submitted.'));
  } else {
    errorResponse('An unexpected error occured while attempting to send the email: ' . $mail->ErrorInfo);
  }
?>

Apart from changing paths and the log level, I've not changed the files within PHPmailer.

Community
  • 1
  • 1
GeorgeWL
  • 333
  • 2
  • 18
  • Your code? The HTML isn't any use in diagnosing this. – Synchro May 26 '16 at 12:50
  • @Synchro Sorry, what would you require? I'm very rusty on php – GeorgeWL May 26 '16 at 13:09
  • We can't help fix your code, in any language, if you don't post it! – Synchro May 26 '16 at 13:22
  • fixed the post now then @Synchro – GeorgeWL May 26 '16 at 13:27
  • Don't use the submitter's address in from; it's forgery and will fail spf checks. Set SMTPDebug=2 to see the SMTP conversation. – Synchro May 26 '16 at 13:29
  • So how would be best to let the user who received this email what the address of the user from the form would be? – GeorgeWL May 26 '16 at 13:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113043/discussion-between-thegeorgel-and-synchro). – GeorgeWL May 26 '16 at 13:33
  • Put your address in from, theirs in reply-to. That way you're not forging anything. – Synchro May 26 '16 at 13:33
  • @Synchro `$mail->Sender = getenv('FEEDBACK_EMAIL');` `$mail->setFrom(getenv('FEEDBACK_EMAIL'));` `$mail->addAddress(getenv('FEEDBACK_EMAIL'));` `$mail->addReplyTo($_POST['email'], $_POST['name']);` `$mail->Subject = $_POST['reason'];` `$mail->Body = $messageBody;` like so? – GeorgeWL May 26 '16 at 13:43
  • What @Synco is referring to is explained here: [Contact Form Authentication Issues](https://www.unlocktheinbox.com/resources/dmarccontactus/) – Henry May 26 '16 at 13:48
  • Well thank you both, will one of you put in a official answer so I can mark this as solved please? – GeorgeWL May 26 '16 at 13:53

0 Answers0