Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fsobh/mail
A Go module to easily send out emails via SendGrid or AWS
https://github.com/fsobh/mail
Last synced: about 1 month ago
JSON representation
A Go module to easily send out emails via SendGrid or AWS
- Host: GitHub
- URL: https://github.com/fsobh/mail
- Owner: fsobh
- Created: 2024-11-28T05:30:01.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2024-11-28T07:48:17.000Z (about 2 months ago)
- Last Synced: 2024-11-28T08:29:48.516Z (about 2 months ago)
- Language: Go
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
Awesome Lists containing this project
README
# Mailing Module for Golang
A Golang module for sending emails seamlessly using [AWS](https://aws.amazon.com/ses/) or [Send Grid](https://sendgrid.com/).
## Features
- **AWS SES Integration**: Send transactional emails via AWS SES.
- **Send Grid Support**: Integrate with SendGrid for email delivery.
- **Simple API**: Easy-to-use interface for developers.---
## Installation
Get started by installing the package:```sh
go get github.com/fsobh/mail
```---
## Usage
### Initialize the Mailer
Before sending emails, configure the mailer with your provider's credentials.#### Ensure that the sender email has been verified with its respective provider :
1. AWS SES guide [here](https://docs.aws.amazon.com/ses/latest/dg/creating-identities.html)
2. Send Grid guide [here](https://www.twilio.com/docs/sendgrid/ui/sending-email/sender-verification)
```go
package mainimport (
"fmt"
"github.com/fsobh/mail"
)func main() {
/*
Never paste your credentials directly in the code for safety reasons
Recommended : Store these in environment variables using [viper](https://github.com/spf13/viper)
*/senderEmail := "[email protected]"
sesRegion := "us-east-1"
sgAPIKey := ""
sgAppName := ""//Initialize a new AWS SES mailer
mailerSES := mail.NewSESSender(sesRegion, senderEmail)//Initialize a new Send Grid mailer
mailerSendGrid := mail.NewSendGridSender(sgAPIKey, sgAppName, senderEmail)subject := "Email Subject"
contentHtml := fmt.Sprintf("Email HTML content
")
to := []string{"[email protected]"}
cc := []string{"[email protected]"}
bcc := []string{"[email protected]>"}
attachments := []string{"../files/example.txt"}// SES example
err := mailerSES.SendMail(subject, contentHtml, to, cc, bcc, nil) //can't do attachments on SESif err != nil {
_ = fmt.Errorf("error sending email via SES: %s", err)
}// Send Grid example
err = mailerSendGrid.SendMail(subject, contentHtml, to, cc, bcc, attachments)if err != nil {
_ = fmt.Errorf("error sending email via Send Grid: %s", err)
}fmt.Println("Email sent successfully")
}
```
## Notes
#### To test AWS SES sends locally, you must export your IAM roles Access ID & Secret :
1. Follow [this](https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/) guide to generate the credentials for your IAM account **(make sure you give it the right access policies to send Emails via SES)**.
2. Export your credentials locally in your terminal
```bash
export AWS_ACCESS_KEY_ID="your-access-key-id"
export AWS_SECRET_ACCESS_KEY="your-secret-access-key"
export AWS_DEFAULT_REGION="us-west-2" # Change to your region
```## License
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.