Skip to content

A Golang package for sending emails using SMTP, SparkPost, SendGrid and MailGun

License

Notifications You must be signed in to change notification settings

harranali/mailing

Repository files navigation

A Golang package for sending emails using SMTP, SparkPost, SendGrid and MailGun

Build Status Test Status Go Report Card GoDoc Coverage Status

Features

  • 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

Install

Here is how to add it to your project

go get github.com/harranali/mailing

Using specific driver

Here is how to use SMTP Driver
// 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
		},
	})
Here is how to use Spark Post Driver
// initiating the mailer with SparkPost driver
mailer := mailing.NewMailerWithSparkPost(&mailing.SparkPostConfig{
		BaseUrl:    "https://api.sparkpost.com",
		ApiKey:     "test-api-key",
		ApiVersion: 1,
	})
Here is how to use Send Grid Driver
// initiating the mailer with SendGrid driver
mailer := mailing.NewMailerWithSendGrid(&mailing.SendGridConfig{
		Host:     "https://api.sendgrid.com",
		Endpoint: "/v3/mail/send",
		ApiKey:   "API-KEY-HERE",
	})
Here is how to use Mail Gun Driver
// 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
	})

Usage

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())
}

Testing you emails with smtp4dev SMTP Testing Server

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.

Running the testing server

Copy the docker-compose.yaml to your pc, then start the container by running

docker-compose up

The testing server configuration

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
		},
	})

Accessing the testing server UI

The testing server UI allows you to check the emails, here is a link to the ui of the testing server

http://localhost:5000

smtp4dev server ui

About

A Golang package for sending emails using SMTP, SparkPost, SendGrid and MailGun

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages