1
1
package actions
2
2
3
3
import (
4
+ "crypto/tls"
4
5
"errors"
5
6
"fmt"
6
7
"log"
@@ -19,15 +20,16 @@ var (
19
20
)
20
21
21
22
type EmailAction struct {
22
- Name string
23
- User string
24
- Password string
25
- Host string
26
- Port int
27
- Sender string
28
- Recipients []string
29
- UseMX bool
30
- sendFunc func (addr string , a smtp.Auth , from string , to []string , msg []byte ) error
23
+ Name string
24
+ User string
25
+ Password string
26
+ Host string
27
+ Port int
28
+ Sender string
29
+ Recipients []string
30
+ ClientHostName string
31
+ UseMX bool
32
+ sendFunc func (addr string , a smtp.Auth , from string , to []string , msg []byte ) error
31
33
}
32
34
33
35
func (email * EmailAction ) GetName () string {
@@ -39,8 +41,13 @@ func (email *EmailAction) Init() error {
39
41
if email .Sender == "" {
40
42
email .Sender = email .User
41
43
}
44
+ if email .ClientHostName != "" {
45
+ log .Printf ("Action %q uses a custom client name %q instead of `localhost`" , email .Name , email .ClientHostName )
46
+ email .sendFunc = email .sendEmailWithCustomClient
47
+ } else {
48
+ email .sendFunc = smtp .SendMail
49
+ }
42
50
43
- email .sendFunc = smtp .SendMail
44
51
return nil
45
52
}
46
53
@@ -89,6 +96,56 @@ func (email *EmailAction) Send(content map[string]string) error {
89
96
return nil
90
97
}
91
98
99
+ // sendEmailWithCustomClient replaces smtp.SendMail() in cases
100
+ // where it is necessary to establish a custom client host name instead of "localhost",
101
+ // while keeping the remaining behavior unchanged.
102
+ func (email EmailAction ) sendEmailWithCustomClient (addr string , a smtp.Auth , from string , to []string , msg []byte ) error {
103
+ log .Printf ("Sending an email via Custom client for action %q" , email .Name )
104
+
105
+ c , err := smtp .Dial (addr )
106
+ if err != nil {
107
+ return err
108
+ }
109
+ defer c .Close ()
110
+
111
+ if err := c .Hello (email .ClientHostName ); err != nil {
112
+ return err
113
+ }
114
+
115
+ if ok , _ := c .Extension ("STARTTLS" ); ok {
116
+ config := & tls.Config {ServerName : email .Host }
117
+ if err = c .StartTLS (config ); err != nil {
118
+ return err
119
+ }
120
+ }
121
+ if a != nil {
122
+ if err = c .Auth (a ); err != nil {
123
+ return err
124
+ }
125
+ }
126
+ if err = c .Mail (from ); err != nil {
127
+ return err
128
+ }
129
+ for _ , addr := range to {
130
+ if err = c .Rcpt (addr ); err != nil {
131
+ return err
132
+ }
133
+ }
134
+ w , err := c .Data ()
135
+ if err != nil {
136
+ return err
137
+ }
138
+ _ , err = w .Write (msg )
139
+ if err != nil {
140
+ return err
141
+ }
142
+ err = w .Close ()
143
+ if err != nil {
144
+ return err
145
+ }
146
+ return c .Quit ()
147
+ }
148
+
92
149
func (email EmailAction ) sendViaMxServers (port string , msg string , recipients []string ) {
93
150
for _ , rcpt := range recipients {
94
151
at := strings .LastIndex (rcpt , "@" )
0 commit comments