Mercurial > gemma
annotate pkg/misc/mail.go @ 5457:fb3e127bccb4 uiimprovements
fixed linting errors
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Thu, 15 Jul 2021 10:11:49 +0200 |
parents | dcd5692a2889 |
children | 1222b777f51f |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
2 // without warranty, see README.md and license for details. |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
6 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
13 |
339
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
14 package misc |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
15 |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
17 "io" |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
18 |
339
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
19 gomail "gopkg.in/gomail.v2" |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
20 |
414
c1047fd04a3a
Moved project specific Go packages to new pkg folder.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
339
diff
changeset
|
21 "gemma.intevation.de/gemma/pkg/config" |
339
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
22 ) |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
23 |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
24 type EmailReceiver struct { |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
25 Name string |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
26 Address string |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
27 } |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
28 |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
29 type EmailAttachment struct { |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
30 Name string |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
31 Content []byte |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
32 } |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
33 |
1371
5b9b8eabcd01
Backend: Added the API documentation of the misc package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
34 // SendMail sends an email to a given address with a given subject |
5b9b8eabcd01
Backend: Added the API documentation of the misc package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
35 // and body. |
5b9b8eabcd01
Backend: Added the API documentation of the misc package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
36 // The credentials to contact the SMPT server are taken from the |
5b9b8eabcd01
Backend: Added the API documentation of the misc package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
37 // configuration. |
5b9b8eabcd01
Backend: Added the API documentation of the misc package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
38 func SendMail(address, subject, body string) error { |
339
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
39 m := gomail.NewMessage() |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
40 m.SetHeader("From", config.MailFrom()) |
1371
5b9b8eabcd01
Backend: Added the API documentation of the misc package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
41 m.SetHeader("To", address) |
339
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
42 m.SetHeader("Subject", subject) |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
43 m.SetBody("text/plain", body) |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
44 |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
45 d := gomail.Dialer{ |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
46 Host: config.MailHost(), |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
47 Port: int(config.MailPort()), |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
48 Username: config.MailUser(), |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
49 Password: config.MailPassword(), |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
50 LocalName: config.MailHelo(), |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
51 SSL: config.MailPort() == 465, |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
52 } |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
53 |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
54 return d.DialAndSend(m) |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
55 } |
5335
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
56 |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
57 func SendMailToAll( |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
58 receivers []EmailReceiver, |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
59 subject string, |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
60 body func(EmailReceiver) (string, error), |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
61 attachments []EmailAttachment, |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
62 errorHandler func(EmailReceiver, error) error, |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
63 ) error { |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
64 |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
65 d := gomail.Dialer{ |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
66 Host: config.MailHost(), |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
67 Port: int(config.MailPort()), |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
68 Username: config.MailUser(), |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
69 Password: config.MailPassword(), |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
70 LocalName: config.MailHelo(), |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
71 SSL: config.MailPort() == 465, |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
72 } |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
73 |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
74 s, err := d.Dial() |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
75 if err != nil { |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
76 return err |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
77 } |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
78 defer s.Close() |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
79 |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
80 m := gomail.NewMessage() |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
81 for _, r := range receivers { |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
82 m.SetHeader("From", config.MailFrom()) |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
83 m.SetAddressHeader("To", r.Address, r.Name) |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
84 m.SetHeader("Subject", subject) |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
85 b, err := body(r) |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
86 if err != nil { |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
87 return err |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
88 } |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
89 m.SetBody("text/plain", b) |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
90 for _, at := range attachments { |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
91 content := at.Content |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
92 m.Attach(at.Name, gomail.SetCopyFunc(func(w io.Writer) error { |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
93 _, err := w.Write(content) |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
94 return err |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
95 })) |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
96 } |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
97 |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
98 if err := gomail.Send(s, m); err != nil { |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
99 if err = errorHandler(r, err); err != nil { |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
100 return err |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
101 } |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
102 } |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
103 m.Reset() |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
104 } |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
105 return nil |
dcd5692a2889
Sending generated reports to receivers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1371
diff
changeset
|
106 } |