Add: Message Id header to avoid spam mails

Closed Akshay Bharambe requested to merge ab_Add_MSGIDForEmail into devbranch
Compare and
2 files
+ 37
2
Preferences
File browser
Compare changes
@@ -7,6 +7,8 @@ import (
"strings"
"sync"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/utiliymdl/guidmdl"
"gopkg.in/gomail.v2"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/configmdl"
@@ -51,6 +53,7 @@ type Email struct {
subject string
attachments []string
body string
plainBody string
}
func NewEmail(to, cc, bcc, attachments []string, from, subject, body string) *Email {
@@ -127,13 +130,21 @@ func (email *Email) parseTemplateText(templateText string, templateData interfac
// Send -send email
func (email *Email) Send() error {
domain, domainErr := getSenderDomain(email.from)
if domainErr != nil {
loggermdl.LogError("error getting domain address: ", domainErr)
return domainErr
}
message := gomail.NewMessage()
message.SetHeader("From", email.from)
message.SetHeader("To", email.to...)
message.SetHeader("Cc", email.cc...)
message.SetHeader("Bcc", email.bcc...)
message.SetHeader("Subject", email.subject)
message.SetBody("text/html", email.body)
message.SetBody("text/plain", email.plainBody)
message.SetHeader("Message-ID", getMSGIDHeader(domain, guidmdl.GetGUID()))
message.AddAlternative("text/html", email.body)
for _, attachment := range email.attachments {
message.Attach(attachment) // attach whatever you want
}
@@ -146,6 +157,19 @@ func (email *Email) Send() error {
return nil
}
func getMSGIDHeader(domain, guid string) string {
return "<" + guid + "@" + domain + ">"
}
func getSenderDomain(from string) (string, error) {
s := strings.Split(from, "@")
if len(s) != 2 {
return "", errormdl.Wrap("invalid email id for sender")
}
return s[1], nil
}
// SendMailSMTP - send email service sends email as per the given html template and data using smtp
// func (email *Email) SendMailSMTP(templateFilePath string, templateData interface{}) error {
// err := email.parseTemplate(templateFilePath, templateData)