如何在Go中发送带附件的电子邮件

huangapple go评论92阅读模式
英文:

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(&quot;From&quot;, &quot;alex@example.com&quot;)
	m.SetHeader(&quot;To&quot;, &quot;bob@example.com&quot;, &quot;cora@example.com&quot;)
	m.SetAddressHeader(&quot;Cc&quot;, &quot;dan@example.com&quot;, &quot;Dan&quot;)
	m.SetHeader(&quot;Subject&quot;, &quot;Hello!&quot;)
	m.SetBody(&quot;text/html&quot;, &quot;Hello &lt;b&gt;Bob&lt;/b&gt; and &lt;i&gt;Cora&lt;/i&gt;!&quot;)
	m.Attach(&quot;/home/Alex/lolcat.jpg&quot;)

	d := gomail.NewPlainDialer(&quot;smtp.example.com&quot;, 587, &quot;user&quot;, &quot;123456&quot;)

	// 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(使用&quot;Content-Type&quot;, &quot;text/plain&quot;
  • 使用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 &quot;Content-Type&quot;, &quot;text/plain&quot;)

  • add your attachements as parts using AddPart.

huangapple
  • 本文由 发表于 2012年6月18日 08:01:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/11075749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定