This project provides functionality to send emails using SMTP, with a focus on sending emails through Gmail. Also includes test cases to ensure the functionality is working as expected.
To use this package, you'll need to obtain an application-specific password for the Gmail account you want to send emails from. This password is required for authentication when sending emails.
First init the mod:
go mod init [project name] && go mod tidyThen you can get the two go modules from github:
go get github.com/jordan-wright/email && go get github.com/stretchr/testify/requireTo obtain an application-specific password for Gmail:
- Go to your sender gmail Account settings: https://myaccount.google.com/
- Click on "Security" in the left sidebar.
- You have to activate the two way factor
- Go to the two way factor menu and search the application passwords
- Then you have to create an application and copy the code, that is your password
- Create a
GmailSenderinstance usingNewGmailSenderfunction, providing your name, Gmail address, and the application-specific password. - Call the
SendEmailmethod of theGmailSenderinstance to send emails.
Here is an example:
package mail
import (
"testing"
"github.com/stretchr/testify/require"
)
const EMAIL_SENDER_NAME = "Example Name"
const EMAIL_SENDER_ADRESS = "examplemail@gmail.com"
const EMAIL_SENDER_PASSWORD = "password that google give you"
func TestNewGmailSender(t *testing.T) {
sender := NewGmailSender(EMAIL_SENDER_NAME, EMAIL_SENDER_ADRESS, EMAIL_SENDER_PASSWORD)
subject := "Test subject"
content := `
<h1>EXAMPLE</h1>
<p>Hello world!</p>
`
to := []string{"exampledestination@gmail.com"}
attachFiles := []string{"rute to your file to send"}
err := sender.SendEmail(subject, content, to, nil, nil, attachFiles)
require.NoError(t, err)
}