Last active
July 27, 2019 00:00
-
-
Save nkkarthik/bbad9b1d119a5c4e8f0252200d3d8155 to your computer and use it in GitHub Desktop.
golang send smtp email with subject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, I need help with a project, can you please send me your e-mail?