Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add envelope from to send mail #17

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import (

// EmailConfig is email setting struct
type EmailConfig struct {
Username string
Password string
Host string
Port string
Sender string
Receivers []string // Can use comma for multiple email
ErrorObj error
Expandos *Expandos // can modify mail subject and content on demand
Username string
Password string
Host string
Port string
Sender string
EnvelopeFrom string
Receivers []string // Can use comma for multiple email
Subject string
ErrorObj error
Expandos *Expandos // can modify mail subject and content on demand
}

func getReceivers() []string {
Expand All @@ -33,14 +35,19 @@ func getReceivers() []string {
// NewEmailConfig create new EmailConfig struct
func NewEmailConfig(err error, expandos *Expandos) EmailConfig {
config := EmailConfig{
Username: os.Getenv("EMAIL_USERNAME"),
Password: os.Getenv("EMAIL_PASSWORD"),
Host: os.Getenv("SMTP_HOST"),
Port: os.Getenv("SMTP_PORT"),
Sender: os.Getenv("EMAIL_SENDER"),
Receivers: getReceivers(),
ErrorObj: err,
Expandos: expandos,
Username: os.Getenv("EMAIL_USERNAME"),
Password: os.Getenv("EMAIL_PASSWORD"),
Host: os.Getenv("SMTP_HOST"),
Port: os.Getenv("SMTP_PORT"),
Sender: os.Getenv("EMAIL_SENDER"),
EnvelopeFrom: os.Getenv("EMAIL_ENVELOPE_FROM"),
Subject: os.Getenv("EMAIL_SUBJECT"),
Receivers: getReceivers(),
ErrorObj: err,
Expandos: expandos,
}
if len(strings.TrimSpace(config.EnvelopeFrom)) == 0 {
config.EnvelopeFrom = config.Sender
}
return config
}
Expand All @@ -55,32 +62,31 @@ func (ec *EmailConfig) Send() error {
r := strings.NewReplacer("\r\n", "", "\r", "", "\n", "", "%0a", "", "%0d", "")

messageDetail := "Error: \r\n" + fmt.Sprintf("%+v", ec.ErrorObj)
subject := os.Getenv("EMAIL_SUBJECT")

// update body and subject dynamically
if ec.Expandos != nil {
if ec.Expandos.EmailBody != "" {
messageDetail = ec.Expandos.EmailBody
}
if ec.Expandos.EmailSubject != "" {
subject = ec.Expandos.EmailSubject
ec.Subject = ec.Expandos.EmailSubject
}
}

message := "To: " + strings.Join(ec.Receivers, ", ") + "\r\n" +
"From: " + ec.Sender + "\r\n" +
"Subject: " + subject + "\r\n" +
"Subject: " + ec.Subject + "\r\n" +
"Content-Type: text/html; charset=\"UTF-8\"\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"\r\n" + base64.StdEncoding.EncodeToString([]byte(string(messageDetail)))
"\r\n" + base64.StdEncoding.EncodeToString([]byte(messageDetail))

if len(strings.TrimSpace(ec.Username)) != 0 {
stmpAuth := smtp.PlainAuth("", ec.Username, ec.Password, ec.Host)

err = smtp.SendMail(
ec.Host+":"+ec.Port,
stmpAuth,
ec.Sender,
ec.EnvelopeFrom,
ec.Receivers,
[]byte(message),
)
Expand All @@ -93,7 +99,7 @@ func (ec *EmailConfig) Send() error {
}

defer conn.Close()
if err = conn.Mail(r.Replace(ec.Sender)); err != nil {
if err = conn.Mail(r.Replace(ec.EnvelopeFrom)); err != nil {
return err
}
// format receiver email
Expand Down
Loading