Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2018
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHPMailer form not receiving email, but showing success

    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.
    Code:
    <?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 = '[email protected]aliq.com';
    $emailfrom = '[email protected]';
    $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;
        }
    }
    }

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,732
    Thanks
    26
    Thanked 589 Times in 582 Posts
    You would have to check your local mail server, if it successfully sent the email.

    From the PHPMailer docs (emphasis mine):
    It's best to start with the mail() function as that's the simplest method (and PHPmailer's default sending mechanism), but requires that you have a working local mail server.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    Andrť Behrens, NY Times Software Developer

  3. #3
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,315
    Thanks
    10
    Thanked 1,183 Times in 1,173 Posts
    GoDaddy will only allow you to send emails (the sender) to be from your website domain. They do that so nobody can highjack and spoof the email.

    You can't use this as the sender: $emailfrom = '[email protected]'; unless the GoDaddy account is rahmankhalic.com ... which it is not. Apparently you can tell GoDaddy what to specify as the sender? You have to set it up to bypass their default rules.

    Read the rules: https://www.godaddy.com/help/specify...orm-mailer-508

    EDIT:
    I guess it's a bigger mess than that?
    This discussion is from 2016, but maybe it's the same today?
    https://www.godaddy.com/community/Ma...ails/td-p/8270
    Last edited by mlseim; Oct 11th, 2018 at 01:46 AM.

  4. #4
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,732
    Thanks
    26
    Thanked 589 Times in 582 Posts
    Alternately, configure PHPMailer to use SMTP.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    Andrť Behrens, NY Times Software Developer


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •