Sending email by PHPMailer using PHP mail not SMTP

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’;
/**

    1. Check if form was submitted.
      /
      if (isset($_POST[‘submit’])) {
      /
      *
      1. 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;
    }

    /**

      1. 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;
        }
        }
        }

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

Any hints are welcome.

Godaddy and other sites sometimes need you to use a FROM email address from the site.
Otherwise, it is considered spam. And, the recipient never gets the email as it sort-of disappears!

Also, some email systems such as AOL, Gmail, Yahoo and others will dump emails if they do not hold to
a strict series of email standards set up by the government and then even more restrictions by them.

Usually, you can fix this by creating one email account on your server and using that as the FROM address.
If you do not really want to have to log into Godaddy’s webmail system to keep up to date with emails, you
can simply forward all mail from your Godaddy’s email account to you personal account. I have done this on
one of my Godaddy servers and it works well. I set the email system to default to [email protected] and
set the info@ email account to forward all mail to my personal account. Works great. Now, on any site on
that domain, I can use [email protected], Info@, Help@, Billing@, ETC@ and all of them come to my
personal account.

Anyway, got off track! I am guessing it is your FROM address. Try that first and if it does not work let us know…

I changed it to this, and no luck… [email protected] is the go daddy email

[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 = ‘

’;
const HTML_ERROR_END = ‘

’;
const HTML_SUCCESS_START = ‘

’;
const HTML_SUCCESS_END = ‘

’;

$receiver = ‘[email protected]’;
$emailfrom = ‘[email protected]’;
$namefrom = ‘Contact Form’; [/code]

Well, I had a lot of trouble with PHPmailer on one Godaddy server. On that server I switched to SwiftMailer which worked for them the first time. On several sites lately, I have just used the standard PHP code to send mail. I found that it depends on the server versions, PHP versions and mailer versions. They all seem to cause problems in certain combinations.

This is what I suggest. Create a small one page test page that just sends out an email to your email address. Test it by going to the site and see if it sends it. Then, adjust the options until it works. Once it gets to your email in box, then copy that version to the larger site. Here is an example of a test page that should work. No buttons or HTML at all. Every time you refresh the page, it will send out an email so be careful doing that.

<?php
 require_once ‘phpmailer/src/PHPmailerAutoload.php';
 $mail = new PHPMailer;
 $mail->setFrom('[email protected]', 'Contact Form'); $mail->addAddress('[email protected]');
 $mail->Subject = 'First PHPMailer Message';
 $mail->Body =  'Just a test e-mail sent through PHPMailer.';
 if ( !$mail->send() ) {
      echo 'Message was not sent.';
     echo 'Mailer error: ' . $mail->ErrorInfo;
 } else {
     echo 'Message has been sent.';
}

This is a sample mostly from the PHPmailer site. It is basically the smallest code possible to send out an email with. Just save it in a simple test file and call it something like testemail.php and run in it a browser.
It should send out an email or display the error. Run it on your live site, of course. If it works, then move at least these parts to your live code. Good luck.

PS: Is there a reason you are not just using PHP’s own sendmail() functions? Just curious…

I tried but got a error.


Also to your PS question I just followed a template, was I suppose to do it another way?

I’m running out of time today, have to leave soon. But, will help for awhile.

What was the error you got on the test page? The 500 error? You can add this to the beginning of the page.
It will turn on all of the error message displays and might tell you more info about the error.
error_reporting(E_ALL);
ini_set(“display_errors”, 1);
… then the require_once for the autoload.php file…

But, my guess is that the REQUER-ONCE is not pointing to the correct location or spelling of the autoload.php file. You should check your folder structure and make it point to the correct PHPmailerAutoload.php file. (It is normally in the phpmailer folder, but, you have it pointing to the src folder.)

To send out emails without using a third-party library, you can do it this way:

<?php
$to = “[email protected]”;
$subject = “Test Email”;
$message = “This is a test email, do not reply!”;
$headers = “From: [email protected]”;
mail($to, $subject, $message, $headers);
?>

Not much to that version.

This is my structure.
tree1


And this is the code for datacheck.php
<?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’;
/**

    1. Check if form was submitted.
      /
      if (isset($_POST[‘submit’])) {
      /
      *
      1. 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;
}
/**

    1. 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 . ’


      // new

      Family name was: ’ . $familyname . ’


      Persons:

      ’;
      foreach ($persons as $cperson) {
      $mailer->Body .= $cperson . ‘
      ’;
      }
      $mailer->Body .= ‘

      Comments were:

      ’;
      // 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;
      }
      }
      }

So, you put this in your public_html folder: (Same as folder as the phpmailer is in!)


And, then you went to a browser and entered your URL which I think is:
http://www.rahmankhaq.com/test.php
What did you get at that point? It should have printed either mail-error or the message-was-sent!
Not a 500 error…

Is the extension of the file PHP… Meaning test.php instead of test.txt or just test…

correct. test.php is what it is

Silly question, but is PHP set up on your server?

Also, put these at the top of the page, right after the <?PHP line so they are the first to execute.

Do you have an htaccess file set up on the main site?
Sometimes this will not allow other files to run.

PS: Are you the groom?

Looking at the main wedding site, it has several errors in it’s code. Of course, I can not see any of the PHP for the site, but, if you right-click on the main page and select VIEW-SOURCE, you will see some errors in RED colors. The errors are two extra 's, one script that is outside of the page at the bottom…

Just FYI

You are in Florida, so you are in the same time zone as Vermont. I can help you more tomorrow, but, I have plans coming up shortly. Running out of time for you today. I do not understand why it will not show the file.
Perhaps you should just try a simpler page first. Like:

<?PHP echo "SOME TEXT"; ?>

And see if it shows that text on your site. I have never seen a php file not work at all like this.
Does your main wedding site have PHP inthe code?

Congratulations! She is cute! (Hope that is okay to say! I know your religion has more rules than mine!)

Ooops! never post passwords on an open forum. Send them in a private message!
It won’t let me blank them out. You should do that now on your post, just click the pencil!

I appreciate it =D… Also I sent my login information, maybe it can help you understand how i did things.

Looking at it now…

Sponsor our Newsletter | Privacy Policy | Terms of Service