简体   繁体   中英

How to send email using Google OAuth (via SMTP)?

Have read this and this and using example from here .

However, I am still having difficulty in sending emails as a verified Google user using OAuth. I have the valid OAuth tokens, secrets, and xoauth token as well.

My code to send the email:

$authenticateParams = array('XOAUTH', $initClientRequestEncoded); 

$smtp = new Zend_Mail_Protocol_Smtp('smtp.gmail.com', 587, array(
"AUTH" => $authenticateParams, 'ssl' => 'tls')); 

try {
   // Create a new mail object
   $mail = new Zend_Mail();

   $mail->setFrom("aaaaa@gmail.com");
   $mail->addTo("bbbbb@gmail.com");
   $mail->setSubject("Your account has been created");

   $email = "Thank you for registering!";

   $mail->setBodyText($email);
   $mail->send();

} catch (Exception $e) {
   echo "error sending email . <BR>" . $e;
}

But this seems to send an anonymous email and not as the user authenticated. If I do:

$smtp = new Zend_Mail_Transport_Smtp('smtp.gmail.com', array('AUTHENTICATE' => $authenticateParams, 'ssl' => 'tls')); 
Zend_Mail::setDefaultTransport($smtp);

I get: exception 'Zend_Mail_Protocol_Exception' with message '5.5.1 Authentication Required.

I'm sure it's just a case of getting the mail transport smtp params right, but the documentation for sending SMTP emails is non-existent and I can't find any code examples for this.

Check out the following blog posts which will show you where you went wrong.

google smtp oauth2 authentication

send mail with gmail smtp and xoauth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM