英文:
How to send an email with attachments in Go
问题
我发现了这个库,并成功地在一封空邮件中发送了附件,但无法将文本和附件组合在一起。
https://github.com/sloonz/go-mime-message
如何实现这个功能?
英文:
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?
答案1
得分: 24
我自己最终实现了它:https://github.com/scorredoira/email
使用非常简单:
m := email.NewMessage("Hi", "this is the body")
m.From = "from@example.com"
m.To = []string{"to@example.com"}
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)
英文:
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 = "from@example.com"
m.To = []string{"to@example.com"}
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)
答案2
得分: 18
我为此目的创建了gomail。它支持附件、多部分邮件和非ASCII字符的编码。它有很好的文档和测试。
这是一个示例:
package main
func main() {
m := gomail.NewMessage()
m.SetHeader("From", "alex@example.com")
m.SetHeader("To", "bob@example.com", "cora@example.com")
m.SetAddressHeader("Cc", "dan@example.com", "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")
// 将邮件发送给Bob、Cora和Dan。
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}
英文:
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", "alex@example.com")
m.SetHeader("To", "bob@example.com", "cora@example.com")
m.SetAddressHeader("Cc", "dan@example.com", "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)
}
}
答案3
得分: 12
我更喜欢使用https://github.com/jordan-wright/email来处理电子邮件。
它支持附件。
> 人性化的电子邮件
>
> 电子邮件包被设计成简单易用,但又足够灵活,不会限制使用。目标是为人类提供电子邮件接口。
>
> 电子邮件包目前支持以下功能:
>
- 发件人、收件人、密送和抄送字段
- 电子邮件地址可以使用"test@example.com"和"First Last <test@example.com>"格式
- 文本和HTML消息正文
- 附件
- 阅读回执
- 自定义头部
- 更多功能即将推出!
英文:
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 "test@example.com" and "First Last <test@example.com>" format
- Text and HTML Message Body
- Attachments
- Read Receipts
- Custom headers
- More to come!
答案4
得分: 3
在SMTP协议中,附件是使用多部分MIME消息发送的。
所以我建议你简单地:
- 创建一个
MultipartMessage
- 将你的文本作为第一部分设置为
TextMessage
(使用"Content-Type", "text/plain"
) - 使用
AddPart
将你的附件添加为部分。
英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论