cancel
Showing results for 
Search instead for 
Did you mean: 
Go to solution

Sending email by PHPMailer using PHP mail not SMTP

Hi there,

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. 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 = 'name@domain.tld';
$emailfrom = 'name@Yahoo.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]));
    }
    $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;
    }

    /**
     *  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>
               <h3>Family name was: ' . $familyname . '</h3>
               <h2>Persons:</h2>';
        foreach ($persons as $cperson) {
            $mailer->Body .= $cperson . '<br>';
        }
        $mailer->Body .= '<h2>Comments were:</h2>';
        $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.

Any hints are welcome.

Thanks in advance - Ulrich

3 REPLIES 3
Community Manager
Community Manager

Re: Sending email by PHPMailer using PHP mail not SMTP

Hi @Sempervivum124. Welcome to GoDaddy Community! Since the script isn't giving you errors, it seems likely that sending is occurring. Mail delivery issues can be tricky to troubleshoot, especially if there are no rejection notices. I'd make sure that when you send the message, it is coming from an email address associated with the domain for the website. You'll also want to make sure that domain has an SPF record that allows sending from our email servers (secureserver.net). Doing anything else could cause the message to be rejected after it's sent. Hope that helps. If not, feel free to provide more information. Others in the community may be able to provide you with additional suggestions.

 

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

Re: Sending email by PHPMailer using PHP mail not SMTP

Hi @JesseW.

thanks for this information, it is very useful. We changed  the from address to  an address at the owner's website: name@domain.tld where domain.tld is hosted  by Godaddy. Unfortunately this didn't help.
You wrote [quote]You'll also want to make sure that domain has an SPF record that allows sending from  our email servers (secureserver.net).[/quote]

I found this ressource :

https://www.godaddy.com/help/add-an-spf-record-19218

and will recommend to the webmaster to create such record.

Best regards - Ulrich

Solution

Re: Sending email by PHPMailer using PHP mail not SMTP

After creating the SPF record it  didn't work yet. We found out that the email was rejected as  spam. This was fixed by changing the subject.  Everything is fine now. Thanks for the support!