Feature: Inject/update Bcc header for missing addresses when SMTP recipients do not match messsage headers

In order to capture Bcc recipients from some platfoms (eg: Laravel) when the SMTP recipients contain Bcc recipients but are not listed in the message headers, the missing addresses are now added into the message Bcc header. If the Bcc header does not exist then it is created.

Resolves #35
This commit is contained in:
Ralph Slooten
2023-04-15 11:34:31 +12:00
parent 7c329b56f8
commit 9c8329a05c
3 changed files with 126 additions and 23 deletions

View File

@@ -1,3 +1,4 @@
// Package cmd is the sendmail cli
package cmd
/**
@@ -13,10 +14,16 @@ import (
"os"
"os/user"
"github.com/axllent/mailpit/config"
"github.com/axllent/mailpit/utils/logger"
flag "github.com/spf13/pflag"
)
var (
// Verbose flag
Verbose bool
)
// Run the Mailpit sendmail replacement.
func Run() {
host, err := os.Hostname()
@@ -42,24 +49,31 @@ func Run() {
fromAddr = os.Getenv("MP_SENDMAIL_FROM")
}
var verbose bool
// override defaults from cli flags
flag.StringVarP(&fromAddr, "from", "f", fromAddr, "SMTP sender")
flag.StringVar(&smtpAddr, "smtp-addr", smtpAddr, "SMTP server address")
flag.BoolVarP(&verbose, "verbose", "v", false, "Verbose mode (sends debug output to stderr)")
flag.BoolVarP(&Verbose, "verbose", "v", false, "Verbose mode (sends debug output to stderr)")
flag.BoolP("long-b", "b", false, "Ignored. This flag exists for sendmail compatibility.")
flag.BoolP("long-i", "i", false, "Ignored. This flag exists for sendmail compatibility.")
flag.BoolP("long-o", "o", false, "Ignored. This flag exists for sendmail compatibility.")
flag.BoolP("long-s", "s", false, "Ignored. This flag exists for sendmail compatibility.")
flag.BoolP("long-t", "t", false, "Ignored. This flag exists for sendmail compatibility.")
flag.CommandLine.SortFlags = false
// set the default help
flag.Usage = func() {
fmt.Printf("A sendmail command replacement for Mailpit (%s).\n\n", config.Version)
fmt.Printf("Usage:\n %s [flags] [recipients]\n", os.Args[0])
fmt.Println("\nFlags:")
flag.PrintDefaults()
}
flag.Parse()
// allow recipient to be passed as an argument
recip = flag.Args()
if verbose {
if Verbose {
fmt.Fprintln(os.Stderr, smtpAddr, fromAddr)
}
@@ -75,13 +89,30 @@ func Run() {
os.Exit(11)
}
if len(recip) == 0 {
// We only need to parse the message to get a recipient if none where
// provided on the command line.
recip = append(recip, msg.Header.Get("To"))
addresses := []string{}
if len(recip) > 0 {
addresses = recip
} else {
// get all recipients in To, Cc and Bcc
if to, err := msg.Header.AddressList("To"); err == nil {
for _, a := range to {
addresses = append(addresses, a.Address)
}
}
if cc, err := msg.Header.AddressList("Cc"); err == nil {
for _, a := range cc {
addresses = append(addresses, a.Address)
}
}
if bcc, err := msg.Header.AddressList("Bcc"); err == nil {
for _, a := range bcc {
addresses = append(addresses, a.Address)
}
}
}
err = smtp.SendMail(smtpAddr, nil, fromAddr, recip, body)
err = smtp.SendMail(smtpAddr, nil, fromAddr, addresses, body)
if err != nil {
fmt.Fprintln(os.Stderr, "error sending mail")
logger.Log().Fatal(err)