I’m supporting a webmaster at godaddy and we are 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.
<?php
// add PHP Mailer
use PHPMailer\PHPMailer\PHPMailer;
require_once ‘phpmailer/src/Exception.php’;
require_once ‘phpmailer/src/PHPMailer.php’;
const HTML_ERROR_START = ‘
’;
const HTML_ERROR_END = ‘
const HTML_SUCCESS_START = ‘
’;
const HTML_SUCCESS_END = ‘
$receiver = ‘[email protected]’;
$emailfrom = ‘[email protected]’;
$namefrom = ‘Contact Form’;
/**
-
- Check if form was submitted.
/
if (isset($_POST[‘submit’])) {
/*
-
- 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]));
}
$familyname = htmlspecialchars(trim($_POST[‘family-name’]));
$comments = htmlspecialchars(trim($_POST[‘paragraph_text’]));
- The function trim() is used to remove all empty spaces from start and end of the string
$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;
}/**
-
- 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 = ‘New message
Choice was: ’ . $choice . ’
Family name was: ’ . $familyname . ’
Persons:
’;
foreach ($persons as $cperson) {
$mailer->Body .= $cperson . ‘
’;
}
$mailer->Body .= ‘Comments were:
’;
$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;
}
}
}
- for sending email we use PHPMailer
- Check if form was submitted.
I checked several other discussions regarding this issue but didn’t find a solution.
Any hints are welcome.