DEV Community

Google Cloud Functions: Sending emails using Gmail SMTP and Nodemailer

Diego Perez on April 12, 2019

I have a very simple website hosted on a GCP storage segment and now I need to make it slightly less simple by adding a contact form. Then I realiz...
Collapse
 
heatherhosler profile image
Heather Hosler

Very helpful guide, thanks. However, I believe there is a typo in your replace function.
I think it should be replacing the single backslash with the escaped doule-backslash:
process.env.PRIVATE_KEY.replace(/\n/g, "\\n")

Collapse
 
i_maka profile image
Diego Perez

Hi Dominic,

Thank you for your suggestion. I had to double check because, I confess, it's a bit counterintuitive.

I don't remember exactly, but I think that at some point GC checks the string and, if it finds a "\n", it will add an additional backslash trying to do us a favor, hence we need to check it and remove the additional backslash to keep maintain the integrity of the private key.

Collapse
 
mahnoorgit profile image
Mahnoor-git

i am new to google function can you please explain , where and how to run "npm i --S nodemailer" command on google cloud platform.
Secondly i am getting this error "Deployment failure:
Function failed on loading user code. Error message: Node.js module defined by file index.js is expected to export function" on deploying

Collapse
 
i_maka profile image
Diego Perez

Hey there, thanks for asking!

"npm i --S nodemailer" has to be run on your local terminal, it will download the Nodemailer dependencies and update your package.json with the dependency. Your file should look like my package.json on the dependencies section.

Regarding the error, bear in mind that GCP expect you to export a method that will be run from the server, in this example I exported the method "sendMail()". Maybe is not very clear on the article, but you can check the final result of the file here. You will find the final method at the very end of the file.

Collapse
 
mahnoorgit profile image
Mahnoor-git

Thank So much for the humble reply .This issue is now resolved. Now i am facing an error {"error":{"code":422,"message":"Missing arguments"}} . G suite admin console require domain verification which i can not do , Can we use another way instead of using GSuite ?

Thread Thread
 
i_maka profile image
Diego Perez

Based on what you say, the error is possibly because you are not providing the api key... I'm afraid you will need to check if there is another way to authenticate GSuite and modify the code accordingly, but there is no option without verifying your domain, as far as I'm aware.

Thread Thread
 
mahnoorgit profile image
Mahnoor-git

Yeah seems like that.Anyways Thanks alot.

Collapse
 
zakfu profile image
Zak Ainsworth

Great guide. Used this to add a contact form to my site. One step that I had to do that wasn't included here was to check the "Enable G Suite Domain-wide Delegation" checkbox when creating the service account.

Collapse
 
i_maka profile image
Diego Perez

Thank you.

That's interesting, I just checked I didn't have to enable it... Maybe your G Suite is configured in a specific way that requires it to be enabled? In my case, G Suite is configured as it comes out-of-the-box.

Collapse
 
gregfenton profile image
gregfenton

So if I have a custom domain (@domain) on Google Workspace, and I have a bunch of Firebase projects each of which I would like to be sending emails "from @domain".

In which account do I use for getting the Service Account, in which do I enable the GMail API, how do I (securely!) get the configuration into each of these different Firebase projects, etc?

Collapse
 
i_maka profile image
Diego Perez

I haven't worked with Firebase, and I'm not entirely sure what you mean by "get the configuration". If you mean the function's URL, I use environment variables in my projects.

Regarding the service account... I would use any account from @domain

Collapse
 
derrickrc profile image
derrickrc

Thanks for this guide! I was getting error code 500 and I realized it was because I wasn't pasting the entire private key (including the very beginning portion of '-----BEGIN PRIVATE KEY-----\n...

Collapse
 
shahzorequreshi profile image
Shahzore Qureshi

I LOVE THIS SO MUCH. You saved me so much time. It is such a complicated procedure for something so simple. I am so happy I found this guide. You are a lifesaver!!! Thank you thank you thank you.

Collapse
 
i_maka profile image
Diego Perez

Very glad I was able to help. Thank you for sharing your feedback :)

Collapse
 
adhendo profile image
adhendo

Anyone having problems with their mail being sent to spam folder? Thank you for any help!
I have enabled domain wide delegation for service account...

Collapse
 
ywroh profile image
ywroh

Thanks for the good information