wesionaryTEAM

Visionary Development Team. We ❤︎ Technology!

Follow publication

Member-only story

Sending Email And Attachment With GO (Golang) Using SMTP, Gmail, and OAuth2

--

Recently, I had to send an email through the app that I was building in Golang. Since using any third-party packages was not an option, I was left with the option of using my own Gmail account. First of all, I tried sending an email with net/smtp package using my email credentials. But this solution required me to go into my account setting and Enable Less Secure Apps. Enabling less secure apps can lead to various security-related issues and knowing this I don’t want my app deployed to production. So after quite a bit of research, I came to the solution of using OAuth2 and Gmail API. Through this approach, an email will be sent from Gmail API and will use OAuth2 credentials to send them which is much better from a security point of view. Let’s explore both ways to send emails in Go.

Sending email with Go via Gmail and net/smtp

This method is very simple. You will need your actual email credentials and enable less secure apps in your email account settings. To enable a less secure app you can go to your email account’s security section and scroll down to Less secure app access which is not recommended by Google itself. The code is pretty simple.

package gomail

import (
"errors"
"fmt"
"net/smtp"
)

var emailAuth smtp.Auth

func…

--

--

Responses (2)

Write a response