cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
New

PHPMailer form not receiving email, but showing success

I am using PHPMailer  in a PHP script which is kind of a contact form.   Error reporting of PHP is switched on and the script is running without errors and the send function of PHPMailer is returning true, i. e. success. However the mail doesn't arrive at the recipient. I tested the same script at my own webspace which is hosted by the german all-inkl and it's working fine there. This is the script:

<?php
// add PHP Mailer
use PHPMailer\PHPMailer\PHPMailer;
require_once 'phpmailer/src/Exception.php';
require_once 'phpmailer/src/PHPMailer.php';
const HTML_ERROR_START = '<div class="alert alert-danger" role="alert"><p class="m-0">';
const HTML_ERROR_END = '</p></div>';
const HTML_SUCCESS_START = '<div class="alert alert-success" role="alert"><p class="m-0">';
const HTML_SUCCESS_END = '</p></div>';

$receiver = 'wedding@rahmankhaliq.com';
$emailfrom = 'wedding@rahmankhaliq.com';
$namefrom = 'Contact Form';
/**
 *  1. Check if form was submitted.
 */
if (isset($_POST['submit'])) {
    /**
     *  2. Filter data
     *  - We use the function htmlspecialchars()
     *  - The function trim() is used to remove all empty spaces from start and end of the string
     */
    $choice = htmlspecialchars(trim($_POST['choice-animals']));
    $persons = [];
    for ($i = 1; isset($_POST['dyninput' . $i]); $i++) {
        $persons[] = htmlspecialchars(trim($_POST['dyninput' . $i]));
    }

// New
$familyname = htmlspecialchars(trim($_POST['family-name']));
$comments = htmlspecialchars(trim($_POST['paragraph_text']));

$errors = [];
$success = false;
$nr = 1;
foreach ($persons as $cperson) {
  if (empty($cperson)) {
    $errors[] = HTML_ERROR_START . 'Please enter a name for person no. ' . $nr . ' !' . HTML_ERROR_END;
    $nr++;
  }
}
if (empty($familyname)) {
    $errors[] = HTML_ERROR_START . 'Please enter a family name!' . HTML_ERROR_END;
}

// for testing only
if (empty($receiver)) {
    $errors[] = HTML_ERROR_START . 'Please enter the email address of the receiver!' . HTML_ERROR_END;
}
/**
 *  4. Check if there were errors, if not send email
 *  - for sending email we use PHPMailer
 */
if (count($errors) === 0) {
    $mailer = new PHPMailer();
    $mailer->CharSet = 'UTF-8'; // Set charset setzen for correct display of special characters
    $mailer->setFrom($emailfrom, $namefrom); // Set email address and name of sender
    $mailer->addAddress($receiver); // Empf채ngeradresse
    $mailer->isHTML(true);
    $mailer->Subject = 'New message'; // Subject
    $mailer->Body = '<h3>New message</h3>
           <h3>Choice was: ' . $choice . '</h3>
           // new
           <h3>Family name was: ' . $familyname . '</h3>
           <h2>Persons:</h2>';
     foreach ($persons as $cperson) {
      $mailer->Body .= $cperson . '<br>';
     }
     $mailer->Body .= '<h2>Comments were:</h2>';
     // new
     $mailer->Body .= $comments;
   /**
     * Check if email was sent successfully
     * if so: Report success
     * if not: Report error(s)
     */
    if (!$mailer->send()) {
        $errors[] = HTML_ERROR_START . 'An errror occured. Please try again in some minutes!' . HTML_ERROR_END;
    } else {
        $success = HTML_SUCCESS_START . 'Your Message was sent successfully!' . HTML_SUCCESS_END;
    }
}
}

I checked several other discussions regarding this issue but didn't find a solution.

2 REPLIES 2
Community Manager
Community Manager

Re: PHPMailer form not receiving email, but showing success

HI @soupi. Welcome to GoDaddy Community! Try changing the subject of the email. "New message" is one that sometimes gets flagged as spam. I'd also avoid things like "test". Try sending after doing that. It should help.

 

JesseW - GoDaddy | Community Manager | 24/7 support available at x.co/247support | Remember to choose a solution and give kudos.
New

Re: PHPMailer form not receiving email, but showing success

I tried that but no luck... any other solutions?

<?php
// add PHP Mailer
use PHPMailer\PHPMailer\PHPMailer;
require_once 'phpmailer/src/Exception.php';
require_once 'phpmailer/src/PHPMailer.php';
const HTML_ERROR_START = '<div class="alert alert-danger" role="alert"><p class="m-0">';
const HTML_ERROR_END = '</p></div>';
const HTML_SUCCESS_START = '<div class="alert alert-success" role="alert"><p class="m-0">';
const HTML_SUCCESS_END = '</p></div>';

$receiver = 'wedding@rahmankhaliq.com';
$emailfrom = 'wedding@rahmankhaliq.com';
$namefrom = 'Contact Form';
/**
 *  1. Check if form was submitted.
 */
if (isset($_POST['submit'])) {
    /**
     *  2. Filter data
     *  - We use the function htmlspecialchars()
     *  - The function trim() is used to remove all empty spaces from start and end of the string
     */
    $choice = htmlspecialchars(trim($_POST['choice-animals']));
    $persons = [];
    for ($i = 1; isset($_POST['dyninput' . $i]); $i++) {
        $persons[] = htmlspecialchars(trim($_POST['dyninput' . $i]));
    }

// New
$familyname = htmlspecialchars(trim($_POST['family-name']));
$comments = htmlspecialchars(trim($_POST['paragraph_text']));

$errors = [];
$success = false;
$nr = 1;
foreach ($persons as $cperson) {
  if (empty($cperson)) {
    $errors[] = HTML_ERROR_START . 'Please enter a name for person no. ' . $nr . ' !' . HTML_ERROR_END;
    $nr++;
  }
}
if (empty($familyname)) {
    $errors[] = HTML_ERROR_START . 'Please enter a family name!' . HTML_ERROR_END;
}

// for testing only
if (empty($receiver)) {
    $errors[] = HTML_ERROR_START . 'Please enter the email address of the receiver!' . HTML_ERROR_END;
}
/**
 *  4. Check if there were errors, if not send email
 *  - for sending email we use PHPMailer
 */
if (count($errors) === 0) {
    $mailer = new PHPMailer();
    $mailer->CharSet = 'UTF-8'; // Set charset setzen for correct display of special characters
    $mailer->setFrom($emailfrom, $namefrom); // Set email address and name of sender
    $mailer->addAddress($receiver); // Empf채ngeradresse
    $mailer->isHTML(true);
    $mailer->Subject = 'RSVP IS HERE!'; // Subject
    $mailer->Body = '<h3>Someone RSVPD</h3>
           <h3>Choice was: ' . $choice . '</h3>
           // new
           <h3>Family name was: ' . $familyname . '</h3>
           <h2>Persons:</h2>';
     foreach ($persons as $cperson) {
      $mailer->Body .= $cperson . '<br>';
     }
     $mailer->Body .= '<h2>Comments were:</h2>';
     // new
     $mailer->Body .= $comments;
   /**
     * Check if email was sent successfully
     * if so: Report success
     * if not: Report error(s)
     */
    if (!$mailer->send()) {
        $errors[] = HTML_ERROR_START . 'An errror occured. Please try again in some minutes!' . HTML_ERROR_END;
    } else {
        $success = HTML_SUCCESS_START . 'Your Message was sent successfully!' . HTML_SUCCESS_END;
    }
}
}