Vikram Rathore
Vikram Rathore

Reputation: 3

How can I send e-mail using Gmail API in php

I'm trying to send email without enabling less secure app in google account.

I've found alternate option to send email using Gmail. I have all set up Gmail API configuration but I am not able to send mail using Gmail API in core php.

Upvotes: 0

Views: 1866

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116908

Let less secure apps access your account If you are using php to send emails then you are always going to have to have less secure app enabled. I have never heard of a developer getting their app upgraded to a level that google would not qualify them as insecure. They leave that to themselves and their partners.

The only way i know of to get around this would be to drop using the Gmail api and go directly though the SMTP server. You will probably need to use user name and password unless you manage to get xoauth2 working

Bonus info Send mail php code

$client = getClient();

$service = new \Google_Service_Gmail($client);
$mailer = $service->users_messages;

$message = (new \Swift_Message('Here is my subject'))
    ->setFrom('[email protected]')
    ->setTo(['[email protected]'=>'Test Name'])
    ->setContentType('text/html')
    ->setCharset('utf-8')
    ->setBody('<h4>Here is my body</h4>');

$msg_base64 = (new \Swift_Mime_ContentEncoder_Base64ContentEncoder())
    ->encodeString($message->toString());

$message = new \Google_Service_Gmail_Message();
$message->setRaw($msg_base64);
$message = $mailer->send('me', $message);
print_r($message);

Upvotes: 1

Related Questions