Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Using Embeded Attachement in buffalo/mail (the wrapper of GoMail) #1040

Closed
kteb opened this issue Apr 21, 2018 · 1 comment
Closed

Using Embeded Attachement in buffalo/mail (the wrapper of GoMail) #1040

kteb opened this issue Apr 21, 2018 · 1 comment
Assignees
Labels
enhancement New feature or request s: in progress Someone is working on this

Comments

@kteb
Copy link
Member

kteb commented Apr 21, 2018

I needed to use message.Embed() in one email that I was sending with two images.

Currently we can only submit message.Attach().

It was very easy to implement it, but I just wanted some recommendation on the signature of the method, for me the more logical way is to make a breaking change, I just wanted the opinion of Mark before committing my solution.

I wanted to replace

func (m *Message) AddAttachment(name, contentType string, r io.Reader) error

by

func (m *Message) AddAttachment(name string, embedded bool, r io.Reader) error

An other option could be to add a new method AddEmbededAttachament

I propose to remove the contentType string as it is never used in the code (or maybe I'm blind, but guru didn't find any usage except from where we assign it a value).

I also found a bug when we where sending more than one file at the time, with the following code.

func (sm SMTPSender) addAttachments(message Message, gm *gomail.Message) {
	for _, at := range message.Attachments {
		settings := gomail.SetCopyFunc(func(w io.Writer) error {
			_, err := io.Copy(w, atReader)
			return err
		})

		gm.Attach(at.Name, settings)
	}
}

I took me a while to debug it, but I saw that inside the returning function it was always the last file that was called with the number of files available. And I remembered then that the returning function where only created at the end of the "mother function" so inside at was always the same value. With this it worked well. So with this small correction it worked well.

func (sm SMTPSender) addAttachments(message Message, gm *gomail.Message) {
	for _, at := range message.Attachments {
		curAt := at
		settings := gomail.SetCopyFunc(func(w io.Writer) error {
			_, err := io.Copy(w, curAt.Reader)
			return err
		})

		gm.Attach(curAt.Name, settings)
	}
}
@kteb kteb mentioned this issue Apr 21, 2018
@stanislas-m stanislas-m added enhancement New feature or request s: in progress Someone is working on this labels Apr 23, 2018
@stanislas-m
Copy link
Member

Fixed with #1041.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request s: in progress Someone is working on this
Projects
None yet
Development

No branches or pull requests

2 participants