-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,338 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package tools | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"net" | ||
"os" | ||
"path" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
// UnixSocket returns a path and a FileMode if the address is in | ||
// the format of unix:<path>:<permission> | ||
func UnixSocket(address string) (string, fs.FileMode, bool) { | ||
re := regexp.MustCompile(`^unix:(.*):(\d\d\d\d?)$`) | ||
|
||
var f fs.FileMode | ||
|
||
if !re.MatchString(address) { | ||
return "", f, false | ||
} | ||
|
||
m := re.FindAllStringSubmatch(address, 1) | ||
|
||
modeVal, err := strconv.ParseUint(m[0][2], 8, 32) | ||
|
||
if err != nil { | ||
return "", f, false | ||
} | ||
|
||
return path.Clean(m[0][1]), fs.FileMode(modeVal), true | ||
} | ||
|
||
// PrepareSocket returns an error if an active socket file already exists | ||
func PrepareSocket(address string) error { | ||
address = path.Clean(address) | ||
if _, err := os.Stat(address); os.IsNotExist(err) { | ||
// does not exist, OK | ||
return nil | ||
} | ||
|
||
if _, err := net.Dial("unix", address); err == nil { | ||
// socket is listening | ||
return fmt.Errorf("socket already in use: %s", address) | ||
} | ||
|
||
return os.Remove(address) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Package cmd is a wrapper library to send mail | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/mail" | ||
"net/smtp" | ||
"os" | ||
|
||
"github.com/axllent/mailpit/internal/logger" | ||
) | ||
|
||
// Send is a wrapper for smtp.SendMail() which also supports sending via unix sockets. | ||
// Unix sockets must be set as unix:/path/to/socket | ||
// It does not support authentication. | ||
func Send(addr string, from string, to []string, msg []byte) error { | ||
socketPath, isSocket := socketAddress(addr) | ||
|
||
fromAddress, err := mail.ParseAddress(from) | ||
if err != nil { | ||
return fmt.Errorf("invalid from address: %s", from) | ||
} | ||
|
||
if len(to) == 0 { | ||
return fmt.Errorf("no To addresses specified") | ||
} | ||
|
||
if !isSocket { | ||
return smtp.SendMail(addr, nil, fromAddress.Address, to, msg) | ||
} | ||
|
||
conn, err := net.Dial("unix", socketPath) | ||
if err != nil { | ||
return fmt.Errorf("error connecting to %s", addr) | ||
} | ||
|
||
client, err := smtp.NewClient(conn, "") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Set the sender | ||
if err := client.Mail(fromAddress.Address); err != nil { | ||
fmt.Fprintln(os.Stderr, "error sending mail") | ||
logger.Log().Fatal(err) | ||
} | ||
|
||
// Set the recipient | ||
for _, a := range to { | ||
if err := client.Rcpt(a); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
wc, err := client.Data() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = wc.Write(msg) | ||
if err != nil { | ||
return err | ||
} | ||
err = wc.Close() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.