Search code examples
node.jsgoogle-cloud-functionsgoogle-oauthgmail-api

sending email from firebase cloud functions, preferably using gmail, behalf of the logged in (google authenticated) user


I have a project that uses Firebase Cloud functions (nodejs). The project has an android app and a web app. I have to send transactional emails (the user invites someone to participate, not many mails, average less than 5 per user within years, definitely not spam) both from the android app and the web app.

I know how to send email using gmail smtp but it sends the mails always from my app's account. Since the invitation mail is a personal thing, it would be great to use the user's email as sender. I know there is a way to start the default email app but it is complicated (not for me but the user) and destroys the user experience.

The users of my app are logged in, authenticated with their google account theefore I guess there must be a way to use their credentials to send the email and so use their email address as sender. I searched the net but could not find any working solution for this.

Anyone any idea? Thanks in advance, Peter

I tried simply change the "from" field, of course it was useless, the mail went on my app's address. I tried to use the default email app but it is uncomfortable, sometimes disturbing, confused the user about what to do and because there is a special code in the mail, the user could change or delete it make the invitation broken. I tried to search other providers but since my users are authenticated with their google account it did not work anyhow.


Solution

  • You can use the gmail api to send emails you will need the user to authorize your application accessing their gmail account to send the email.

    just remember that verification for a gmail api scope is not free and is time-consuming.

    I wouldnt bother for 5 emails a year.