Skip to content

Commit 88e4181

Browse files
authored
Merge pull request #7 from symfonycorp/from-support
Allow to set email sender
2 parents 0020fb5 + d3a5f15 commit 88e4181

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

CHANGELOG

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
1.3.0
22
-----
33

4-
* Add go.mod
4+
* Allow to set email sender
55
* Fix timeout support when the command starts sub-processes
6+
* Add go.mod
67

78
1.2.0
89
-----

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ That's it!
3434
Note that the `MAILTO` environment variable can also be defined globally in
3535
`/etc/crontab`; it supports multiple recipients by separating them with a comma.
3636

37+
You can also customize the email sender by setting the `MAILFROM` environment
38+
variable.
39+
3740
If you need to use "special" shell characters in your command (like `;` or `|`),
3841
don't forget to quote it:
3942

main.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var version = "master"
2020
type request struct {
2121
command []string
2222
emails string
23+
from string
2324
timeout time.Duration
2425
transport string
2526
verbose bool
@@ -43,6 +44,7 @@ func main() {
4344

4445
req := request{
4546
emails: os.Getenv("MAILTO"),
47+
from: os.Getenv("MAILFROM"),
4648
}
4749

4850
var versionf bool
@@ -145,31 +147,37 @@ func (r *result) sendEmail() {
145147
}
146148
}
147149

148-
if transportType == "" {
150+
switch transportType {
151+
default:
149152
fmt.Printf("Unable to find a path for %s\n", r.request.transport)
150153
os.Exit(1)
151-
}
152154

153-
if transportType == "mail" {
155+
case "mail":
154156
for _, email := range emails {
155-
cmd := exec.Command(transportPath, "-s", r.subject(), strings.TrimSpace(email))
157+
args := []string{"-s", r.subject()}
158+
if from := r.request.from; from != "" {
159+
args = append(args, "-a", from)
160+
}
161+
args = append(args, strings.TrimSpace(email))
162+
cmd := exec.Command(transportPath, args...)
156163
cmd.Stdin = r.render()
157164
cmd.Env = os.Environ()
158165
if err := cmd.Run(); err != nil {
159166
fmt.Printf("Could not send email to %s: %s\n", email, err)
160167
os.Exit(1)
161168
}
162169
}
163-
return
164-
}
165170

166-
if transportType == "sendmail" {
171+
case "sendmail":
167172
var message string
168173
if len(emails) > 1 {
169174
message = fmt.Sprintf("To: %s\r\nCc: %s\r\nSubject: %s\r\n\r\n%s", emails[0], strings.Join(emails[1:], ","), r.subject(), r.render().String())
170175
} else {
171176
message = fmt.Sprintf("To: %s\r\nSubject: %s\r\n\r\n%s", emails[0], r.subject(), r.render().String())
172177
}
178+
if from := r.request.from; from != "" {
179+
message = fmt.Sprintf("From: %s\r\n%s", from, message)
180+
}
173181
cmd := exec.Command(transportPath, "-t")
174182
cmd.Stdin = strings.NewReader(message)
175183
cmd.Stdout = os.Stdout
@@ -179,7 +187,6 @@ func (r *result) sendEmail() {
179187
fmt.Printf("Could not send email to %s: %s\n", emails, err)
180188
os.Exit(1)
181189
}
182-
return
183190
}
184191
}
185192

0 commit comments

Comments
 (0)