0

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.

Krawly
  • 67
  • 6

1 Answers1

0

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...)

Winni
  • 16
  • 1
  • Hi @Winni , I have already solved the first problem. Now I am stuck on second problem. The library i am using is [link](https://pkg.go.dev/gopkg.in/gomail.v2) and it has one way to add attachment to the body. But now I am implementing an API which will take the attachment from the request body and embed the attachment in the mail. That is the problem where i am stuck at. Is there any way to read the attachment data from body and embed it into mail? – Krawly Oct 04 '22 at 09:26
  • Hi @Krawly, as you say, the go-mail library you are using has only one way to attach files via [Message.Attach](https://pkg.go.dev/gopkg.in/gomail.v2#Message.Attach). This requires you to take the attachment you get from the form body and store it in a temporary file and the access the file with `Message.Attach`. Since the go-mail library seems to be not maintained anymore, I recommend to switch to this [go-mail](https://github.com/wneessen/go-mail) since it's much more flexible and would allow you to access the file directly via `io.Reader` interface (given your API has that) – Winni Oct 05 '22 at 11:18