An easy way to send emails with attachments in Go

PROJECT DISCONTINUED

This repository only exists for archival purposes.


email Travis-CI GoDoc Report card

An easy way to send emails with attachments in Go

Install

go get github.com/scorredoira/email

Usage

package email_test

import (
	"log"
	"net/mail"
	"net/smtp"

	"github.com/scorredoira/email"
)

func Example() {
	// compose the message
	m := email.NewMessage("Hi", "this is the body")
	m.From = mail.Address{Name: "From", Address: "[email protected]"}
	m.To = []string{"[email protected]"}

	// add attachments
	if err := m.Attach("email.go"); err != nil {
		log.Fatal(err)
	}

	// add headers
	m.AddHeader("X-CUSTOMER-id", "xxxxx")

	// send it
	auth := smtp.PlainAuth("", "[email protected]", "pwd", "smtp.zoho.com")
	if err := email.Send("smtp.zoho.com:587", auth, m); err != nil {
		log.Fatal(err)
	}
}

Html

	// use the html constructor
	m := email.NewHTMLMessage("Hi", "this is the body")

Inline

	// use Inline to display the attachment inline.
	if err := m.Inline("main.go"); err != nil {
		log.Fatal(err)
	}
Owner
Santiago Corredoira
Developer
Santiago Corredoira
Comments
  • Split long filenames

    Split long filenames

    If the filename is too long, some mail services (like gmx.de) and possibly some mail clients refuse to display the attachment at the moment. It must be split like this:

    Content-Disposition: attachment;
     filename*0="=?UTF-8?B?MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nz";
     filename*1="g5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMj";
     filename*2="M0?="
    
  • Changed 'Date' format from RFC822 to RFC822Z

    Changed 'Date' format from RFC822 to RFC822Z

    Some email clients including Microsoft Outlook on OSX treat the 'Date' field when formatted as RFC822 as +0000, and so add on the local time zone off-set. For users infront of GMT, this causes emails to appear to be recieved in the future. For example, an email sent at 12pm AEST on 2017-07-20, will display in Outlook on OSX as being recieved at 10:00pm 2017-07-20.

    Swapping the date format from RFC822 to RFC822Z (which includes the time zone) produces the expected behaviour in Outlook on OSX, Outlook on Windows, Gmail and Mac Mail.

  • Attachement and Email body

    Attachement and Email body

    I've attempted to send an email containing both body text and an attachment, and the text never seems to show. I can do one or the other, but not both.

  • Fix Tolist format

    Fix Tolist format

    This pull request updates Tolist() to return a list of email address so that smtp.Sendmail can correctly follow the specifications in the SMTP protocol.

    According to section 4.1.1.3 of RFC 5321, the format should be like this:

          RCPT TO:<[email protected]>
    

    As can be seen in lines 356-360 and 266-272 of net/smtp/smtp.go, the to []string list of recipients, is put on to the SMTP stream without any further processing. The relevant line is line 270:

    	_, _, err := c.cmd(25, "RCPT TO:<%s>", to)
    

    Since the package net/smtp does not do any further processing of the list of strings (email addresses) passed, the list passed to email.Send (and thereby, the list passed to smtp.Sendmail), should be a list of plain email addresses, instead of RFC 5322 addresses.

  • Good work! How do I send the attachment as variable instead of a file?

    Good work! How do I send the attachment as variable instead of a file?

    The input is a variable. I don't want to store on disk to be retrieved again. Can you provide an example of using the variable as output immediately?

    var buf []Bytes or something?

  • Support custom headers

    Support custom headers

    Hi,

    i wanted to say thank you for this awesome package!

    In my case i'm required to add custom headers to the message, so i created this small PR

    • implemented Message.AddHeader(key string, value string)
    • implemented header tests

    cheers

  • Attachement is empty

    Attachement is empty

    This might be a long shot, but I was able to use the library to attach an image and send the email, however when I download and open the attachment it says the following

    The file “test.png” could not be opened because it is empty.
    

    Here is my code, I have been debugging for awhile maybe a second set of eye can help me find the error

    func sendMyEmail() {
        m := email.NewMessage("Hi", "this is the body")
        m.From = mail.Address{Name: "From", Address: "[email protected]"}
        m.To = []string{"[email protected]"}
    
        if err := m.Attach("./img_dump/test.png"); err != nil {
            // There is no error here
            log.Fatal(err)
        }
    
        auth := smtp.PlainAuth("", "[email protected]", "mypassword", "smtp.gmail.com")
        if err := email.Send("smtp.gmail.com:587", auth, m); err != nil {
             // There is no error here also
            log.Fatal(err)
        }
    }
    

    Thanks

  • Update README.md

    Update README.md

    The fact that it uses both the identified "email" and "mail" in the example is a bit confusing especially when (like me) you're not aware that there's a net/mail package that needs to be imported.

    So I think it would be cleared to put the complete example in the readme.

    In any case, thank you for this useful package!

  • Add Date field to email header

    Add Date field to email header

    Date field is one of the must exist field, since this field is not set by the library mails will be flagged as having bad header by content filters like Amavis and mail might possibly end up in spam.

  • Inlining of attachments

    Inlining of attachments

    I am using your package for a small project but missed the feature to inline attachments. I know that it is not that well thought out because it is just one use case of inlining. I will clean up when I have more free time.

  • New features: Cc, Bcc options

    New features: Cc, Bcc options

    Added Cc and Bcc options, updated From, To, Cc in Header.

    Updated Readme file with example and Install help.

    Signed-off-by: Aravinda VK [email protected]

  • Only first of the Multiple attachments being sent

    Only first of the Multiple attachments being sent

    I have written a simple wrapper to send an email with multiple attachment:

    		var m *email.Message
    		if mail.Html {
    			m = email.NewHTMLMessage(mail.Subject, mail.Body)
    		} else {
    			m = email.NewMessage(mail.Subject, mail.Body)
    		}
    		m.From.Address = mail.Sender
    		m.From.Name = "Rein Games"
    		m.To = mail.To
    		m.Cc = mail.Cc
    
    		if mail.Attachments != nil && len(mail.Attachments) > 0 {
    			for _, a := range mail.Attachments {
    				if a.Inline {
    					m.Inline(a.FilePath)
    				} else {
    					m.Attach(a.FilePath)
    				}
    			}
    		}
    
    		auth := smtp.PlainAuth("", mail.Sender, conf.Password, "smtp.gmail.com")
    		return email.Send("smtp.gmail.com:587", auth, m)
    

    However, using this code only the first attachment is being sent with the email. The subsequent attachments do not get attached. What am I missing?

    Thanks,

  • Memory Leak

    Memory Leak

    Hi,

    We are using below code for send email and there is some memory leak after every mail sent.

    		mailCompose := email.NewHTMLMessage(strSubject, strMessage)
    		
    		mailCompose.From = mail.Address{Name: strSenderName, Address: strSenderEmail}
    		aryTo := strings.Split(strTo, ";")
    		mailCompose.To = aryTo
    			
    		if err := email.Send(strSMTPURL, auth, mailCompose); err != nil {
    			errorHandler(err,"SendEmail : Error in send email.")
    			return
    		}
    
    		mailCompose = nil
    

    Not able to understand where is the issue. Can you just check is there any issue in my code or library for send email.

    Thanks Sandeep Kumar

  • Add method to use HTML template in HTML emails instead of HTML via string

    Add method to use HTML template in HTML emails instead of HTML via string

    Hi,

    I've been using your library because of the attachment integration. I have email-templates stored as files which need execution. I've added a method which parses a HTML template and sets it to the body, it also forces the content-type to text/html, for these cases.

    Example:

    package email_test
    
    import (
        "log"
        "net/mail"
        "net/smtp"
    
        "github.com/scorredoira/email"
    )
    
    func Example() {
        // compose the message, both NewMessage and NewHTMLMessage work
        m := email.NewMessage("Hi", "")
        m.From = mail.Address{Name: "From", Address: "[email protected]"}
        m.To = []string{"[email protected]"}
    
        // parse html template
        if err := m.HTMLTemplate("template.html", map[string]interface{}{"Test":"string"}); err != nil {
            log.Fatal(err)
        }
    
        // add attachments
        if err := m.Attach("email.go"); err != nil {
            log.Fatal(err)
        }
    
        // send it
        auth := smtp.PlainAuth("", "[email protected]", "pwd", "smtp.zoho.com")
        if err := email.Send("smtp.zoho.com:587", auth, m); err != nil {
            log.Fatal(err)
        }
    }
    

    I hope you like it. I've also optimized the Tolist() function, it now omits the loops.

    Cheers!

    Rik

Related tags
The best way to send emails in Go.

Gomail Introduction Gomail is a simple and efficient package to send emails. It is well tested and documented. Gomail can only send emails using an SM

Dec 29, 2022
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
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
📮 Simple (but useful) email sender written in pure Go v1.17. Support HTML templates and attachments.

?? Go Email Sender Simple (but useful) email sender written in pure Go v1.17. Yes, yet another email package here! ?? Support HTML templates and attac

Dec 31, 2021
: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
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
Easy-to-use distributed multi-function current limiter.

go-limiter Easy-to-use distributed multi-function current limiter. Installation Run the following command under your project: go get -u github.com/NIC

Mar 10, 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
The best way to send emails in Go.

Gomail Introduction Gomail is a simple and efficient package to send emails. It is well tested and documented. Gomail can only send emails using an SM

Dec 29, 2022
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
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
📮 Simple (but useful) email sender written in pure Go v1.17. Support HTML templates and attachments.

?? Go Email Sender Simple (but useful) email sender written in pure Go v1.17. Yes, yet another email package here! ?? Support HTML templates and attac

Dec 31, 2021