go-gomail / gomail

The best way to send emails in Go.
MIT License
4.37k stars 577 forks source link

Should Message be an interface? #5

Closed hobeone closed 10 years ago

hobeone commented 10 years ago

It seems like it might be a good idea to make Message an interface in gomail rather than a concrete struct. This would allow users of the library to use the sending functionality separately from the message creation functionality.

I'm was looking at switching over my rss mailer (https://github.com/hobeone/rss2go) to use gomail and ran into this. I could rework my mailer to use gomail.Message directly but I thought that maybe this pointed out a way to make gomail better.

alexcesaro commented 10 years ago

What kind of function do you need exactly?

The previous version of Gomail exposed a Send function. Is that the kind of function you need?

hobeone commented 10 years ago

I had wrapped up gomail.Message in my own struct:

type Message struct {
  gomail.Message
}

to allow for validation of the message before sending (as I mention in issue #6). I can't pass this to gomail.Mailer.Send() directly as it's not the right type. I thought an interface might be better here as all the Mailer needs is something it can call Export() on. (or maybe just pass in a *mail.Message directly?)

alexcesaro commented 10 years ago

Have you tried something like that?

package main

import "fmt"

type Message struct{}

type MessageWrapper struct {
    *Message
}

func main() {
    m := new(MessageWrapper)
    Send(m.Message)
}

func Send(m *Message) {
    fmt.Println("Message sent!")
}

http://play.golang.org/p/Pzf3_s2TIv

hobeone commented 10 years ago

Yeah this totally works. I guess my question was more if it might make some use cases cleaner to have it be an Interface.