Jump to content

Sending email by PHPMailer using PHP mail not SMTP


soupi

Recommended Posts

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.

Link to comment
Share on other sites

I changed it to this, and no luck... wedding@rahmankhaliq.com is created through go daddy

<?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 = 'harahman@syr.edu';
$emailfrom = 'wedding@rahmankhaliq.com';
$namefrom = 'Contact Form'; 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.