The best way to send emails in Go.

Gomail

Build Status Code Coverage Documentation

Introduction

Gomail is a simple and efficient package to send emails. It is well tested and documented.

Gomail can only send emails using an SMTP server. But the API is flexible and it is easy to implement other methods for sending emails using a local Postfix, an API, etc.

It is versioned using gopkg.in so I promise there will never be backward incompatible changes within each version.

It requires Go 1.2 or newer. With Go 1.5, no external dependencies are used.

Features

Gomail supports:

  • Attachments
  • Embedded images
  • HTML and text templates
  • Automatic encoding of special characters
  • SSL and TLS
  • Sending multiple emails with the same SMTP connection

Documentation

https://godoc.org/gopkg.in/gomail.v2

Download

go get gopkg.in/gomail.v2

Examples

See the examples in the documentation.

FAQ

x509: certificate signed by unknown authority

If you get this error it means the certificate used by the SMTP server is not considered valid by the client running Gomail. As a quick workaround you can bypass the verification of the server's certificate chain and host name by using SetTLSConfig:

package main

import (
	"crypto/tls"

	"gopkg.in/gomail.v2"
)

func main() {
	d := gomail.NewDialer("smtp.example.com", 587, "user", "123456")
	d.TLSConfig = &tls.Config{InsecureSkipVerify: true}

    // Send emails using d.
}

Note, however, that this is insecure and should not be used in production.

Contribute

Contributions are more than welcome! See CONTRIBUTING.md for more info.

Change log

See CHANGELOG.md.

License

MIT

Contact

You can ask questions on the Gomail thread in the Go mailing-list.

Comments
  • 504 5.7.4 Unrecognized authentication type

    504 5.7.4 Unrecognized authentication type

    From golang-nuts: https://groups.google.com/d/msg/golang-nuts/ywPpNlmSt6U/0Mxttkx9kgQJ

    mailer := gomail.NewMailer("smtp.office365.com", "[email protected]", "password", 587)
    

    Returns error 504 5.7.4 Unrecognized authentication type

  • Cannot send email from docker container

    Cannot send email from docker container

    dialandsend return an error when inside a privileged docker container When inside a kubernetes, the error does not seem to happen, but the email do not get sended.

    web_1 | /api/register
    web_1 | 172.18.0.1 - - [10/Apr/2016:20:51:59 +0000] "POST /api/register HTTP/1.1" 200 78
    web_1 | http://localhost:9001/api/confirm/$2a$04$6Dad45u6SlTB7rWTNz2GzOc3j9jWOjT.1oTjfuvO5Bd8gKGwnvX56
    web_1 | panic: x509: failed to load system roots and no roots provided
    web_1 | 
    web_1 | goroutine 49 [running]:
    web_1 | panic(0x9f49c0, 0xe2e628)
    web_1 |         /usr/lib/go/src/runtime/panic.go:464 +0x3e6
    web_1 | bitbucket.org/cescoferraro/templatego/src/backend/api.SendRegisterEmail(0xc820210b60, 0x0, 0x0, 0xc8201f99d0, 0x9, 0xc8201fd540, 0x1b, 0xc820200b00, 0x3c, 0x0, ...)
    web_1 |         /home/cesco/code/go/src/bitbucket.org/cescoferraro/templatego/src/backend/api/register.go:85 +0x7af
    web_1 | created by bitbucket.org/cescoferraro/templatego/src/backend/api.RegisterEndPoint
    web_1 |         /home/cesco/code/go/src/bitbucket.org/cescoferraro/templatego/src/backend/api/register.go:55 +0x656
    templatego_web_1 exited with code 2
    
  • go get fail

    go get fail

    somehow running the go get gopkg.in/gomail.v1 in centos 6 is not working.. it just stuck there for very very long. and i check the $GOPATH/src/gopkg.in folder its empty... and i have been very curious why is it not go get github.com/go-gomail/gomail instead...

  • AddAlternative bug

    AddAlternative bug

    ` message.SetBody("text/plain", "Hello pj")

    message.AddAlternative("text/html", "Hello html <b>pj</b>")
    

    ` This produces:

    `

    --908b4cfdd408aaeb4ad5a5525eed978aa6361f8e322bf69397de36fa21d6 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

    Hello pj --908b4cfdd408aaeb4ad5a5525eed978aa6361f8e322bf69397de36fa21d6 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

    Hello html pj --908b4cfdd408aaeb4ad5a5525eed978aa6361f8e322bf69397de36fa21d6-- `

    Hotmail shows the html version. Gmail shows the html version.

  • anonymous relay

    anonymous relay

    Hi .

    I am trying to relay on a server with no authentication . using a telnet client , I can simply :

    1. telnet MyServer MyPort
    2. Helo
    3. MAIL FROM: [email protected]
    4. RCPT TO: [email protected]
    5. SUBJECT:how are you?
    6. period (.)

    I tried to create an Auth with Start function that returns immediately , with empty return value , or "MAIL FROM:" , but I guess no matter what I do , smtp package will go through : code, msg64, err := c.cmd(0, "AUTH %s %s", mech, resp64) that the server will decline .

    Do you have an idea for doing anonymous connection/ESMTP AUTH . my best help so far was http://www.fehcom.de/qmail/smtpauth.html

    Thanks .

  • gmail login not working

    gmail login not working

    When I tried to test send an email using my gmail account I get the follow:

    panic: 534 5.7.9 Please log in with your web browser and then try again. Learn more at
    5.7.9  https://support.google.com/mail/?p=WebLoginRequired d134sm2180690ywb.23 - gsmtp
    
  • Encoded headers violate RFC 2047

    Encoded headers violate RFC 2047

    Here's a small Golang program which demonstrates the problem:

    package main
    
    import (
        "gopkg.in/gomail.v2"
        "os"
    )
    
    func main() {
        m := gomail.NewMessage()
        m.SetHeader("From", "[email protected]")
        m.SetHeader("To", "[email protected]")
        m.SetHeader("Subject", "{$firstname} Bienvendio a Apostólica, aquí inicia el camino de tu")
        m.SetBody("text/plain", "Hello, World")
        m.WriteTo(os.Stdout)
    }
    

    This program will output these headers:

    Mime-Version: 1.0
    Date: Wed, 16 Mar 2016 09:08:29 -0400
    From: [email protected]
    To: [email protected]
    Subject: =?UTF-8?q?{$firstname}_Bienvendio_a_Apost=C3=B3lica,_aqu=C3=AD_inicia_el_camino_de_tu?=
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: quoted-printable
    

    Note that the Subject header line is 96 characters long. According to RFC 2047,

    While there is no limit to the length of a multiple-line header field, each line of a header field that contains one or more 'encoded-word's is limited to 76 characters.

    The Subject header should have been folded at 76 characters but it was not.

  • It will become less secure app when send to gmail

    It will become less secure app when send to gmail

    I use gmail account to send a email,but it return a error with a warning. I can fixed it by turn off some secure stuff. https://support.google.com/accounts/answer/6010255?hl=en

    Can this stuff be fixed in this library?

  • Export to []byte

    Export to []byte

    It would be nice to be able to export gomail.Message to []byte. I would suggest a function Raw() []byte on Message.

    I could implement this and make a pull request.

  • There's a way to efficiently send more than one mail in one connection?

    There's a way to efficiently send more than one mail in one connection?

    I made a quick review of your code and I think you open close a connection for each Send (https://github.com/go-gomail/gomail/blob/master/send.go#L22).

    I want to implement a job to send emails from a queue in bulk, but reusing the connection to be as more efficient as possible.

    I miss something from your API?

    Thanks!

  • Auth: CRAM-MD5

    Auth: CRAM-MD5

    Hi,

    as far as I understood your code you implement the smtp.Auth interface which is then used by the smtp package when calling c.Auth().

    In auth.go you implement LOGIN and PLAIN mechanisms. Have you ever thought about implementing CRAM-MD5?

    You would have to return CRAM-MD5 in Auth.Send, listen for the base64 encoded challenge in Auth.Next, decode it and return (base64 encoded) the username and a MD5 response à la

    digest = MD5(('password' XOR opad), MD5(('password' XOR ipad), challenge)).

    Details can be found in this Tutorial and RFC 2195.

  • Incorrect behavior using html/template with AddAlternativeWriter

    Incorrect behavior using html/template with AddAlternativeWriter

    There is an issue where if the template crashes, the gomail.Send(sender, msg) returns the error, therefore implying correct error handling, but the email is sent regardless of template execution. I.e.: the html contains:

    a class="button-3" href="{{.Data.ConfirmationLink}}"> Confirm Email

    where Data is a map. the template is configured for "missingkey=error", so if Data does not contain this key, the function tmp.execute(w, mail) will return an error. However, the email will still be sent: Снимок экрана 2022-12-27 в 13 47 37 As seen, with the error log being:

    {"level":"error","time":"2022-12-27T13:47:24+03:00","message":"Could not send email to "xxx": gomail: could not send email 1: template: emailConfirmation.html:110:53: executing "emailConfirmation.html" at <.Data.ConfirmationLink>: map has no entry for key "ConfirmationLink""}

    I might be wrong in pointing this out, but in my opinion, if an error is found while executing the message template writer, the whole message should be discarded

  • Recipient address rejected: Email Sending Quota Exceeded

    Recipient address rejected: Email Sending Quota Exceeded

    I'm trying to send a mail using gomail. It was working well. I did send many test mails to my gmail (to check how email template renders) and now I got this error. I tried sending test mails to a different mail id (my yahoo.com mail) but I get the same error.

    What does this error actually mean. Is there a problem in the recipient address or in my server's code (sender). If the problem lies in the recipient address (as the error message says) then why am I not able to send mails to a completely different address. Also what is this sending quota and how to increase it ?

    What I already checked

    1. My inboxes (of both sending and receiving mail addresses) are no where close to full.
    2. Other normal mails (sent manually from some email client) works (no issues in any mail configurations [SMTP/POP3 etc.])

    Not forgetting the code

    func SendMail(mail_options map[interface{}]interface{}) {
    
    	from := mail_options["from"].(string)
    	pass := mail_options["password"].(string)
    	to := mail_options["to"].([]string)
    
    	host := mail_options["host"].(string)
    	port := 587
    
    	subject := mail_options["subject"].(string)
    	mail_body := mail_options["body"].(string)
    
    	msg := gomail.NewMessage()
    
    	msg.SetHeader("From", from)
    	msg.SetHeader("To", to...)
    
    	msg.SetHeader("Subject", subject)
    
    	msg.SetBody("text/html", mail_body)
    
    	// msg.attach("/path/to/file")
    
    	mail := gomail.NewDialer(host, port, from, pass)
    
    	err := mail.DialAndSend(msg)
    
    	if err != nil {
    		fmt.Println("ERROR: in sending mail")
    		utils.LogError(err) // A working function to log errors to a file.
    	}
    }
    
    

    Full Error Message

    ERROR: 2022/09/01 10:26:39 logger.go:27: gomail: could not send email 1: 550 5.4.6 <[email protected]>: Recipient address rejected: Email Sending Quota Exceeded
    

    Someone please help 🙏🙏

  • Mail not reciving to Yahoo

    Mail not reciving to Yahoo

    Hi, I have updated the go version from 1.15 to 1.18. But while sending with version 1.18, mails not receiving to yahoo accounts. Getting the error message 5.6.7(destination does not support SMTPUTF8 required for message delivery). Working fine with version 1.15.

  • Save as .eml file

    Save as .eml file

    Is there any way to create an email and,instead of sending it with SMTP or so, saving the email on disk in .eml format so another software take care of sending it?

    Thanks

Related tags
An easy way to send emails with attachments in Go

PROJECT DISCONTINUED This repository only exists for archival purposes. email An easy way to send emails with attachments in Go Install go get github.

Dec 12, 2022
Hxgomail - Gomail - a simple and efficient package to send emails

Gomail Introduction Gomail is a simple and efficient package to send emails. It

Jan 4, 2022
:white_check_mark: A Go library for email verification without sending any emails.

email-verifier ✉️ A Go library for email verification without sending any emails. Features Email Address Validation: validates if a string contains a

Dec 30, 2022
✉️ A Go library for email verification without sending any emails.

email-verifier ✉️ A Go library for email verification without sending any emails. Features Email Address Validation: validates if a string contains a

Jun 24, 2021
Sort the emails contained in a .csv file into a text file

Go convert csv to txt This snippet of code allows you to sort the emails contained in a .csv file into a text file.

Nov 23, 2021
A simple microservice designed in Go using Echo Microframework for sending emails and/or calendar invitations to users.

Calenvite A simple microservice designed in GO using Echo Microframework for sending emails and/or calendar invitations to users. Features Send emails

Oct 29, 2022
Mail_sender - This library is for sending emails from your mail

Mail Sender This library is for sending emails from your mail Installation mail_

Dec 30, 2021
Golang package for send email. Support keep alive connection, TLS and SSL. Easy for bulk SMTP.

Go Simple Mail The best way to send emails in Go with SMTP Keep Alive and Timeout for Connect and Send. IMPORTANT Examples in this README are for v2.2

Jan 8, 2023
Using Mailchain, blockchain users can now send and receive rich-media HTML messages with attachments via a blockchain address.

Mailchain Introduction Mailchain enables blockchain-based email-like messaging with plain or rich text and attachment capabilities. Using blockchain p

Dec 28, 2022
Simple tool to test SMTP mail send with various settings including TLS1.1 downgrade

smtptest Simple tool to test SMTP mail send with various settings including TLS1.1 downgrade All settings are configurable in the config.yaml file ser

Sep 19, 2022
Send markdown files as MIME-encoded electronic mail.

Send markdown files as MIME-encoded electronic mail.

Aug 9, 2022
Sending emails using email server talking to RabbitMQ and send grid server sending emails to email ids consumed from RabbitMQ
Sending emails using email server talking to RabbitMQ and send grid server sending emails to email ids consumed from RabbitMQ

Sending emails using email server talking to RabbitMQ and send grid server sending emails to email ids consumed from RabbitMQ

Oct 27, 2022
An easy way to send emails with attachments in Go

PROJECT DISCONTINUED This repository only exists for archival purposes. email An easy way to send emails with attachments in Go Install go get github.

Dec 12, 2022
Node is where client will send data to, create block send to miner, create block send to parent and receive tick from validator and do validate

Node Receive Tick from validator Validate POH of tick send result to validator Receive confirm block from validator Send Checked block to validator Ho

Dec 31, 2021
An Alert notification service is an application which can receive alerts from certain alerting systems like System_X and System_Y and send these alerts to developers in the form of SMS and emails.

Alert-System An Alert notification service is an application which can receive alerts from certain alerting systems like System_X and System_Y and sen

Dec 10, 2021
Hxgomail - Gomail - a simple and efficient package to send emails

Gomail Introduction Gomail is a simple and efficient package to send emails. It

Jan 4, 2022
Search and store the best cryptos for the best scalable and modern application development.

Invst Hunt Search and store the best cryptos for the best scalable and modern application development. Layout Creating... Project Challenge The Techni

Nov 12, 2021
Eget is the best way to easily get pre-built binaries for your favorite tools.
Eget is the best way to easily get pre-built binaries for your favorite tools.

Eget: easy pre-built binary installation Eget is the best way to easily get pre-built binaries for your favorite tools. It downloads and extracts pre-

Dec 27, 2022
This small Docker project is the easiest way to send notifications directly via .txt files to services like: Gotify, Telegram, SMTP (Email) or Webhook.
This small Docker project is the easiest way to send notifications directly via .txt files to services like: Gotify, Telegram, SMTP (Email) or Webhook.

This small Docker project is the easiest way to send notifications directly via .txt files to services like: Gotify, Telegram, SMTP (Email) or Webhook.

Oct 5, 2022