Skip to content

Instantly share code, notes, and snippets.

@nkkarthik
Last active July 27, 2019 00:00
Show Gist options
  • Save nkkarthik/bbad9b1d119a5c4e8f0252200d3d8155 to your computer and use it in GitHub Desktop.
Save nkkarthik/bbad9b1d119a5c4e8f0252200d3d8155 to your computer and use it in GitHub Desktop.
golang send smtp email with subject
type Email struct {
From string
To []string
Subject string
Body string
}
func (email *Email) Send() error {
data := fmt.Sprintf("To: %s\r\nSubject: %s\r\n\r\n%s\r\n",
strings.Join(email.To, ","),
email.Subject,
email.Body)
c, err := smtp.Dial(EmailServer)
if err != nil {
return err
}
defer c.Close()
if err := c.Mail(email.From); err != nil {
return err
}
for _, addr := range email.To {
if err := c.Rcpt(addr); err != nil {
return err
}
}
wc, err := c.Data()
if err != nil {
return err
}
defer wc.Close()
log.Printf("sending email: %s\n", data)
buf := bytes.NewBufferString(data)
_, err = buf.WriteTo(wc)
return err
}
@OmnyaAmr
Copy link

Hello, I need help with a project, can you please send me your e-mail?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment