Sending mime/multipart emails with go

3,817 views
Skip to first unread message

Birol Bilgin

unread,
Aug 24, 2011, 11:04:51 PM8/24/11
to golang-nuts
I would appreciate if someone could provide me with an example, how to
send email with attachments.
I think it can be done with multipart pkg , but I could not figure out
how to use multipart.Writer.

Thanks in advance.

Joshua Poehls

unread,
Jul 30, 2013, 9:36:09 AM7/30/13
to golan...@googlegroups.com, birol...@gmail.com
I'm resurrecting this topic years later because I'm having a heck of a time with Go's mail package as well. I hate complain, but why oh why can't we have a Message type like this included in a core package? Sending an email with both a plain text and HTML body and including an attachment shouldn't be this obtuse. :\

I'm looking around on GoDoc but other than the AppEngine package I haven't seen any appealing way to send email yet.

If anyone has any tips or packages to make this easier I'd love to see them. Once I get this figured out for my app then I'm sure I'll put out my own package.

- Joshua

Henrik Johansson

unread,
Jul 30, 2013, 9:39:49 AM7/30/13
to Joshua Poehls, golang-nuts, birol...@gmail.com
I agree, that would be very convenient!


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

André Paquet

unread,
Jul 30, 2013, 10:56:38 AM7/30/13
to golan...@googlegroups.com, birol...@gmail.com

Henrik Johansson

unread,
Jul 30, 2013, 11:49:50 AM7/30/13
to André Paquet, birol...@gmail.com, golang-nuts

Looks nice, thx!

--

Joshua Poehls

unread,
Jul 30, 2013, 12:44:12 PM7/30/13
to André Paquet, golan...@googlegroups.com, birol...@gmail.com
Thanks André. That package looks like a great example of using a multipart.Writer.

I'm pretty close to understanding how to do both a plain text & HTML body using Content-Type: multipart/alternative but I'm still not sure how to do attachments as well. All of the examples I've seen use either multipart/alternative or multipart/mixed (attachments) but not both.

On the up side, I'm learning more about MIME than I ever wanted to... :)


--
Joshua Poehls


--

Paul Samways

unread,
Jul 30, 2013, 6:07:21 PM7/30/13
to Joshua Poehls, golang-nuts
Hi Joshua,

To send an email with multiple views (Plain/HTML) as well as attachments you need the following MIME layout:

multipart/mixed (multiple independent parts - body & attachments)
    multipart/alternative (multiple alternative parts - plain & html) 
        text/plain
            ...
        text/html
            ...
    image/jpeg (jpeg attachment)
        ...

Paul.

Joshua Poehls

unread,
Jul 30, 2013, 10:57:50 PM7/30/13
to Paul Samways, golang-nuts
Thanks Paul! That actually helps a lot.

I'm a complete noob with Go but I'm making a little headway here. I've pushed the beginnings of my new mail package, gophermail, to GitHub. 

I'd appreciate any early feedback. Like I said, complete noob and MIME ignoramus. As you'll see from the myriad of TODOs I still have a lot of work ahead of me but it builds and the "test" runs. If anyone is interested in helping out on this I'd welcome it.

Thanks,

--
Joshua Poehls

Joshua Poehls

unread,
Jul 31, 2013, 11:56:40 PM7/31/13
to golan...@googlegroups.com
Just in case anyone is following this thread... I just pushed another update to gophermail. There's now a SendMessage() that does the SMTP leg work and my tests with sending through Gmail are successful so far. Cc and Bcc recipients and Plain Text and HTML bodies all supported. ::fingers crossed::

I still need to address some header (specifically Subject) encoding issues and add support for attachments but I think we're really close.

I'm hoping I can find someone who knows more about this SMTP/MIME stuff than I do to do a code review of this when I'm done. I feel like anything I get working could easily be luck and I'd like this package to be fairly bullet proof. Feel free to raise your hand. :)


--
Joshua Poehls
Reply all
Reply to author
Forward
0 new messages