Email sending issue: Authentication error with Gmail SMTP on hosted server

I’m having trouble with a PHP script that sends emails. It works fine on my local XAMPP setup, but fails on my hosted server. Here’s what I’m seeing:

SMTP -> ERROR: Password not accepted from server: 535 Incorrect authentication data
SMTP -> ERROR: RCPT not accepted from server: 550-Please turn on SMTP Authentication...

I suspect it’s a server configuration issue, but I’m not sure what needs to be adjusted. Can anyone offer some advice?

Here’s a revised version of my code:

function send_email($to, $subject, $message) {
    $mailer = new CustomMailer();
    $mailer->setup('smtp.example.com', 587, true, 'tls');
    $mailer->setCredentials('[email protected]', 'mypassword');
    $mailer->setFrom('[email protected]', 'Sender Name');
    $mailer->addRecipient($to);
    $mailer->setSubject($subject);
    $mailer->setBody($message);
    
    return $mailer->send();
}

Any ideas on what might be causing this? Thanks in advance!

I’ve encountered similar issues when deploying email scripts to hosted servers. One common problem is that many hosts block outgoing SMTP connections on default ports for security reasons. Have you tried using Gmail’s alternative SMTP port 465 with SSL instead of 587 with TLS? Also, ensure you’re using an app-specific password if you have 2-factor authentication enabled on your Gmail account. Another thing to check is if your host allows outgoing SMTP connections at all - some require you to use their own SMTP servers or a third-party email service. It might be worth contacting your hosting provider to confirm their email sending policies and any specific configuration requirements they have.

hey emma, have u tried using app passwords instead of ur regular gmail password? Google’s pretty strict bout security n sometimes blocks regular passwords for apps. go to ur google account settings, make an app password for ur script, n use that instead. might solve ur issue without changin much code. good luck!

I’ve experienced similar email authentication issues before and found that the switch from using less secure app access to OAuth 2.0 was necessary. Initially, I used to rely on enabling less secure apps, but since that method is no longer supported, I migrated to OAuth by setting up credentials in the Google Cloud Console and using a library like PHPMailer that supports this authentication method.

I updated my code accordingly and made sure my server’s PHP version could handle the modern encryption methods required. Ultimately, transitioning to OAuth not only resolved the issues but also enhanced the security of email sending on the hosted server.