PHPMailer form not working on hostgator

the mail() function

Moderators: egami, macek, gesf

Post Reply
consort
New php-forum User
New php-forum User
Posts: 1
Joined: Wed Jan 31, 2018 5:21 am

Wed Jan 31, 2018 5:28 am

I get the following error when I submit the form:
Invalid address: (to):

Fatal error: Uncaught exception 'PHPMailer\PHPMailer\Exception' with message 'Invalid address: (to): ' in /home1/consort/public_html/vendor/phpmailer/phpmailer/src/PHPMailer.php:972 Stack trace: #0 /home1/consort/public_html/vendor/phpmailer/phpmailer/src/PHPMailer.php(902): PHPMailer\PHPMailer\PHPMailer->addOrEnqueueAnAddress('to', NULL, '') #1 /home1/consort/public_html/reservation_form.php(22): PHPMailer\PHPMailer\PHPMailer->addAddress(NULL) #2 /home1/consort/public_html/reservation_form.php(133): smtpmailer(NULL, 'from@mail.com', 'Reservation For...', 'New Reservation', NULL) #3 {main} thrown in /home1/consort/public_html/vendor/phpmailer/phpmailer/src/PHPMailer.php on line 972
Here is my form code in reservation.php:

Code: Select all

<form class="reserve2" name="contactform" method="post" action="reservation_form.php">
    <table >
      <tr>
        <td>
          <input  type="text" name="first_name" maxlength="50" size="30" placeholder="First Name">
        </td>
      </tr>
      <tr>
        <td>
          <input  type="text" name="last_name" maxlength="50" size="30" placeholder="Last Name">
        </td>
      </tr>
      <tr>
        <td>
          <input  type="email" name="email" maxlength="80" size="30" placeholder="Email Address">
        </td>
      </tr>
      <tr>
        <td>
          <input  type="text" name="telephone" maxlength="30" size="30" placeholder="Phone Number">
        </td>
      </tr>
      <tr>
        <td>
          <input type="date" name="arrival" maxlength="10" size="30" placeholder="Arrival Date"></input>
        </td>
      </tr>
      <tr>
        <td>
          <input type="date" name="departure" maxlength="10" size="30" placeholder="Departure Date"></input>
        </td>
      </tr>
      <tr>
        <td>
          <input  type="number" name="adults" maxlength="12" size="30" value="1" placeholder="No. of Adults">
        </td>
      </tr>
      <tr>
        <td>
          <input  type="number" name="children" maxlength="12" size="30" value="0" placeholder="No. of Children">
        </td>
      </tr>
      <tr>
        <td>
          <input  type="number" name="rooms" maxlength="12" size="30" value="1" placeholder="No. of Rooms">
        </td>
      </tr>
      <tr>
        <td>
          <input  type="text" list="suites" name="suite_type" maxlength="30" size="30" placeholder="Room Type">
          <datalist id="suites">
            <option value="Standard Room">
            <option value="Standard Twin Room">
            <option value="Grand Luxe Suite">
            <option value="Penthouse Suite">
          </datalist>
        </td>
      </tr>
      <tr>
      <tr>
        <td colspan="2" style="text-align:center">
          <input type="submit" value="Submit" class="submit">
        </td>
      </tr>
    </table>
  </form>
Here is my php code for reservation_form.php:

Code: Select all

<?php

  require_once('vendor/autoload.php');

  define('GUSER', 'consortsuites@gmail.com'); // GMail username
  define('GPWD', 'consortsuites01'); // GMail password

  function smtpmailer($to, $from, $from_name, $subject, $body) {
	global $error;
	$mail = new PHPMailer\PHPMailer\PHPMailer(true);  // create a new object
	$mail->IsSMTP(); // enable SMTP
	$mail->SMTPDebug = 1;  // debugging: 1 = errors and messages, 2 = messages only
	$mail->SMTPAuth = true;  // authentication enabled
	$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
	$mail->Host = 'smtp.gmail.com';
	$mail->Port = 587;
	$mail->Username = GUSER;
	$mail->Password = GPWD;
	$mail->SetFrom($from, $from_name);
	$mail->Subject = $subject;
	$mail->Body = $body;
	$mail->AddAddress($to);
	if(!$mail->Send()) {
		$error = 'Mail error: '.$mail->ErrorInfo;
		return false;
	} else {
		$error = 'Message sent!';
		return true;
	}
  }

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

      $email_to = "consortsuites@gmail.com";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }


    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['arrival']) ||
        !isset($_POST['departure']) ||
        !isset($_POST['adults']) ||
        !isset($_POST['children']) ||
        !isset($_POST['rooms']) ||
        !isset($_POST['suite_type'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');
    }



    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $arrival = $_POST['arrival']; // required
    $departure = $_POST['departure'];
    $adults = $_POST['adults'];
    $children = $_POST['children'];
    $rooms = $_POST['rooms'];
    $suite_type = $_POST['suite_type'];

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }

  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }

  /*if(!preg_match($string_exp,$arrival)) {
    $error_message .= 'The Arrival date you entered does not appear to be valid.<br />';
  }

  if(!preg_match($string_exp,$departure)) {
    $error_message .= 'The Departure date you entered does not appear to be valid.<br />';
  }

  if(!preg_match($string_exp,$rooms)) {
    $error_message .= 'The number of rooms you entered does not appear to be valid.<br />';
  }*/

  if(!preg_match($string_exp,$suite_type)) {
    $error_message .= 'The Suite type you entered does not appear to be valid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }

    $body = "Reservation details below:.\n\n";


    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }



    $body .= "First Name: ".clean_string($first_name)."\n"."\n";
    $body .= "Last Name: ".clean_string($last_name)."\n"."\n";
    $body .= "Email: ".clean_string($email_from)."\n"."\n";
    $body .= "Telephone: ".clean_string($telephone)."\n"."\n";
    $body .= "Arrival: ".clean_string($arrival)."\n"."\n";
    $body .= "Departure: ".clean_string($departure)."\n"."\n";
    $body .= "Adults: ".clean_string($adults)."\n"."\n";
    $body .= "Children: ".clean_string($children)."\n"."\n";
    $body .= "Rooms: ".clean_string($rooms)."\n"."\n";
    $body .= "Suite Type: ".clean_string($suite_type)."\n"."\n";
  }

  //smtpmailer($email_to, 'from@mail.com', 'Reservation Form', 'New Reservation', $body);
  if (smtpmailer($email_to, 'from@mail.com', 'Reservation Form', 'New Reservation', $body)) {

	echo "Thank you for contacting us. We will be in touch with you very soon.";

  }
  if (!empty($error)) echo $error;

 ?>
I believe I've narrowed it down to the "method=post" attribute not working for some reason.

Post Reply