- Multiple File attachments
- Multiple recipients
- Multiple CC
- Multiple BCC
- HTML content type support
- Plain Text content type support
- Easy integration with smtp4dev testing server for development
- Multiple Drivers Support: SMTP, SparkPost, SendGrid and MailGun
Here is how to add it to your project
go get github.com/harranali/mailing
// initiating the mailer with smtp driver
mailer := mailing.NewMailerWithSMTP(&mailing.SMTPConfig{
Host: "localhost", //the SMTP server host
Port: 25, // The Port
Username: "",
Password: "",
TLSConfig: tls.Config{
ServerName: "localhost",
InsecureSkipVerify: true, // (use true for development only) true accepts any certificate presented by the server
},
})
// initiating the mailer with SparkPost driver
mailer := mailing.NewMailerWithSparkPost(&mailing.SparkPostConfig{
BaseUrl: "https://api.sparkpost.com",
ApiKey: "test-api-key",
ApiVersion: 1,
})
// initiating the mailer with SendGrid driver
mailer := mailing.NewMailerWithSendGrid(&mailing.SendGridConfig{
Host: "https://api.sendgrid.com",
Endpoint: "/v3/mail/send",
ApiKey: "API-KEY-HERE",
})
// initiating the mailer with MailGun driver
mailer := mailing.NewMailerWithMailGun(&mailing.MailGunConfig{
Domain: "your-domain.com", // your-domain.com
APIKey: "TEST-API-KEY", // your api key
SkipTLSVerification true // (set true for development only!) // true means accepts any tls certificate sent by the domain without verification
})
Here is how to use it
// Initiate the package with SMTP Driver
mailer := mailing.NewMailerWithSMTP(&mailing.SMTPConfig{
Host: "localhost", //the SMTP server host
Port: 25, // The Port
Username: "",
Password: "",
TLSConfig: tls.Config{
ServerName: "localhost",
InsecureSkipVerify: true, // (use true for development only) true accepts any certificate presented by the server
},
})
// Set the Sender email address
mailer.SetFrom(mailing.EmailAddress{
Name: "from name", // name can be set to empty string ("")
Address: "[email protected]",
})
// Set the Recipients email addresses
mailer.SetTo([]mailing.EmailAddress{
// name can be set to empty string ("")
{Name: "first to name", Address: "[email protected]"},
{Name: "second to name", Address: "[email protected]"},
})
// Set CC email addresses
mailer.SetCC([]mailing.EmailAddress{
// name can be set to empty string ("")
{Name: "cc name", Address: "[email protected]"},
{Name: "cc name", Address: "[email protected]"},
})
// Set BCC email addresses
mailer.SetBCC([]mailing.EmailAddress{
// name can be set to empty string ("")
{Name: "bcc name", Address: "[email protected]"},
})
// Set the subject
mailer.SetSubject("This is the subject")
// Set the body (pick one: either HTML or Plain Text)
mailer.SetHTMLBody("<h1>This is the email body</h1>")
// OR
mailer.SetPlainTextBody("This is the email body")
// Set the sttachments files
mailer.SetAttachments([]mailing.Attachment{
{
Name: "first file",
Path: "./myfiles/first-file.jpg",
},
{
Name: "second file",
Path: "./myfiles/second-file.pdf",
},
})
// Send the email
err := mailer.Send()
if err != nil {
panic(err.Error())
}
While developing your app you might need to test your emails, for that a customized docker-compose.yaml from the SMTP testing server smtp4dev is included.
Copy the docker-compose.yaml to your pc, then start the container by running
docker-compose up
Here is how to connect to the testing server
mailer := mailing.NewMailerWithSMTP(&mailing.SMTPConfig{
Host: "localhost", //the SMTP server host
Port: 25, // The Port
Username: "",
Password: "",
TLSConfig: tls.Config{
ServerName: "localhost",
InsecureSkipVerify: true, // (use true for development only) true accepts any certificate presented by the server
},
})
The testing server UI
allows you to check the emails, here is a link to the ui of the testing server
http://localhost:5000