From 2727e3386eaa459e489b2916ec84baccda5f8447 Mon Sep 17 00:00:00 2001 From: Jumpy Squirrel Date: Tue, 10 Dec 2024 11:24:34 +0100 Subject: [PATCH] feat(#118): migrate to wneessen/go-mail - Shopify/gomail is abandonware --- go.mod | 8 +-- go.sum | 75 ++++++++++++++++--- internal/service/mailsrv/mailsrv.go | 108 ++++++++++++++++++---------- 3 files changed, 141 insertions(+), 50 deletions(-) diff --git a/go.mod b/go.mod index f3a06dc..aa02864 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/eurofurence/reg-mail-service go 1.19 require ( - github.com/Shopify/gomail v0.0.0-20220729171026-0784ece65e69 github.com/StephanHCB/go-autumn-logging v0.4.0 github.com/StephanHCB/go-autumn-logging-zerolog v0.6.0 github.com/StephanHCB/go-autumn-restclient v0.9.0 @@ -14,6 +13,7 @@ require ( github.com/google/uuid v1.6.0 github.com/rs/zerolog v1.33.0 github.com/stretchr/testify v1.10.0 + github.com/wneessen/go-mail v0.5.2 gopkg.in/yaml.v2 v2.4.0 gorm.io/driver/mysql v1.5.7 gorm.io/gorm v1.25.12 @@ -28,8 +28,8 @@ require ( github.com/mattn/go-isatty v0.0.19 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sony/gobreaker v1.0.0 // indirect - golang.org/x/sys v0.12.0 // indirect - golang.org/x/text v0.14.0 // indirect - gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect + golang.org/x/crypto v0.28.0 // indirect + golang.org/x/sys v0.26.0 // indirect + golang.org/x/text v0.19.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 87f4042..b35b447 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/Shopify/gomail v0.0.0-20220729171026-0784ece65e69 h1:gPoXdwo3sKq8qcfMu/Nc/wkJMLKwe7kaG9Uo8tOj3cU= -github.com/Shopify/gomail v0.0.0-20220729171026-0784ece65e69/go.mod h1:RS+Gaowa0M+gCuiFAiRMGBCMqxLrNA7TESTU/Wbblm8= github.com/StephanHCB/go-autumn-logging v0.4.0 h1:/EC41JJBi1Ao8eFmx4jReokJsbKsRoMoGTaCJZ/Nins= github.com/StephanHCB/go-autumn-logging v0.4.0/go.mod h1:dPABYdECU3XrFib03uXbQFVLftUP5c4YaKSineiw37U= github.com/StephanHCB/go-autumn-logging-zerolog v0.6.0 h1:ljwPUnCVB/qIjeqPWb5+OICC272C1GLshYF5Jdj6A5g= @@ -21,6 +19,7 @@ github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9 github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo= github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= @@ -44,18 +43,78 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/wneessen/go-mail v0.5.2 h1:MZKwgHJoRboLJ+EHMLuHpZc95wo+u1xViL/4XSswDT8= +github.com/wneessen/go-mail v0.5.2/go.mod h1:kRroJvEq2hOSEPFRiKjN7Csrz0G1w+RpiGR3b6yo+Ck= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= +golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= +golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= +golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= +golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= +golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= -gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= +golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/mail.v2 v2.3.1 h1:WYFn/oANrAGP2C0dcV6/pbkPzv8yGzqTjPmTeO7qoXk= -gopkg.in/mail.v2 v2.3.1/go.mod h1:htwXN1Qh09vZJ1NVKxQqHPBaCBbzKhp5GzuJEA4VJWw= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/internal/service/mailsrv/mailsrv.go b/internal/service/mailsrv/mailsrv.go index a414181..16e17c1 100644 --- a/internal/service/mailsrv/mailsrv.go +++ b/internal/service/mailsrv/mailsrv.go @@ -5,11 +5,11 @@ import ( "crypto/rand" "encoding/hex" "fmt" - "github.com/Shopify/gomail" aulogging "github.com/StephanHCB/go-autumn-logging" "github.com/eurofurence/reg-mail-service/internal/api/v1/mail" "github.com/eurofurence/reg-mail-service/internal/entity" "github.com/eurofurence/reg-mail-service/internal/repository/config" + gomail "github.com/wneessen/go-mail" "strings" "time" ) @@ -19,76 +19,108 @@ type MailServiceImplData struct { func (s *MailServiceImplData) SendMail(ctx context.Context, dto mail.MailSendDto, template entity.Template, body string) error { // Create a new message and set sender - m := gomail.NewMessage() - m.SetHeader("From", config.EmailFrom()) + m := gomail.NewMsg() + aulogging.Logger.Ctx(ctx).Info().Printf("Preparing to send mail with template (%s/%s)...", dto.CommonID, dto.Lang) + + aulogging.Logger.Ctx(ctx).Info().Printf("From: %s", config.EmailFrom()) + if err := m.From(config.EmailFrom()); err != nil { + aulogging.Logger.Ctx(ctx).Warn().WithErr(err).Printf("failed to set From: address '%s': %s", config.EmailFrom(), err.Error()) + return err + } if config.MessageIdDomain() != "" { - m.SetHeader("Message-ID", generateMessageId(ctx, config.MessageIdDomain())) + msgId := generateMessageId(ctx, config.MessageIdDomain()) + aulogging.Logger.Ctx(ctx).Info().Printf("Message-ID: %s", msgId) + m.SetMessageIDWithValue(msgId) } // Avoid getting auto-response emails and triggering spam filters - m.SetHeader("Precedence", "bulk") + m.SetGenHeader("Precedence", "bulk") + aulogging.Logger.Ctx(ctx).Info().Print("Precedence: bulk") + + // add extra Bcc if configured + if config.AddAutoBcc() != "" { + dto.Bcc = append(dto.Bcc, config.AddAutoBcc()) + } // Set recipients & Subject if config.MailDevMode() { - m.SetHeader("To", config.MailDevMails()...) - m.SetHeader("Subject", fmt.Sprintf("[regtest] %s", template.Subject)) - } else { - m.SetHeader("To", dto.To...) - m.SetHeader("Subject", template.Subject) - + aulogging.Logger.Ctx(ctx).Info().Printf("To: %s", strings.Join(config.MailDevMails(), ", ")) + // only log original To: + aulogging.Logger.Ctx(ctx).Info().Printf("Original-To: %s", strings.Join(dto.To, ", ")) + if err := m.To(config.MailDevMails()...); err != nil { + aulogging.Logger.Ctx(ctx).Warn().WithErr(err).Printf("failed to set To: addresses '%v': %s", config.MailDevMails(), err.Error()) + return err + } + aulogging.Logger.Ctx(ctx).Info().Printf("Subject: [regtest] %s", template.Subject) + m.Subject(fmt.Sprintf("[regtest] %s", template.Subject)) if len(dto.Cc) > 0 { - m.SetHeader("Cc", dto.Cc...) + // only log + aulogging.Logger.Ctx(ctx).Info().Printf("Original-Cc: %s", strings.Join(dto.Cc, ", ")) + } + if len(dto.Bcc) > 0 { + // only log + aulogging.Logger.Ctx(ctx).Info().Printf("Original-Bcc: %s", strings.Join(dto.Bcc, ", ")) } + } else { + if len(dto.To) > 0 { + aulogging.Logger.Ctx(ctx).Info().Printf("To: %s", strings.Join(dto.To, ", ")) + if err := m.To(dto.To...); err != nil { + aulogging.Logger.Ctx(ctx).Warn().WithErr(err).Printf("failed to set To: addresses: %s", err.Error()) + return err + } + } + aulogging.Logger.Ctx(ctx).Info().Printf("Subject: %s", template.Subject) + m.Subject(template.Subject) - // add extra Bcc if configured - if config.AddAutoBcc() != "" { - dto.Bcc = append(dto.Bcc, config.AddAutoBcc()) + if len(dto.Cc) > 0 { + aulogging.Logger.Ctx(ctx).Info().Printf("Cc: %s", strings.Join(dto.Cc, ", ")) + if err := m.Cc(dto.Cc...); err != nil { + aulogging.Logger.Ctx(ctx).Warn().WithErr(err).Printf("failed to set Cc: addresses '%v': %s", dto.Cc, err.Error()) + return err + } } if len(dto.Bcc) > 0 { - m.SetHeader("Bcc", dto.Bcc...) + aulogging.Logger.Ctx(ctx).Info().Printf("Bcc: %s", strings.Join(dto.Bcc, ", ")) + if err := m.Bcc(dto.Bcc...); err != nil { + aulogging.Logger.Ctx(ctx).Warn().WithErr(err).Printf("failed to set Bcc: addresses '%v': %s", dto.Bcc, err.Error()) + return err + } } } // Set Body - m.SetBody("text/plain", body) + m.SetBodyString(gomail.TypeTextPlain, body) // Send E-Mail err := error(nil) if !config.MailLogOnly() { - d := &gomail.Dialer{Host: config.SmtpHost(), Port: config.SmtpPort()} - + var opts []gomail.Option + opts = append(opts, gomail.WithPort(config.SmtpPort())) + authIs := "disabled" if len(config.EmailFromPassword()) > 0 { - d.Username = config.EmailFrom() - d.Password = config.EmailFromPassword() + opts = append(opts, gomail.WithSMTPAuth(gomail.SMTPAuthPlain), gomail.WithUsername(config.EmailFrom()), gomail.WithPassword(config.EmailFromPassword())) + authIs = "enabled" } - err = d.DialAndSend(m) + client, err := gomail.NewClient(config.SmtpHost(), opts...) + if err != nil { + aulogging.Logger.Ctx(ctx).Warn().WithErr(err).Printf("failed to create email client: smtp host '%s', auth %s: %s", config.SmtpHost(), authIs, err.Error()) + return err + } + if err := client.DialAndSend(m); err != nil { + aulogging.Logger.Ctx(ctx).Warn().WithErr(err).Printf("failed to send email: %s", err.Error()) + return err + } aulogging.Logger.Ctx(ctx).Info().Printf("Mail with template (%s/%s) sent.", dto.CommonID, dto.Lang) - logTargets(ctx, dto) - aulogging.Logger.Ctx(ctx).Info().Printf("Subject: %s", template.Subject) } else { aulogging.Logger.Ctx(ctx).Info().Printf("Mail body with template (%s/%s) logged below (**not** sent).", dto.CommonID, dto.Lang) - logTargets(ctx, dto) - aulogging.Logger.Ctx(ctx).Info().Printf("Subject: %s", template.Subject) aulogging.Logger.Ctx(ctx).Info().Printf(body) } return err } -func logTargets(ctx context.Context, dto mail.MailSendDto) { - if len(dto.To) > 0 { - aulogging.Logger.Ctx(ctx).Info().Printf("To: %s", strings.Join(dto.To, ", ")) - } - if len(dto.Cc) > 0 { - aulogging.Logger.Ctx(ctx).Info().Printf("Cc: %s", strings.Join(dto.Cc, ", ")) - } - if len(dto.Bcc) > 0 { - aulogging.Logger.Ctx(ctx).Info().Printf("Bcc: %s", strings.Join(dto.Bcc, ", ")) - } -} - const messageIdTimestampFormat = "20060102150405.000" var fallbackToken uint8 = 0