Wednesday, 19 March 2025

Golang - Email - Secure code warrior

 package mail


import (

"net/smtp"


"gobin/config"

)


var (

emailConfig config.Email

)


type Mail struct {

Destination string

Subject     string

Body        string

}


func SetConfig(c *config.Config) {

emailConfig = c.Email

}


func New(dst, subj, body string) *Mail {

return &Mail{Destination: dst, Subject: subj, Body: body}

}


func Send(m *Mail) error {

msg := "From: " + emailConfig.From + "<" + emailConfig.From + ">" + "\n" +

"To: " + m.Destination + "<" + m.Destination + ">" + "\n" +

"Subject: " + m.Subject + "\n\n" +

m.Body


data := []byte(msg)

addr := emailConfig.Host + ":" + emailConfig.Port

auth := smtp.PlainAuth("GoForum", emailConfig.From, emailConfig.Password,

emailConfig.Host)

dst := []string{m.Destination}


return smtp.SendMail(addr, auth, emailConfig.From, dst, data)

}


No comments:

Post a Comment

Golang - Email - Secure code warrior

 package mail import ( "net/smtp" "gobin/config" ) var ( emailConfig config.Email ) type Mail struct { Destinati...