发送电子邮件给多个收件人和附件

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

Go-Mail multiple recipients and attachment

问题

我正在尝试使用go-mail库创建一个电子邮件服务,并且已经使其工作。然而,我无法解决一些挑战。

我有一个用于表单数据的结构体-

type SendMailBody struct {
	EmailTo []string `form:"sendTo"`
	Subject string   `form:"subject"`
	Body    string   `form:"body"`
}

我发送到API的表单数据是

subject: 通知
sendTo:["abc@gmail.com", "xyz@gmail.com"]
body:您已收到通知

挑战-

  1. 如果我在"sendTo"中传递一个单独的电子邮件,它可以正常工作。但是在传递电子邮件列表后,它无法将电子邮件发送给收件人。我该如何解决这个问题?

  2. 如果我通过表单数据传递附件,如何将其附加到邮件中?文档中提到可以这样做:"m.Attach("/tmp/image.jpg")"。但是如果我通过API传递附件,我应该如何操作?

请指导我。

英文:

I am trying to make an email service using go-mail library and made it working. However there are few challenges that i am unable to solve

I have the struct for form data-

type SendMailBody struct {
	EmailTo []string `form:"sendTo"`
	Subject string   `form:"subject"`
	Body    string   `form:"body"`
}

The form data that i am sending to the API is

subject: Notification
sendTo:["abc@gmail.com", "xyz@gmail.com"]
body:You have been notified

Challenges-

  1. If I pass a single email in "sendTO", It is working fine. But after passing the slice of emails, it is unable to send emails to the recepients. How can i make it work?

  2. If I pass the attachment through Form data, how can I attach it with the mail. In documentation, it is mentioned that we can do it like that "m.Attach("/tmp/image.jpg")" . But how should i do it if i pass the attachment via form data in API

Please guide me through that.

答案1

得分: 0

需要一些更详细的信息才能提供帮助。特别是你使用的哪个 go-mail 库?

对于问题 1:

如果你指的是 https://github.com/wneessen/go-mail,使用 Msg.To() 应该可以正常处理多个收件人。请参考文档:https://pkg.go.dev/github.com/wneessen/go-mail#Msg.To

如果你指的是 https://github.com/go-mail/mail,该库中的 Message.SetAddressHeader()(https://pkg.go.dev/github.com/go-mail/mail?utm_source=godoc#Message.SetAddressHeader)不支持多个收件人地址。你需要使用 Message.SetHeaders() 来设置 "To" 头部(https://pkg.go.dev/github.com/go-mail/mail?utm_source=godoc#Message.SetHeaders)。

对于问题 2:

这完全取决于你如何读取附件数据(同样也取决于你使用的 go-mail 库)。https://github.com/wneessen/go-mail 提供了不同的附件和嵌入文件的方式(例如从本地文件、从 embedFS、从 io.Reader...)。

英文:

Some more details are needed to help here. In particular which go-mail are you using?

For 1.)

If you refer to https://github.com/wneessen/go-mail, using Msg.To() should work fine with multiple recipient. See the documentation at: https://pkg.go.dev/github.com/wneessen/go-mail#Msg.To

If you refer to https://github.com/go-mail/mail, there is Message.SetAddressHeader() (https://pkg.go.dev/github.com/go-mail/mail?utm_source=godoc#Message.SetAddressHeader) which does not support multiple recipient addresses. You would need to use Message.SetHeaders() for the "To"-header instead (https://pkg.go.dev/github.com/go-mail/mail?utm_source=godoc#Message.SetHeaders).

For 2.)

This totally depends on how you read the attachment data (and again also on the go-mail library you are using). https://github.com/wneessen/go-mail has different ways of attaching and embedding files (i. e. from a local file, from embedFS, from an io.Reader...)

huangapple
  • 本文由 发表于 2022年9月16日 18:30:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/73743457.html
匿名

发表评论

匿名网友

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

确定