2

I'm building a small react native app with Expo and needs a functionality to send e-mails from within the app. The user doesn't need to compose anything in the first place, so it's simply a matter of pushing a button that sends an e-mail to an address that the user types in.

My question is what the best architecture for this job is. So far I have looked into simply using the Fetch API and then calling the MailGun API. However, as far as I understand it's not best practice to expose the API key in plain text, so I have considered Cloud Functions from Firebase, but am not sure whether this would be a viable option. Can anyone help me by pointing me in the right direction?

avriis
  • 1,581
  • 4
  • 17
  • 31
  • 1
    Use cloud functions, setup a listener on a key `.onCreate`. Then when the record is created in the react app - after the button is pressed, the cloud function can fire up and send the email. – sketchthat Jun 19 '18 at 01:15
  • 1
    As far as using the Fetch API, it wouldn’t work anyway because you can’t make authenticated requests to the Mailgun API from frontend JavaScript code running in a browser. The Mailgun API intentionally doesn’t support that. See the answer at https://stackoverflow.com/questions/50076659/mailgun-api-request-header-field-authorization-is-not-allowed-by-access-control/50081948#50081948 – sideshowbarker Jun 19 '18 at 07:23
  • 1
    Thanks @sketchthat, that worked for me. If you'll write into an answer, I'll accept :) – avriis Jun 19 '18 at 13:22

1 Answers1

1

You're correct, it's not best practice to expose the API key as others could access it and use your MailGun credit.

An easy way to get started is to create a Cloud Function that listens to some path .onCreate.

Within your application - push to that path and the Cloud Function will fire up and send the mail without exposing your private key.

Something to note - you cannot connect to external domains while on the firebase free tier - so if you get an error over the API it could be because you need to update plans.

sketchthat
  • 2,678
  • 2
  • 13
  • 22
  • what do you mean by cannot connect to external domains? – Zyzz Shembesh Jun 23 '19 at 15:37
  • On the Free Tier of Firebase you can only connect to Google services. Because MailGun is an external service you need to have a paid plan active for the cloud function to work. – sketchthat Jun 24 '19 at 01:20