Send email via SMTP

OK, I move PHPMailer directory to the root. It was still l returning a 500. I looked in the error log, and even though I was die-ing the script (as per @wake689 's suggestion above) its falling over on the line below it!

The error is

[Tue Nov 23 18:42:51 2021] [error] [client 217.32.33.106:0] PHP Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in /home/sites/###.com/public_html/PHPMailer/src/Exception.php on line 23

The code:

echo "Got here";
die;
/**
 * PHPMailer Exception class.
 * PHP Version 5.5.
 *
 * @see       https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
 *
 * @author    Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
 * @author    Jim Jagielski (jimjag) <jimjag@gmail.com>
 * @author    Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
 * @author    Brent R. Matzelle (original founder)
 * @copyright 2012 - 2020 Marcus Bointon
 * @copyright 2010 - 2012 Jim Jagielski
 * @copyright 2004 - 2009 Andy Prevost
 * @license   http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
 * @note      This program is distributed in the hope that it will be useful - WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 */

namespace PHPMailer\PHPMailer;

I would have thought the code would have stopped executing after the die.

Anyway, I can now target that script (which is good) but now I have this new error (which is bad).

1 Like

Does PHP notice this while parsing the script file before it starts actually executing, and that’s why you are getting this problem? Move the echo and die to after all the namespace instructions and see if it makes any difference.

Ah! Moving the echo/die has worked! Well done!

But now what? :grimacing:

Now you know it’s including the file(s) correctly you can carry on with whatever you were trying to do in the first place.

OK great! Im getting closer with this! (or at least Im not getting namespace errors any more!)

Im now getting “SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0)”

Im using 0365. What would go in the $mail->Host variable?

Thanks!

That’s your mail server mail.domain.com, smtp.domain.com or some such.

Have you tried adding (or changing)

$mail->SMTPDebug = 2;

to see what it going on?

OK, Ive change the host to mail.####.com and smtp.###.com and its now returning “Failed to connect to server: No route to host (113)”

I will contact the IT company that manages the o365 installation and ask them what smtp settings I should use.

For the Windows 10 ‘Mail’ program, I am using smtp.office365.com:587:1 for the outgoing server and outlook.office365.com:993:1 for the incoming.

Does that help?

Hmm. Changing the host to smtp.office365.com and leaving the port at 465 hangs the page.

Changing the port to 587 immediately returns “Failed to connect to server: (0)”.

My relevant bit of code:

 $mail->Host       = 'smtp.office365.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = '###@####.com';                     //SMTP username
    $mail->Password   = '#####';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 587;

Should SMTPSecure be set to enable implicit TLS encryption?

Have you tried adding the debug option?

I did yes @Gandalf . Not sure if it elucidates anything further, but the full error it returns is:

2021-11-24 12:18:28 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Notes I have found with a quick search suggest that SMTPSecure should be set to 'tls' for Office 365.

Oooo. Im getting closer. Changing the SMTPSecure to ‘tls’ returns this:

This suggests that the authentication failed becuase the password for the mailbox is wrong? Im going to ask the IT company to confirm the password.

Does it help to change the port to 587:1 ?

According to this page, the encryption should be STARTTLS.

$mail->SMTPSecure = PHPMailer::STARTTLS;

I think??

I have an idea that’s the same thing:

const ENCRYPTION_STARTTLS = 'tls';

although I’m not completely sure.

const ENCRYPTION_STARTTLS = 'tls';

results in the above screenshotted error

$mail->SMTPSecure = PHPMailer::STARTTLS;

results in a 500 error,

@Archibald : Yes, Im using port 587 now.

I wasn’t suggesting you should add that to your code, I was saying that I think it is already set to that value, so I don’t think there is any difference between setting SMTPSecure either to the string 'tls' or to the constant STARTTLS - my point was that they are the same thing.

Yes in theory this is bad practice as it is picked up as spoofing if the receiving server checks the SPF record as you found.

Rather than having to setup email addresses on the server etc the best way is to use the ‘Reply to’ header which should allow you to set the correct ‘From’ address from a single registered email on your server and then set ‘Reply to’ whatever you want e.g a users email.

Having said that sometimes the receiving server isn’t setup correctly.
A while back I built a form to allow users to send letters to DEFRA as though it was from their email address. I correctly used our site email as the sender and used the ‘Reply to’ setting to set the users email. DEFRAs server wasn’t configured correctly so failed to use the ‘Reply to’ field. Instead i set the users email to the From address (bad practice) and it was all accepted without a problem, when it should have been rejected. But I had no other way of making it work.

Ive just checked with the IT company that manages the o365 install. Theyve confirmed Ive got the correct password for the relevant mailbox.

So, as it stands, the page is generating the error that Ive screenshotted above.

The relevant bit of my code is:

$mail->SMTPDebug = 2;
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.office365.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = '#@###.com';                     //SMTP username
    $mail->Password   = '####';                               //SMTP password
	$mail->SMTPSecure = "tls";
    $mail->Port       = 587;   

So I presume the above is whats causing the Authentication unsuccessful error. Do I need to specify more settings?

Thanks