github.com/triarius/goreleaser@v1.12.5/internal/pipe/smtp/smtp.go (about) 1 package smtp 2 3 import ( 4 "crypto/tls" 5 "fmt" 6 7 "github.com/caarlos0/env/v6" 8 "github.com/caarlos0/log" 9 "github.com/triarius/goreleaser/internal/tmpl" 10 "github.com/triarius/goreleaser/pkg/context" 11 gomail "gopkg.in/mail.v2" 12 ) 13 14 const ( 15 defaultSubjectTemplate = `{{ .ProjectName }} {{ .Tag }} is out!` 16 defaultBodyTemplate = `You can view details from: {{ .ReleaseURL }}` 17 ) 18 19 type Pipe struct{} 20 21 func (Pipe) String() string { return "smtp" } 22 func (Pipe) Skip(ctx *context.Context) bool { return !ctx.Config.Announce.SMTP.Enabled } 23 24 type Config struct { 25 Host string `env:"SMTP_HOST,notEmpty"` 26 Port int `env:"SMTP_PORT,notEmpty"` 27 Username string `env:"SMTP_USERNAME,notEmpty"` 28 Password string `env:"SMTP_PASSWORD,notEmpty"` 29 } 30 31 func (Pipe) Default(ctx *context.Context) error { 32 if ctx.Config.Announce.SMTP.BodyTemplate == "" { 33 ctx.Config.Announce.SMTP.BodyTemplate = defaultBodyTemplate 34 } 35 36 if ctx.Config.Announce.SMTP.SubjectTemplate == "" { 37 ctx.Config.Announce.SMTP.SubjectTemplate = defaultSubjectTemplate 38 } 39 40 return nil 41 } 42 43 func (Pipe) Announce(ctx *context.Context) error { 44 subject, err := tmpl.New(ctx).Apply(ctx.Config.Announce.SMTP.SubjectTemplate) 45 if err != nil { 46 return fmt.Errorf("announce: failed to announce to SMTP: %w", err) 47 } 48 49 body, err := tmpl.New(ctx).Apply(ctx.Config.Announce.SMTP.BodyTemplate) 50 if err != nil { 51 return fmt.Errorf("announce: failed to announce to SMTP: %w", err) 52 } 53 54 m := gomail.NewMessage() 55 56 // Set E-Mail sender 57 m.SetHeader("From", ctx.Config.Announce.SMTP.From) 58 59 // Set E-Mail receivers 60 receivers := ctx.Config.Announce.SMTP.To 61 m.SetHeader("To", receivers...) 62 63 // Set E-Mail subject 64 m.SetHeader("Subject", subject) 65 66 // Set E-Mail body. You can set plain text or html with text/html 67 m.SetBody("text/plain", body) 68 69 var cfg Config 70 if err := env.Parse(&cfg); err != nil { 71 return fmt.Errorf("announce: failed to announce to SMTP: %w", err) 72 } 73 74 // Settings for SMTP server 75 d := gomail.NewDialer(cfg.Host, cfg.Port, cfg.Username, cfg.Password) 76 77 // This is only needed when SSL/TLS certificate is not valid on server. 78 // In production this should be set to false. 79 d.TLSConfig = &tls.Config{InsecureSkipVerify: ctx.Config.Announce.SMTP.InsecureSkipVerify} 80 81 // Now send E-Mail 82 if err := d.DialAndSend(m); err != nil { 83 return fmt.Errorf("announce: failed to announce to SMTP: %w", err) 84 } 85 86 log.Infof("announce: The mail has been send from %s to %s\n", ctx.Config.Announce.SMTP.From, receivers) 87 88 return nil 89 }