PHPMailer not working in infinityfree but works on localhost

PHPMailer doesn’t work on infinityfree but works in localhost using XAMPP

here it is the error:

Invalid address: (to):
Message could not be sent.Mailer Error: You must provide at least one recipient email address.

This is my code:

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

require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';


    $data = JSON_DECODE(file_get_contents('php://input'));
    $emailToName = $data->name;
    $emailTo = $data->email;
    $body = $data->body;

    $emailFrom = "[email protected]";
    $emailFromName = "MyName";

    $mail = new PHPMailer;
    $mail->isSMTP(); 
    $mail->SMTPDebug = 2; 
    $mail->Host = "smtp.gmail.com"; 
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true;
    $mail->Username = "[email protected]";
    $mail->Password = "xxxxxxxxx";
    $mail->setFrom($emailFrom, $emailFromName);
    $mail->addAddress($emailTo, $emailToName);
    $mail->Subject = 'Test Email';
    $mail->msgHTML("Test Email");

    
    if(!$mail->send()){
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }else{
        $mailToMe->send();
        $data = json_encode(["alert" => "Message sent!"]);

        echo $data;
    }

The passing of data from file_get_contents is successful but it is not working. But in my localhost it is working. I use Gmail and I’m only sending gmail emails.

Hello there,

I think that error already explains it all, you should specify an email address that you would like to send an email to.

I know but this error doesn’t show in my localhost. The data is actually passed I don’t know why it doesn’t in the infinityfree server.

Could you refer to the following posts:

Yep I read all of those posts and none of them is the same error on what I got.

Here is the whole error:

2020-05-11 23:15:50 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP s11sm29488568wms.5 - gsmtp
2020-05-11 23:15:50 CLIENT -> SERVER: EHLO brentjude.ml
2020-05-11 23:15:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [185.27.134.185]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2020-05-11 23:15:50 CLIENT -> SERVER: STARTTLS
2020-05-11 23:15:50 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2020-05-11 23:15:50 CLIENT -> SERVER: EHLO brentjude.ml
2020-05-11 23:15:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [185.27.134.185]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2020-05-11 23:15:50 CLIENT -> SERVER: AUTH LOGIN
2020-05-11 23:15:50 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2020-05-11 23:15:50 CLIENT -> SERVER: [credentials hidden]
2020-05-11 23:15:50 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2020-05-11 23:15:50 CLIENT -> SERVER: [credentials hidden]
2020-05-11 23:15:51 SERVER -> CLIENT: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs534-5.7.14 eyNeEZgY375nHlJBVEeyY_7EPxhQGbttnTR9pfUt53-_19C7Ahzf0c4UlVOOObcFI_PWy534-5.7.14 q72PTEPxvmauAyN1UsqDsTCLLwQ7UURscj7nVgxGgaz9ZR6vyVO5ahAhxENt1S7k>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 s11sm29488568wms.5 - gsmtp
2020-05-11 23:15:51 SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs534-5.7.14 eyNeEZgY375nHlJBVEeyY_7EPxhQGbttnTR9pfUt53-_19C7Ahzf0c4UlVOOObcFI_PWy534-5.7.14 q72PTEPxvmauAyN1UsqDsTCLLwQ7UURscj7nVgxGgaz9ZR6vyVO5ahAhxENt1S7k>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 s11sm29488568wms.5 - gsmtp
SMTP Error: Could not authenticate.
2020-05-11 23:15:51 CLIENT -> SERVER: QUIT
2020-05-11 23:15:52 SERVER -> CLIENT: 221 2.0.0 closing connection s11sm29488568wms.5 - gsmtp
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

Could you try adding this to your code:

$m->addAddress('[email protected]');

As far as what I’m aware that’s used when you want to specify an email recipient.

You might also want to check examples from PHP Mailer:

You should do a troubleshoot as what the error said, it might help you:

Actually google will always turn off the “less secure apps” if there is a suspicious activity using the account and the phpmailer triggered the activity and turned off the “less secure apps”. That’s why there is an error here:

2020-05-11 23:15:51 SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs534-5.7.14 eyNeEZgY375nHlJBVEeyY_7EPxhQGbttnTR9pfUt53-_19C7Ahzf0c4UlVOOObcFI_PWy534-5.7.14 q72PTEPxvmauAyN1UsqDsTCLLwQ7UURscj7nVgxGgaz9ZR6vyVO5ahAhxENt1S7k>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 Can't sign in to your Google Account - Gmail Help s11sm29488568wms.5 - gsmtp
SMTP Error: Could not authenticate.

Google’s “Less Secure Apps” system has always been annoying, and IIRC they will actually remove it completely this year. Instead, you should enable two factor authentication and use an App Specific Password to login instead.

The process is detailed in the setup instructions of our example contact form:

4 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.