Phpmailer: phpmailer error Invalid address: (From): $google_email

Created on 20 May 2019  路  8Comments  路  Source: PHPMailer/PHPMailer

I wanted to connect to gmail via phpmailer but i can't because of gmail security issue, so i opted for the secure way by using PHPMailer XAUTH2 authentication process. then i start having this error Invalid address: (From): $google_email.

  `<?php
require ("../vendor/autoload.php");

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\OAuth;
use League\OAuth2\Client\Provider\Google;

/* Set the script time zone to UTC. */

 date_default_timezone_set('Etc/UTC');
 /* Information from the XOAUTH2 configuration. */
 $google_email = '[email protected]';
$oauth2_clientId = myclientid.apps.googleusercontent.com';
$oauth2_clientSecret = 'myclientsecret';
$oauth2_refreshToken = 'myrefreshtoken';
//Create a new PHPMailer instance
$mail = new PHPMailer(TRUE);
try{
//Set who the message is to be sent from
$mail->setFrom('$google_email');
//Set who the message is to be sent to
 $mail->addAddress('recipientaddress');
$mail->Subject = "Sending from localhost";
$mail->Body = "Testing Testing Testing";   
$mail->isSMTP();
$mail->port = 465;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.google.com';
$mail->AuthType = 'XOAUTH2';
$provider = new Google(
[
    'clientId' => $oauth2_clientId,
    'clientSecret' => $oauth2_clientSecret,
]
);
$mail->setOAuth(
new OAuth(
    [
        'provider' => $provider,
        'clientId' => $oauth2_clientId,
        'clientSecret' => $oauth2_clientSecret,
        'refreshToken' => $oauth2_refreshToken,
        'username' => $google_email,
    ]
)
);
$mail->send();
}
catch (Exception $e)
{
   echo $e->errorMessage();
  echo $mail->ErrorInfo;
   }
    catch (\Exception $e)
     {
     echo $e->getMessage();
      echo $mail->ErrorInfo;
     }`

Most helpful comment

I think you need to remove the single quotes from the following line (the variable isn't expanded inside):
$mail->setFrom('$google_email');

All 8 comments

I think you need to remove the single quotes from the following line (the variable isn't expanded inside):
$mail->setFrom('$google_email');

I think you need to remove the single quotes from the following line (the variable isn't expanded inside):
$mail->setFrom('$google_email');

i did then i receive this error
Notice: Undefined index: userName in C:xampphtdocsfreevendorphpmailerphpmailersrcOAuth.php on line 89
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Because PHP array keys are case-sensitive, so username != userName.

So how will I solve
Invalid address: (From): $google_email
please

I think what @Synchro hinted at was this:

You need a) the change from my earlier reply, and then b) in your "new OAuth(" block, in the line: 'username' => $google_email, (apparently your line 89) replace username with userName.

If it's complaining that the address is literally $google_email (which is indeed not a valid email address), it sounds like you have not removed the single quotes as @m4z suggested. To be clear:

$mail->setFrom($google_email);

I remove the quote and block the line then I receive this error
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

So follow the link, and do what it says - it will be failing for a reason, but we can't tell what that is for you, so you need to investigate further.

Was this page helpful?
0 / 5 - 0 ratings