| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "net/smtp" |
| 8 | "os" |
| 9 | "runtime" |
| 10 | "strings" |
| 11 | "time" |
| 12 | |
| 13 | "launchpad.net/email-reminder/internal/config" |
| 14 | "launchpad.net/email-reminder/internal/events" |
| 15 | "launchpad.net/email-reminder/internal/util" |
| 16 | ) |
| 17 | |
| 18 | type Notifier interface { |
| 19 | Send(body, subject string, recipients []util.EmailRecipient) []error |
| 20 | SendDebug(now time.Time, event events.Event, when string, failures []error, defaultRecipient util.EmailRecipient) |
| 21 | } |
| 22 | |
| 23 | type SmtpNotifier struct { |
| 24 | Conf config.Config |
| 25 | } |
| 26 | |
| 27 | func (n *SmtpNotifier) Send(body, subject string, recipients []util.EmailRecipient) []error { |
| 28 | var errs []error |
| 29 | |
| 30 | for _, to := range recipients { |
| 31 | fullBody := util.FullBody(body, to, config.VersionNumber) |
| 32 | if err := sendEmail(fullBody, subject, to, n.Conf); err != nil { |
| 33 | if n.Conf.Verbose { |
| 34 | log.Printf("Failed to send reminder to %s: %v", to.NameAndEmail(), err) |
| 35 | } |
| 36 | errs = append(errs, err) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | return errs |
| 41 | } |
| 42 | |
| 43 | func (n *SmtpNotifier) SendDebug(now time.Time, event events.Event, when string, failures []error, defaultRecipient util.EmailRecipient) { |
| 44 | eventName := strings.TrimSpace(event.Name) |
| 45 | if eventName == "" { |
| 46 | eventName = event.String() |
| 47 | } |
| 48 | |
| 49 | occurrenceLine := "Since this event is OCCURRING TODAY, you should really check your reminders manually." |
| 50 | if when != "today" { |
| 51 | occurrenceLine = fmt.Sprintf("Since this event is scheduled for %s, please double-check your reminders manually.", when) |
| 52 | } |
| 53 | |
| 54 | if n.Conf.Verbose { |
| 55 | log.Printf("Sending debug email to %s for %s", defaultRecipient.Email, event) |
| 56 | } |
| 57 | |
| 58 | body := fmt.Sprintf(`WARNING: Due to an error, the email reminder for the event "%s" could not be processed and all I could do was to let you know that there was a problem. |
| 59 | |
| 60 | %s |
| 61 | |
| 62 | Please forward this email to the email-reminder author so that this problem can be fixed in future versions: |
| 63 | |
| 64 | Francois Marier <[email protected]> |
| 65 | |
| 66 | Thanks! |
| 67 | |
| 68 | -------------------------------------------------------------- |
| 69 | %s`, eventName, occurrenceLine, composeDebugInfo(now, event, failures)) |
| 70 | subject := fmt.Sprintf("Email-reminder ERROR: %s", eventName) |
| 71 | |
| 72 | if err := sendEmail(body, subject, defaultRecipient, n.Conf); err != nil { |
| 73 | log.Printf("Could not send debug email for %s: %v", event, err) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func sendEmail(body string, subject string, recipient util.EmailRecipient, conf config.Config) error { |
| 78 | msg := util.AssembleRawMessage(body, subject, recipient.NameAndEmail(), conf.MailFrom, config.VersionNumber) |
| 79 | if conf.Verbose { |
| 80 | log.Println(msg) |
| 81 | } |
| 82 | if conf.Simulate { |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | var auth smtp.Auth |
| 87 | if conf.SmtpUsername != "" && conf.SmtpPassword != "" { |
| 88 | auth = smtp.PlainAuth("", conf.SmtpUsername, conf.SmtpPassword, conf.SmtpServer) |
| 89 | } |
| 90 | if conf.SmtpSsl { |
| 91 | if err := util.SendMailImplicitTLS(conf.SmtpServer, auth, conf.MailFrom, []string{recipient.Email}, []byte(msg)); err != nil { |
| 92 | return fmt.Errorf("could not send '%s' email (TLS) to '%s': %w", subject, recipient.Email, err) |
| 93 | } |
| 94 | } else if err := util.SendMailPlaintext(conf.SmtpServer, auth, conf.MailFrom, []string{recipient.Email}, []byte(msg)); err != nil { |
| 95 | return fmt.Errorf("could not send '%s' email to '%s': %w", subject, recipient.Email, err) |
| 96 | } |
| 97 | return nil |
| 98 | } |
| 99 | |
| 100 | func composeDebugInfo(now time.Time, event events.Event, failures []error) string { |
| 101 | info := []string{ |
| 102 | fmt.Sprintf("Email-reminder: %s", config.VersionNumber), |
| 103 | fmt.Sprintf("Go: %s (%s/%s)", runtime.Version(), runtime.GOOS, runtime.GOARCH), |
| 104 | } |
| 105 | |
| 106 | if hostname, err := os.Hostname(); err == nil && strings.TrimSpace(hostname) != "" { |
| 107 | info = append(info, fmt.Sprintf("Hostname: '%s'", hostname)) |
| 108 | } |
| 109 | |
| 110 | if pathVar, ok := os.LookupEnv("PATH"); ok { |
| 111 | info = append(info, fmt.Sprintf("PATH: '%q'", pathVar)) |
| 112 | } else { |
| 113 | info = append(info, "PATH: <unset>") |
| 114 | } |
| 115 | |
| 116 | if homeVar, ok := os.LookupEnv("HOME"); ok { |
| 117 | info = append(info, fmt.Sprintf("HOME: '%q'", homeVar)) |
| 118 | } else { |
| 119 | info = append(info, "HOME: <unset>") |
| 120 | } |
| 121 | |
| 122 | if output, err := util.SecureExecCommand("/usr/bin/lsb_release", "-s", "-d"); err == nil { |
| 123 | if trimmed := strings.TrimSpace(string(output)); trimmed != "" { |
| 124 | info = append(info, fmt.Sprintf("OS: %s", trimmed)) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if output, err := util.SecureExecCommand("/usr/bin/uname", "-a"); err == nil { |
| 129 | if trimmed := strings.TrimSpace(string(output)); trimmed != "" { |
| 130 | info = append(info, fmt.Sprintf("Kernel: %s", trimmed)) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | info = append(info, fmt.Sprintf("Time: %s", now.Format(time.RFC3339))) |
| 135 | info = append(info, "Error(s):") |
| 136 | |
| 137 | for _, failure := range failures { |
| 138 | info = append(info, fmt.Sprintf("- %v", failure)) |
| 139 | } |
| 140 | |
| 141 | if data, err := json.MarshalIndent(event, "", " "); err == nil { |
| 142 | info = append(info, "Event:") |
| 143 | info = append(info, string(data)) |
| 144 | } else { |
| 145 | info = append(info, fmt.Sprintf("Event: <unable to serialize: %v>", err)) |
| 146 | } |
| 147 | |
| 148 | return strings.Join(info, "\n") |
| 149 | } |