Error sending Multipart messages
scorredoira opened this issue · 3 comments
Hi,
I am trying to send an email with attachments but I only receive one part:
- If I add both parts I get only the text without the attachment.
- If I add only part1 I get the text.
- If I add only part2 I get the text attached as "noname".
this is the code:
package main
import (
"bytes"
"errors"
"github.com/sloonz/go-mime-message"
"github.com/sloonz/go-qprintable"
"log"
"net/smtp"
)
func main() {
m := message.NewMultipartMessage("alternative", "")
m.SetHeader("Subject", message.EncodeWord("test"))
m1 := message.NewTextMessage(qprintable.UnixTextEncoding, bytes.NewBufferString("this is the text body."))
m1.SetHeader("Content-Type", "text/plain")
m.AddPart(m1)
m2 := message.NewBinaryMessage(bytes.NewBufferString("this is some text as attachment."))
m2.SetHeader("Content-Type", "application/octet-stream")
m.AddPart(m2)
var buf bytes.Buffer
buf.ReadFrom(m)
erri := smtp.SendMail(
"mail.padelclick.com:25",
smtp.PlainAuth("", "test", "test", "mail.example.com"),
"test@example.com",
[]string{"test@example.com"},
buf.Bytes())
}
With the attached source code, I get a mail with two parts. My MUA show me the text attachment, and I can download the text attachment. This is the source code of the received mail :
Return-Path: <xxx@xxx.xxx>
X-Original-To: xxx@xxx.xxx.xxx
Delivered-To: xxx@xxx.xxx.xxx
Received: <snipped>
Received: <snipped>
MIME-Version: 1.0
Subject: test
Content-Type: multipart/alternative; boundary="==GoMultipartBoundary:0."
Message-Id: <20120619145207.5AEAD3FB0B@xxx.localdomain>
Date: Tue, 19 Jun 2012 16:52:07 +0200 (CEST)
From: xxx@xxx.xxx.xxx
--==GoMultipartBoundary:0.
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain
this is the text body.
--==GoMultipartBoundary:0.
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream
dGhpcyBpcyBzb21lIHRleHQgYXMgYXR0YWNobWVudC4=
--==GoMultipartBoundary:0.--
Which seems correct to me. What you can do :
- Check the source code of your received mail. Check that it is similar to the above one.
- Compare it with a mail you know it works (send it with gmail for example) and compare the differences
My source is the same, but it seems that not all email clients parse in the same way. I am getting different results from Gmail and Thunderbird.
From the tests I have been doing, the problem seems to be NewMultipartMessage("alternative", ""). In order to send it as an attachment you need to set it as NewMultipartMessage("mixed", "")
http://en.wikipedia.org/wiki/MIME
The multipart/alternative subtype indicates that each part is an "alternative" version of the same (or similar) content, each in a different format denoted by its "Content-Type" header.
Multipart/mixed is used for sending files with different "Content-Type" headers inline (or as attachments).
I have uploaded a very basic version that works fine with gmail: https://github.com/scorredoira/email