Basic email sending
This commit is contained in:
@ -1,11 +1,17 @@
|
||||
package mail
|
||||
|
||||
const (
|
||||
ContentTypeHTML = "text/html"
|
||||
ContentTypeText = "text/plain"
|
||||
)
|
||||
|
||||
type Option struct {
|
||||
Host string
|
||||
Port int
|
||||
User string
|
||||
Password string
|
||||
InsecureSkipVerify bool
|
||||
UseStartTLS bool
|
||||
}
|
||||
|
||||
type OptionFunc func(*Option)
|
||||
@ -14,6 +20,33 @@ type Mailer struct {
|
||||
opt *Option
|
||||
}
|
||||
|
||||
func NewMailer(funcs ...OptionFunc) *Mailer {
|
||||
return &Mailer{}
|
||||
func WithTLS(useStartTLS, insecureSkipVerify bool) OptionFunc {
|
||||
return func(opt *Option) {
|
||||
opt.UseStartTLS = useStartTLS
|
||||
opt.InsecureSkipVerify = insecureSkipVerify
|
||||
}
|
||||
}
|
||||
|
||||
func WithServer(host string, port int) OptionFunc {
|
||||
return func(opt *Option) {
|
||||
opt.Host = host
|
||||
opt.Port = port
|
||||
}
|
||||
}
|
||||
|
||||
func WithCredentials(user, password string) OptionFunc {
|
||||
return func(opt *Option) {
|
||||
opt.User = user
|
||||
opt.Password = password
|
||||
}
|
||||
}
|
||||
|
||||
func NewMailer(funcs ...OptionFunc) *Mailer {
|
||||
opt := &Option{}
|
||||
|
||||
for _, fn := range funcs {
|
||||
fn(opt)
|
||||
}
|
||||
|
||||
return &Mailer{opt}
|
||||
}
|
||||
|
@ -40,10 +40,14 @@ func WithCharset(charset string) func(*SendOption) {
|
||||
}
|
||||
}
|
||||
|
||||
func WithFrom(address string, name string) func(*SendOption) {
|
||||
func WithSender(address string, name string) func(*SendOption) {
|
||||
return WithAddressHeader("From", address, name)
|
||||
}
|
||||
|
||||
func WithSubject(subject string) func(*SendOption) {
|
||||
return WithHeader("Subject", subject)
|
||||
}
|
||||
|
||||
func WithAddressHeader(field, address, name string) func(*SendOption) {
|
||||
return func(opt *SendOption) {
|
||||
opt.AddressHeaders = append(opt.AddressHeaders, AddressHeader{field, address, name})
|
||||
@ -56,14 +60,32 @@ func WithHeader(field string, values ...string) func(*SendOption) {
|
||||
}
|
||||
}
|
||||
|
||||
func WithRecipients(addresses ...string) func(*SendOption) {
|
||||
return WithHeader("To", addresses...)
|
||||
}
|
||||
|
||||
func WithCopies(addresses ...string) func(*SendOption) {
|
||||
return WithHeader("Cc", addresses...)
|
||||
}
|
||||
|
||||
func WithInvisibleCopies(addresses ...string) func(*SendOption) {
|
||||
return WithHeader("Cci", addresses...)
|
||||
}
|
||||
|
||||
func WithBody(contentType string, content string, setting gomail.PartSetting) func(*SendOption) {
|
||||
return func(opt *SendOption) {
|
||||
if setting == nil {
|
||||
setting = gomail.SetPartEncoding(gomail.Unencoded)
|
||||
}
|
||||
opt.Body = Body{contentType, content, setting}
|
||||
}
|
||||
}
|
||||
|
||||
func WithAlternativeBody(contentType string, content string, setting gomail.PartSetting) func(*SendOption) {
|
||||
return func(opt *SendOption) {
|
||||
if setting == nil {
|
||||
setting = gomail.SetPartEncoding(gomail.Unencoded)
|
||||
}
|
||||
opt.AlternativeBodies = append(opt.AlternativeBodies, Body{contentType, content, setting})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user