Search code examples
emailgomultipart

multipart email in go


I'm trying to send multipart-emails using golang, but I can't figure out how to create them. I know there's a multipart package, but there are no example how to use it.

I already tried the library mailyak, but it doesn't work like it should. So, how can I create multipart emails with normal golang smtp/multipart package?

The mail should have a html and a plain-text part.


Solution

  • You may like this package https://github.com/scorredoira/email

    // compose the message
    m := email.NewMessage("Hi", "this is the body")
    m.From = mail.Address{Name: "From", Address: "from@example.com"}
    m.To = []string{"to@example.com"}
    
    // add attachments
    if err := m.Attach("email.go"); err != nil {
        log.Fatal(err)
    }
    
    // send it
    auth := smtp.PlainAuth("", "from@example.com", "pwd", "smtp.zoho.com")
    if err := email.Send("smtp.zoho.com:587", auth, m); err != nil {
        log.Fatal(err)
    }