简体   繁体   中英

Using Other Domain to Send E-Mail in GMail API with PHP

My client want to use GMail API for their existing web application using their own domain . So they can send automatic e-mail from their mail server. The code they are using is in plain PHP. Is this possible? Can anyone show me the step-by-step tutorial for this? Thank you very much

[EDIT]

So they want to send e-mail whose sender is like this: foo@mycompany.co.id

Yes it is possible!

1st you will have to enable "Less secure apps in Gmail"

在此处输入图片说明

IMPORTANT: If you use 2 factor authentication to access your Google account, access for less secure apps cannot be enabled. In this case, you will have to sign in using App password.

You can find Google SMTP details below:

  • List itemSMTP Server : smtp.gmail.com
  • SMTP Username: Your full Gmail username (email address), for example youremail@gmail.com
  • SMTP Password: Your Gmail password.
  • SMTP Port: 587
  • TLS/SSL: Required.

Now, you need to integrate into your PHP Code. There are many libraries you could use . Eg: PHPMailer .

It is possible and here are some links and notes to do that

  1. Start here https://developers.google.com/gmail/api/quickstart/php to understand how to run a test program.
  2. Now, edit the test program, to add code to send email. Use the example here - https://developers.google.com/gmail/api/v1/reference/users/messages/send . Select the PHP tab to view the relevant code.
  3. Once you get a feel of it, you need to set up things for real time use
  4. You have different OAuth 2.0 options to set up. Choose one from here - https://developers.google.com/api-client-library/php/guide/aaa_overview
  5. If you are unsure of which one to choose, I would recommend this - https://developers.google.com/api-client-library/php/auth/web-app
  6. Once you get the access token and refresh token (MUST to use from an automated program), save them in the database and start making calls ex. like the one in https://developers.google.com/gmail/api/v1/reference/users/messages/send Note: To get refresh token, you need to ask for "offline" access. Whenever the access token is expired, you need to get a new access token using the refresh token that you saved.

Hope this helps.

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