Search code examples
phpformsemailphpmailercloudways

phpmailer gives HTTP ERROR 500 when i try to test email on cloudways


I am a newbie in web development. I wrote a php code to send emails from a form and i hosted it in cloudways. I get a HTTP ERROR 500 whenever i hit the submit button.

    <?php

  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    # code...
      $name = trim($_POST["fullname"]);
      $email = trim($_POST["email"]);
      $phone_no = trim($_POST["phone_num"]);
      $partner_type = $_POST["Partner_type"];

     if ($name == "" || $email == "" || $phone_no == " ") {
        echo "please fill the required fields name , email , phone number";
      }

      if ($_POST["adds"] != "") {
        echo "Bad form input";
        exit;
      }

      require 'class.phpmailer.php';

      $mail = new PHPMailer();

      if (!$mail->ValidateAddress($email)) {
        # code...
        echo "Invalid Email Address";
        exit;
      }


      $email_body = "";
      $email_body .= "Name " . $name . "\n";
      $email_body .= "Email " . $email . "\n";
      $email_body  .= "Phone Number " . $phone_no . "\n";
      $email_body .= "Partner Type " . $partner_type . "\n";


      $mail->setFrom($email, $name);
      $mail->addAddress('rescuetl@localhost', 'gbubemi');     // Add a recipient

    /**$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
      $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
      **/

      $mail->isHTML(false);                                  // Set email format to HTML

      $mail->Subject = 'Become a Partner ' . $name ;
      $mail->Body    = $email_body;
      $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

      if(!$mail->send()) {
          echo 'Message could not be sent.';
          echo 'Mailer Error: ' . $mail->ErrorInfo;
          exit;
      } else {
          echo 'Message has been sent';
      }

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

 ?>

please it is really frustrating i need help. Thanks.


Solution

  • First you need to debug the application weather email is sending or not or may be it's stuck in a queue. you can check it by running "mailq" on terminal. Next first try to send simple email on Cloudways like this:

    <?php 
    require_once "vendor/autoload.php"; //PHPMailer Object 
    $mail = new PHPMailer; //From email address and name 
    $mail->From = "[email protected]"; 
    $mail->FromName = "Full Name"; //To address and name 
    $mail->addAddress("[email protected]", "Recepient Name");//Recipient name is optional
    $mail->addAddress("[email protected]"); //Address to which recipient will reply 
    $mail->addReplyTo("[email protected]", "Reply"); //CC and BCC 
    $mail->addCC("[email protected]"); 
    $mail->addBCC("[email protected]"); //Send HTML or Plain Text email 
    $mail->isHTML(true); 
    $mail->Subject = "Subject Text"; 
    $mail->Body = "<i>Mail body in HTML</i>";
    $mail->AltBody = "This is the plain text version of the email content"; 
    if(!$mail->send()) 
    {
    echo "Mailer Error: " . $mail->ErrorInfo; 
    } 
    else { echo "Message has been sent successfully"; 
    }
    if(!$mail->send()) 
    { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
    } 
    else 
    { 
    echo "Message has been sent successfully"; 
    }
    

    If it works and email sent then try sending by applying smtp credentials like this:

    <?php
    require_once "vendor/autoload.php";
    
    $mail = new PHPMailer;
    
    //Enable SMTP debugging.
    
    $mail->SMTPDebug = 3;                           
    
    //Set PHPMailer to use SMTP.
    
    $mail->isSMTP();        
    
    //Set SMTP host name                      
    
    $mail->Host = "smtp.gmail.com";
    
    //Set this to true if SMTP host requires authentication to send email
    
    $mail->SMTPAuth = true;                      
    
    //Provide username and password
    
    $mail->Username = "[email protected]";             
    
    $mail->Password = "super_secret_password";                       
    
    //If SMTP requires TLS encryption then set it
    
    $mail->SMTPSecure = "tls";                       
    
    //Set TCP port to connect to
    
    $mail->Port = 587;                    
    
    $mail->From = "[email protected]";
    
    $mail->FromName = "Full Name";
    
    $mail->addAddress("[email protected]", "Recepient Name");
    
    $mail->isHTML(true);
    
    $mail->Subject = "Subject Text";
    
    $mail->Body = "<i>Mail body in HTML</i>";
    
    $mail->AltBody = "This is the plain text version of the email content";
    
    if(!$mail->send())
    
    {
    
    echo "Mailer Error: " . $mail->ErrorInfo;
    
    }
    
    else
    
    {
    
    echo "Message has been sent successfully";
    
    }
    ?> 
    

    You can also set up Smtp on Cloudways Platform in server settings: SMTP settings for cloudways

    Read the full article here: https://www.cloudways.com/blog/send-emails-in-php-using-phpmailer/