Commit d34d4bbb authored by Akshay Bharambe's avatar Akshay Bharambe
Browse files

Add: Message Id header to avoid spam mails

1. Add: Message Id header to avoid spam mails
parent df5ab86d
2 merge requests!76Add: Message Id header to avoid spam mails,!75Add: Message Id header to avoid spam mails
Showing with 37 additions and 2 deletions
......@@ -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)
......
......@@ -8,6 +8,7 @@ import (
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/utiliymdl/guidmdl"
gomail "gopkg.in/gomail.v2"
)
......@@ -47,13 +48,23 @@ func (email *Email) SendMailFromSLS(hostName string) error {
config = tmp
}
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.body)
message.SetHeader("Message-ID", getMSGIDHeader(domain, guidmdl.GetGUID()))
message.AddAlternative("text/html", email.plainBody)
for _, attachment := range email.attachments {
message.Attach(attachment) // attach whatever you want
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment