How to send an email with attachments in Go
Asked Answered
G

5

21

I have found this library and have managed to send an attachment in an empty email but not to combine text and attachments.

https://github.com/sloonz/go-mime-message

How can it be done?

Gombach answered 18/6, 2012 at 0:1 Comment(0)
P
19

I created gomail for this purpose. It supports attachments as well as multipart emails and encoding of non-ASCII characters. It is well documented and tested.

Here is an example:

package main

func main() {
    m := gomail.NewMessage()
    m.SetHeader("From", "[email protected]")
    m.SetHeader("To", "[email protected]", "[email protected]")
    m.SetAddressHeader("Cc", "[email protected]", "Dan")
    m.SetHeader("Subject", "Hello!")
    m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
    m.Attach("/home/Alex/lolcat.jpg")

    d := gomail.NewPlainDialer("smtp.example.com", 587, "user", "123456")

    // Send the email to Bob, Cora and Dan.
    if err := d.DialAndSend(m); err != nil {
        panic(err)
    }
}
Protuberancy answered 26/6, 2014 at 11:28 Comment(4)
how do i attach a file from a URL?Alforja
@Alforja To send a url you either download the file from the URL and send it as attachement, or you send the URL as a html encoded string.Fawcette
gomail appears unmaintained in 2021. If you find a need to attach a file using io.Reader, this fork implements the necessary method: github.com/go-mail/mail.Abecedarian
2023 the most active repo for this seems to be github.com/wneessen/go-mail. But it is frustrating that such an important problem does not have the solution in Go. (In particular, not in the stdlib.)Kuopio
G
23

I ended up implementing it myself: https://github.com/scorredoira/email

Usage is very simple:

m := email.NewMessage("Hi", "this is the body")
m.From = "[email protected]"
m.To = []string{"[email protected]"}

err := m.Attach("picture.png")
if err != nil {
    log.Println(err)
}

err = email.Send("smtp.gmail.com:587", smtp.PlainAuth("", "user", "password", "smtp.gmail.com"), m)
Gombach answered 19/6, 2012 at 18:38 Comment(5)
Gmail does not allow this any longer.Eduction
@qed, I've just tested with Gmail and it works fine.Bast
@Eduction you need to go into your google account settings and under apps / security enable insecure app access. I highly recommend a throw-away email account for this purpose. If you register for gmail and you're under a certain age - I think a 16 or 18 - you don't have to provide a previous email address when registering.Donets
The m.From line does not work anymore because m.From only takes an mail.Address. First there has to be a mail.ParseAddress("[email protected]") to return a *mail.Address, then it has to be dereferenced to be given to m.From.Strap
It seems that the project has been deprecated.Fyke
P
19

I created gomail for this purpose. It supports attachments as well as multipart emails and encoding of non-ASCII characters. It is well documented and tested.

Here is an example:

package main

func main() {
    m := gomail.NewMessage()
    m.SetHeader("From", "[email protected]")
    m.SetHeader("To", "[email protected]", "[email protected]")
    m.SetAddressHeader("Cc", "[email protected]", "Dan")
    m.SetHeader("Subject", "Hello!")
    m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
    m.Attach("/home/Alex/lolcat.jpg")

    d := gomail.NewPlainDialer("smtp.example.com", 587, "user", "123456")

    // Send the email to Bob, Cora and Dan.
    if err := d.DialAndSend(m); err != nil {
        panic(err)
    }
}
Protuberancy answered 26/6, 2014 at 11:28 Comment(4)
how do i attach a file from a URL?Alforja
@Alforja To send a url you either download the file from the URL and send it as attachement, or you send the URL as a html encoded string.Fawcette
gomail appears unmaintained in 2021. If you find a need to attach a file using io.Reader, this fork implements the necessary method: github.com/go-mail/mail.Abecedarian
2023 the most active repo for this seems to be github.com/wneessen/go-mail. But it is frustrating that such an important problem does not have the solution in Go. (In particular, not in the stdlib.)Kuopio
D
12

I prefer to use https://github.com/jordan-wright/email for email purposes. It supports attachments.

Email for humans

The email package is designed to be simple to use, but flexible enough so as not to be restrictive. The goal is to provide an email interface for humans.

The email package currently supports the following:

  • From, To, Bcc, and Cc fields
  • Email addresses in both "[email protected]" and "First Last " format
  • Text and HTML Message Body
  • Attachments
  • Read Receipts
  • Custom headers
  • More to come!
Doughboy answered 13/5, 2015 at 9:24 Comment(0)
K
3

Attachements in the SMTP protocol are sent using a Multipart MIME message.

So I suggest you simply

  • create a MultipartMessage

  • set your text in the fist part as a TextMessage (with "Content-Type", "text/plain")

  • add your attachements as parts using AddPart.

Kailey answered 18/6, 2012 at 6:26 Comment(2)
That is what I'm trying but for some reason I can't get it to work. I created an issue explaining the details here: github.com/sloonz/go-mime-message/issues/1Gombach
Can you please tell me which library you are referring?.Raffin
A
0

If you don't want a library where you'd still have to write your own Golang code, but just a (CLI) app, I made this one: https://github.com/pepa65/mailer

Alegar answered 13/9, 2023 at 1:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.