如何使用PHP和Gmail API从我的个人Gmail帐户发送电子邮件?

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.

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.