PHPmailer creating http error 500

I have looked through the threads here and think I might need to check my server log for more info. I didn’t see any logs under my php or logs folder for todays date when I installed and started using PHPmailer. I am not sure what to do next. I have installed PHPmailer in my public folder and called the class per the install instructions. I am just trying to validate the SMTP connection before going trying to build a contact form. Here is the code that is generating the error:

/**

  • This uses the SMTP class alone to check that a connection can be made to an SMTP server,
  • authenticate, then disconnect
    */

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don’t have access to that
date_default_timezone_set(‘America/Denver’);

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require ‘/home/talktodo/domains/mydomain.com/public_html/PHPMailer/src/Exception.php’;
require ‘/home/talktodo/domains/mydomain.com/public_html/PHPMailer/src/PHPMailer.php’;
require ‘/home/talktodo/domains/mydomain.com/public_html/PHPMailer/src/SMTP.php’;

//Create a new SMTP instance
$smtp = new SMTP;

//Enable connection-level debug output
$smtp->do_debug = SMTP::DEBUG_CONNECTION;

try {
//Connect to an SMTP server
if ($smtp->connect(‘mail.mydomain.com’, 587)) {
//Say hello
if ($smtp->hello(‘mydomain.com’)) { //Put your host name in here
//Authenticate
if ($smtp->authenticate(‘[email protected]’, ‘password’)) {
echo “Connected ok!”;
} else {
throw new Exception('Authentication failed: ’ . $smtp->getLastReply());
}
} else {
throw new Exception('HELO failed: '. $smtp->getLastReply());
}
} else {
throw new Exception(‘Connect failed’);
}
} catch (Exception $e) {
echo 'SMTP error: '. $e->getMessage(), “\n”;
}
//Whatever happened, close the connection.
$smtp->quit(true);

First, please post code inside of code-tags. We can not copy your code this was as just text! Thanks!

Next, where did you get this sample code? There is no ->hello function in PHPmailer.
And, the mess of nested function calls and if’s are hard to follow.

Here is a site that explains how to set this up correctly. Hope it helps! PHPmailer Example

I copied this from the github PHPmailer page and added the use and require pieces per the install instructions.

<?php /** * This uses the SMTP class alone to check that a connection can be made to an SMTP server, * authenticate, then disconnect */ //SMTP needs accurate times, and the PHP time zone MUST be set //This should be done in your php.ini, but this is how to do it if you don't have access to that date_default_timezone_set('America/Denver'); use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require '/home/talktodo/domains/talktodock.com/public_html/PHPMailer/src/Exception.php'; require '/home/talktodo/domains/talktodock.com/public_html/PHPMailer/src/PHPMailer.php'; require '/home/talktodo/domains/talktodock.com/public_html/PHPMailer/src/SMTP.php'; //Create a new SMTP instance $smtp = new SMTP; //Enable connection-level debug output $smtp->do_debug = SMTP::DEBUG_CONNECTION; try { //Connect to an SMTP server if ($smtp->connect('mail.talktodock.com', 587)) { //Say hello if ($smtp->hello('talktodock.com')) { //Put your host name in here //Authenticate if ($smtp->authenticate('[email protected]', 'testing123')) { echo "Connected ok!"; } else { throw new Exception('Authentication failed: ' . $smtp->getLastReply()); } } else { throw new Exception('HELO failed: '. $smtp->getLastReply()); } } else { throw new Exception('Connect failed'); } } catch (Exception $e) { echo 'SMTP error: '. $e->getMessage(), "\n"; } //Whatever happened, close the connection. $smtp->quit(true); ?>
/**
    This uses the SMTP class alone to check that a connection can be made to an SMTP server,
    authenticate, then disconnect
*/

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don’t have access to that
date_default_timezone_set(‘America/Denver’);

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require ‘/home/talktodo/domains/mydomain.com/public_html/PHPMailer/src/Exception.php’;
require ‘/home/talktodo/domains/mydomain.com/public_html/PHPMailer/src/PHPMailer.php’;
require ‘/home/talktodo/domains/mydomain.com/public_html/PHPMailer/src/SMTP.php’;

//Create a new SMTP instance
$smtp = new SMTP;

//Enable connection-level debug output
$smtp->do_debug = SMTP::DEBUG_CONNECTION;

try {
    //Connect to an SMTP server
    if ($smtp->connect(‘mail.mydomain.com’, 587)) {
        //Say hello
        if ($smtp->hello(‘mydomain.com’)) { //Put your host name in here
            //Authenticate
            if ($smtp->authenticate(‘[email protected]’, ‘password’)) {
                echo “Connected ok!”;
            } else {
                throw new Exception('Authentication failed: ' . $smtp->getLastReply());
            }
        } else {
            throw new Exception('HELO failed: '. $smtp->getLastReply());
        }
    } else {
        throw new Exception(‘Connect failed’);
    }
} catch (Exception $e) {
    echo 'SMTP error: '. $e->getMessage(), “\n”;
}
//Whatever happened, close the connection.
$smtp->quit(true);

Can you see the difference in this post than your previous two? We can read this version !

Now, with that said, did you look at the page I sent you???
THERE IS NO HELLO() function in PHPmailer! It does not exist. I searched for this and did find one GitHub example and they created some sort of function named hello(). Since you did not post the example page you are using, we can not help with someone else’s code, just what you post here. Rewrite your code without the extra functions to fix it. Or, rewrite it like the example I posted and it should work.

Thanks Ernie. I will start over. I am not sure why my formatting is getting so screwed up. I do see a lot of difference in what you posted. I believe my hello was from the page that generated my email user on my host but your code and the page you posted seem much more straight forward. I believe I started with the github example but it was way past my bedtime when I turned to help. I had started with an phpmail example from a youtube channel.

I never use Youtube for programming. Just my opinion, it is for videos… LOL
I have used PHPmailer a lot in the past. Currently setting it up for another website, so post here if you can’t solve it all…

As far as the posting code, it is easy. Just use the backward ‘tick’ mark on the upper left of your keyboard.
Type three of them like ``` and press enter. Paste your code there, then press enter and do it again.
This should work. Often the BLOCKQUOTE and PREFORMATTED tags do not work unless in the correct sequence. But, the three back-ticks seem to always work for me. Sorry for the confusion with that!

Good luck!

<?php


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require '/home/talktodo/domains/talktodock.com/public_html/PHPMailer/src/Exception.php';
require '/home/talktodo/domains/talktodock.com/public_html/PHPMailer/src/PHPMailer.php';
require '/home/talktodo/domains/talktodock.com/public_html/PHPMailer/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;

$mail = new PHPMailer();

$mail->isSMTP();
$mail->Host = 'mail.talktodock.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; //paste one generated by Mailtrap
$mail->Password = 'testing123’ //paste one generated by Mailtrap
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('[email protected]', 'me');
$mail->addReplyTo('[email protected]', 'Development at talk');

$mail->Subject = 'Test Email via TalktoDock SMTP using PHPMailer';
$mailContent = "<h1>Send HTML Email using SMTP in PHP</h1>
    <p>This is a test email I’m sending using SMTP mail server with PHPMailer.</p>";
$mail->Body = $mailContent;
if($mail->send()){
    echo 'Message has been sent';
}else{
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>

I didn’t notice the preview to the right. I hate being a newbie. I copied and pasted everything. I have a trouble ticket in to the host provider to verify my path but I am ready to dig a big hole, shoot things, and bury (not literal for all the fact checkers)

The break in the color sequence shows you where you broke it. Look at your post.

i fixed the password line that you brought to attention. I also was informed through the trouble ticket that my include path was wrong as the zip expanded to PHPMail-Master. I made the changes to the includes in the file and tested with the same error 500. I thnen renamed the folder to match the example and put the includes back and still have the same error.

I also added debugging level 4

You need to find where your server is logging errors; otherwise you’re basically guessing. Open another ticket with your host, asking where to find your error logs.

I finally got email sent. Thank you to all who have helped. I duplicated the use line and the errors are logging on the root of the shared host. I do not have access to see these logs.

I started this process to forward questions and issues about my site to my yahoo email account I have had for decades. Yahoo is rejecting it.

Often you can not use a third-party email address such as Yahoo or AOL.
I have got around this by using a free Gmail account. Then, in the Gmail account, have it forwarded to your Yahoo account. This seems work work. AOL and Yahoo, merged as one company now, do not allow redirected email systems nor do they let your account be used in a third party software system.
Hope that helps!

i had a cc still sending to the test mail domain. I removed it as I am now trying to build the actual contact page and I got an email. If I start getting blocked I will utilize one of your suggestions.

Glad you got it all working. Always nice to solve a programming puzzle. Good for you!

Sponsor our Newsletter | Privacy Policy | Terms of Service