Skip to content

Commit ba3603d

Browse files
authored
fix: send email with local name via custom client (#607)
* fix: send email with local name via custom client * remove unneeded (duplicated) checks * add `local-name` to a config file * remove unneeded `return` * change logs * refactor: using smtp.Dial() * add comments for the new fuction * update: change a field name
1 parent 4c539f2 commit ba3603d

File tree

4 files changed

+78
-18
lines changed

4 files changed

+78
-18
lines changed

actions/email.go

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package actions
22

33
import (
4+
"crypto/tls"
45
"errors"
56
"fmt"
67
"log"
@@ -19,15 +20,16 @@ var (
1920
)
2021

2122
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
3133
}
3234

3335
func (email *EmailAction) GetName() string {
@@ -39,8 +41,13 @@ func (email *EmailAction) Init() error {
3941
if email.Sender == "" {
4042
email.Sender = email.User
4143
}
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+
}
4250

43-
email.sendFunc = smtp.SendMail
4451
return nil
4552
}
4653

@@ -89,6 +96,56 @@ func (email *EmailAction) Send(content map[string]string) error {
8996
return nil
9097
}
9198

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+
92149
func (email EmailAction) sendViaMxServers(port string, msg string, recipients []string) {
93150
for _, rcpt := range recipients {
94151
at := strings.LastIndex(rcpt, "@")

cfg.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ actions:
9696
host: # Mandatory: SMTP host name (e.g. smtp.gmail.com)
9797
port: # Mandatory: SMTP server port (e.g. 587)
9898
sender: # Mandatory: The email address to use as a sender
99+
client-host-name: # Optional: setting the local client name instead of `localhost`
99100
recipients: ["", ""] # Mandatory: comma separated list of recipients
100101

101102
- name: my-email-smtp-server

router/builders.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ func buildSlackAction(sourceSettings *ActionSettings, aqua string) *actions.Slac
6464

6565
func buildEmailAction(sourceSettings *ActionSettings) *actions.EmailAction {
6666
return &actions.EmailAction{
67-
Name: sourceSettings.Name,
68-
User: sourceSettings.User,
69-
Password: sourceSettings.Password,
70-
Host: sourceSettings.Host,
71-
Port: sourceSettings.Port,
72-
Sender: sourceSettings.Sender,
73-
Recipients: sourceSettings.Recipients,
74-
UseMX: sourceSettings.UseMX,
67+
Name: sourceSettings.Name,
68+
User: sourceSettings.User,
69+
Password: sourceSettings.Password,
70+
Host: sourceSettings.Host,
71+
Port: sourceSettings.Port,
72+
Sender: sourceSettings.Sender,
73+
Recipients: sourceSettings.Recipients,
74+
ClientHostName: sourceSettings.ClientHostName,
75+
UseMX: sourceSettings.UseMX,
7576
}
7677
}
7778
func buildNexusIqAction(sourceSettings *ActionSettings) *actions.NexusIqAction {

router/integrations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type ActionSettings struct {
2525
Recipients []string `json:"recipients,omitempty"`
2626
Sender string `json:"sender,omitempty"`
2727
Token string `json:"token,omitempty"`
28+
ClientHostName string `json:"client-host-name,omitempty"`
2829
UseMX bool `json:"use-mx,omitempty"`
2930
InstanceName string `json:"instance,omitempty"`
3031
SizeLimit int `json:"size-limit,omitempty"`

0 commit comments

Comments
 (0)