wneessen / go-mail

📧 Easy to use, yet comprehensive library for sending mails with Go
https://go-mail.dev
MIT License
677 stars 48 forks source link

[Feature Req] Interface that accept To/CC with a single string #152

Closed suntong closed 12 months ago

suntong commented 1 year ago

Is your feature request related to a problem? Please describe.

Please consider adding/allowing passing a single string to To/CC. Currently there is an extra check preventing it:

failed to set To address: failed to parse mail address "a1@gmail.com,a2@gmail.com", mail: expected single address, got ",a2@gmail.com"

When using

if err := m.To("a1@gmail.com,a2@gmail.com"); err != nil {

in https://go-mail.dev/getting-started/introduction/#hl-5-13

Reason being:

Describe the solution you'd like

Adding/allowing passing a single string to To/CC.

I can work on PR for that.

Describe alternatives you've considered

No response

Additional context

No response

wneessen commented 1 year ago

Hi @suntong,

thanks for the feature request. I see your need here and I think it might be a good addition to go-mail. To not break any current code, I suggest something like m.ToFromString() and m.CcFromString(). If you'd like a provide a PR, that'll be appreciated. Otherwise I'll put this request on the todo list for the next release.

suntong commented 1 year ago

Thanks for the confirmation. I'll make a try...

james-d-elliott commented 1 year ago

This could be handled locally fairly easily too:

if err := m.To(strings.Split("a1@gmail.com,a2@gmail.com", ",")...); err != nil {
suntong commented 1 year ago

indeed!

wneessen commented 1 year ago

Thanks for the PR @suntong, but I feel that adding complexity SetAddrHeader should not be the desired solution. As either To(), Cc() and Bcc() already accept slices of strings, @james-d-elliott's solution should probably be preferred by just adding helper methods like:

func (m *Msg) ToFromSimpleString(v string) error {
    return m.To(strings.Split(v)...);
}

or something similar. This let's us stay true with the KISS principle.

suntong commented 12 months ago

Yeah, agree.

suntong commented 12 months ago

should probably be preferred by just adding helper methods like ... Otherwise I'll put this request on the todo list for the next release.

That'd be great, if possible.

Reopen it again, and feel free to close it anytime.