No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Zeni Kim 855de1acef
Some checks are pending
build-main / build (push) Waiting to run
Code Coverage / Test with Coverage (push) Waiting to run
test-main / build (push) Waiting to run
fix
2026-09-30 17:55:19 -05:00
.github/workflows add github workflows 2023-07-22 00:35:27 +04:00
smtp-testing-server update readme file 2023-07-21 11:04:46 +04:00
testingdata add message builder test 2023-07-21 20:20:54 +04:00
.gitignore update readme file 2023-07-21 11:04:46 +04:00
go.mod update golang and packages version 2026-09-22 21:20:30 -05:00
go.sum update golang and packages version 2026-09-22 21:20:30 -05:00
LICENSE init commit 2023-07-19 16:43:21 +04:00
mailgun-driver.go add tls handling config 2023-07-27 21:21:46 +04:00
mailgun-driver_test.go add mail gun test 2023-07-27 21:01:34 +04:00
mailing.go implement initiate mail gun driver 2023-07-27 21:08:31 +04:00
mailing_test.go increase test coverage 2023-07-22 17:23:01 +04:00
message.go fix(mailing): add RFC 2046 part separators and omit empty Cc header 2026-09-28 22:07:27 -05:00
message_test.go add sparkpost driver 2023-07-22 16:59:36 +04:00
README.md feat(mailing): select SMTP auth mechanism dynamically (LOGIN/PLAIN) for Outlook/Office 365 2026-09-30 12:16:35 -05:00
sendgrid-driver.go add mail gun test 2023-07-27 21:01:34 +04:00
sendgrid-driver_test.go add sendgrid test 2023-07-27 14:39:08 +04:00
smtp-driver.go fix 2026-09-30 17:55:19 -05:00
smtp-driver_test.go add sendgrid driver 2023-07-23 17:45:03 +04:00
sparkpost-driver.go add sendgrid driver 2023-07-23 17:45:03 +04:00
sparkpost-driver_test.go add sendgrid driver 2023-07-23 17:45:03 +04:00

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

This project starts from a full copy from https://github.com/harranali/mailing. 22 Sep 2026.

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
		},
	})
SMTP authentication mechanism

By default the driver selects the authentication mechanism dynamically: after STARTTLS it reads the server's advertised AUTH capabilities and prefers AUTH LOGIN (accepted by Microsoft Outlook/Office 365, Gmail, Amazon SES, Mailgun, SendGrid, Postmark, ...), falling back to AUTH PLAIN for legacy servers that only support it. This means Outlook/Office 365 accounts work out of the box even though they reject AUTH PLAIN.

You can force a specific mechanism through the AuthMechanism field:

mailer := mailing.NewMailerWithSMTP(&mailing.SMTPConfig{
		Host:          "smtp.office365.com",
		Port:          587,
		Username:      "user@yourdomain.com",
		Password:      "your-password",
		AuthMechanism: mailing.AuthLogin, // AuthAuto (default) | AuthLogin | AuthPlain
		TLSConfig: tls.Config{
			ServerName: "smtp.office365.com",
		},
	})

Leaving AuthMechanism empty keeps the previous behavior of being fully automatic and is backward compatible with existing configurations. AuthXOAuth2 is reserved as groundwork for OAuth2.

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: "from@mail.com",
    })

// Set the Recipients email addresses
mailer.SetTo([]mailing.EmailAddress{
        // name can be set to empty string ("")
        {Name: "first to name", Address: "theFirstTo@mail.com"},
        {Name: "second to name", Address: "theSecondTo@mail.com"},
    })

// Set CC email addresses
mailer.SetCC([]mailing.EmailAddress{
        // name can be set to empty string ("")
        {Name: "cc name", Address: "cc1@mail.com"},
        {Name: "cc name", Address: "cc2@mail.com"},
    })

// Set BCC email addresses
mailer.SetBCC([]mailing.EmailAddress{
        // name can be set to empty string ("")
        {Name: "bcc name", Address: "bcc@mail.com"},
    })

// 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