PHP Build a Basic PHP Website (2018) Adding a Basic Form SMTP with Google

502 Bad Gateway The server returned an invalid or incomplete response.

I get this message when i send the form. What could be going wrong? Dont think is a spelling error since it the suggest.php opens fine.

Spenser Hale
Spenser Hale
18,330 Points

Hey Demain,

Could you provide the code you wrote?

Alena Holligan nobody seems to know the answer could u help me with this one ?

3 Answers

Kristen Gunther
Kristen Gunther
2,208 Points

I have this question too!

//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/src/Exception.php';
require 'vendor/phpmailer/src/SMTP.php';

if($_SERVER["REQUEST_METHOD"] == "POST"){
    $name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
    $email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));
    $details =trim(filter_input(INPUT_POST,"details",FILTER_SANITIZE_SPECIAL_CHARS));

  if ($name == "" OR $email == "" OR $details == ""){
    echo"please fill in the required fields: Name, Email and Details";
    exit;
  }
  if ($_POST["adress"] != ""){
    echo"Bad form input";
    exit;
  }
  if (!PHPMailer::validateAddress($email)){
    echo "Invalid Email Address";
    exit;
  }

    $email_body = "";
    $email_body .= "Name " . $name . "\n";
    $email_body .=  "Email " . $email . "\n";
    $email_body .=  "Details " . $details . "\n";

    $mail = new PHPMailer;
  //Tell PHPMailer to use SMTP
    $mail->isSMTP();
    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 2;
    //Set the hostname of the mail server
    $mail->Host = 'smtp.gmail.com';
    // use
    // $mail->Host = gethostbyname('smtp.gmail.com');
    // if your network does not support SMTP over IPv6
    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
    $mail->Port = 587;
    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'tls';
    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;
    //Username to use for SMTP authentication - use full email address for gmail
    $mail->Username = "demimoremasamor@gmail.com";
    //Password to use for SMTP authentication
    $mail->Password = "svjcnyoxnnoqmfaq";
        //It's important not to use the submitter's address as the from address as it's forgery,
        //which will cause your messages to fail SPF checks.
        //Use an address in your own domain as the from address, put the submitter's address in a reply-to
    $mail->setFrom('demimoremasamor@gmail.com',$name);
    $mail->addReplyTo($email, $name);
    $mail->addAddress('demimoremasamor@gmail.com', 'Demian Arizaleta');
    $mail->Subject = 'Library suggestion from' . $name;
    $mail->Body = $email_body;
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
        exit;
    } 


    header("location:suggest.php?status=thanks");
}


$pageTitle = "Suggest a Media Item";
$section = "suggest";

include("inc/header.php"); ?>

<div class="section page">
  <div class = "wrapper">
    <h1>Suggest a Media Item</h1>
    <?php if (isset($_GET["status"]) && $_GET["status"] == "thanks"){
  echo "<p>Thanks for the email! I&rsquo;ll check out your suggestion shortly!</p>";
    }else { ?>
    <p>If you think there is something I&rsquo;m missing, let me know! Complete the form to send me an email.</p>
    <form method="post" action="suggest.php">
      <table>
        <tr>
          <th><label for="email">Email</label></th>
          <td><input type="text" id="email" name="email"/></td>
        </tr>
        <tr>
          <th><label for="name">Name</label></th>
          <td><input type="text" id="name" name="name"/></td> 
        </tr>
        <tr>
          <th><label for="name">Suggest Item Details</label></th>
          <td><textarea name="details" id="details" ></textarea></td>  
        <tr style="display:none">
          <th><label for="adress">Address</label></th>
          <td><input type="text" id="adress" name="adress"/><p>Please leave this field blank</p></td>
        </tr>
      </table>
      <input type="submit" value="send"/>

    </form>
    <?php } ?>
  </div>

</div>

<?php include("inc/footer.php"); ?>```

i have the opening php tags but it i put it on the same line a the tick and it didnt copy it sorry

Jacob Wick
Jacob Wick
8,912 Points

I'm getting the same response. My code is below...

<?php 

//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/src/Exception.php';
require 'vendor/phpmailer/src/SMTP.php';


if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = trim(filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING));
  $email = trim(filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL));
  $details = trim(filter_input(INPUT_POST, "details", FILTER_SANITIZE_SPECIAL_CHARS));

  if ($name == "" || $email == "" || $details == "") {
    echo "Please fill in required fields: Name, Email, and Details.";
    exit;
  }
  if ($_POST["address"] != "") {
    echo "Bad form input.";
    exit;
  }
  if (!PHPMailer::validateAddress($email)) {
    echo "Invalid Email.";
    exit;
  }

  echo "<pre>";
  $email_body = "";
  $email_body .= "Name " . $name . "\n";
  $email_body .= "Email " . $email . "\n";
  $email_body .= "Details " . $details;

  $mail = new PHPMailer;
  //Tell PHPMailer to Use SMTP
  $mail->isSMTP();
  //Enable SMTP debugging
  // 0 = off (for production use)
  // 1 = client messages
  // 2 = client and server messages
  $mail->SMTPDebug = 2;
  //Set the hostname of the mail server
  $mail->Host = 'smtp.gmail.com';
  // use
  // $mail->Host = gethostbyname('smtp.gmail.com');
  // if your network does not support SMTP over IPv6
  //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
  $mail->Port = 587;
  //Set the encryption system to use - ssl (deprecated) or tls
  $mail->SMTPSecure = 'tls';
  //Whether to use SMTP authentication
  $mail->SMTPAuth = true;
  //Username to use for SMTP authentication - use full email address for gmail
  $mail->Username = "jacob.wick@gmail.com";
  //Password to use for SMTP authentication
  $mail->Password = "###";

  //It's important not to use the submitter's address as the from address as it's forgery,
  //which will cause your messages to fail SPF checks.
  //Use an address in your own domain as the from address, put the submitter's address in a reply-to
  $mail->setFrom('jacob@rainwater.ws', $name);
  $mail->addReplyTo($email, $name);  
  $mail->addAddress('jacob@rainwater.ws', 'Jacob Wick');
  $mail->Subject = 'Library Suggestion From' . $name;
  $mail->Body = $email_body;
  if (!$mail->send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
      exit;
  }  

  header("location:suggest.php?status=thanks");
}
$pageTitle = "Suggest a Media Item";
$section = null;


include('inc/header.php'); ?>

<div class="section page">
  <div class="wrapper">
    <h1>Suggest a Media Item</h1>
    <?php if (isset($_GET["status"]) && $_GET["status"] == "thanks") {
       echo "<p>Thanks for your e-mail! I&rsaquo;ll check out your suggestion shortly.</p>";
      } else { ?>
    <p>If you think there is something I&rsquo;m missing, let me know! Complete the form to send me an email.</p>
    <form method="post" action="suggest.php">
      <table>
        <tr>
          <th><label for="name">Name</label></th>
          <td><input type="text" id="name" name="name"></td>
        </tr>
        <tr>
            <th><label for="email">Email</label></th>
            <td><input type="email" id="email" name="email"></td>
        </tr>
        <tr>
          <th><label for="details">Details</label></th>
          <td><textarea name="details" id="details"></textarea></td>
        </tr> 
        <tr style="display:none">
          <th><label for="address">Address</label></th>
          <td><input type="text" id="address" name="address"></td>
          <p>Please leave this field blank.</p>
        </tr>      
       </table>
      <input type="submit" value="Send">
    </form>
    <?php } ?>
  </div>
</div>