-1

I want to programmatically send email from my personal email account using Gmail API and PHP. The reason for asking this question is:

I cannot use SMPT, because I'm using A2 Hosting which bans shared hosting plans from connecting to a remote SMPT server.

It seems the only solution is using Gmail API. I can see many similar tutorials. However, they are mostly not for using personal email account(i.e., most of these methods ask for users' authorization and send emails on their behalf). My case is different: I just want to send emails using my personal gmail account.

So far, I have just found one relevant tutorial on this topic http://sahmwebdesign.com/gmail-api-via-service-accounts/ . But it seems I have to have a G-Suite account for this method, which means additional cost to me.

Is there anyway to get service accounts working with a normal gmail user account. If not how can I use the gmail api without having to login all the the time.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Tao Hu
  • 57
  • 1
  • 9
  • see https://stackoverflow.com/questions/19766912/how-do-i-authorise-an-app-web-or-installed-without-user-intervention – pinoyyid Jan 29 '19 at 00:51
  • @pinoyyid Thanks, I've read your answers on the above link. Do you have any example code in PHP? – Tao Hu Mar 02 '19 at 12:33
  • The only code you need is a rest call to obtain an access token using the refresh token, and then a rest call to submit your email. – pinoyyid Mar 02 '19 at 14:43

1 Answers1

0

You need to remember that Gmail account is private user data. In order to use the Gmail API to send an email you must have the permission of the owner of that account in order to send email programmatic from that account.

To do that we use Oauth2. You will need to authenticate the application once store the refresh token someplace then you will be able to use the refresh token at a later date to get a new access token to use it. There is no way around this using a normal user gmail account

I have some code here which shows how to use a refresh token Oauth2Authentication.php

$client->refreshToken($_SESSION['refresh_token']);
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->setAccessToken($client->getAccessToken()); 

Service accounts

Service accounts are intended for server to server interaction they are pre approved. Unfortunately service accounts only work with Gsuite gmail accounts not normal user gmail accounts.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Thanks DalmTo. I'm not clear about RedirectUri. I don't need my application to redirect to a page for composing an email, because my email content is known to me (including receiver email address, subject, content, attachment). Do I still have to specify a RedirectUri? If so, what will it be like (or what shall I do in the RedirectUri)? I'm trying hard to understand the example code on this page https://www.pipiscrew.com/2015/04/php-send-mail-through-gmail-api/ It seems after the user authenticates, it redirects to https://www.x.com/pipis2 (but I have no idea what this link does). Many thanks! – Tao Hu Feb 03 '19 at 12:06