Skip to content

Instantly share code, notes, and snippets.

@dhanush
Last active June 4, 2020 02:54
Show Gist options
  • Save dhanush/f1bac67b659cdd88d3703ea758a313c0 to your computer and use it in GitHub Desktop.
Save dhanush/f1bac67b659cdd88d3703ea758a313c0 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"html/template"
"net/smtp"
)
var auth smtp.Auth
func main() {
auth = smtp.PlainAuth("", "dhanush@geektrust.in", "password", "smtp.gmail.com")
templateData := struct {
Name string
URL string
}{
Name: "Dhanush",
URL: "http://geektrust.in",
}
r := NewRequest([]string{"junk@junk.com"}, "Hello Junk!", "Hello, World!")
err := r.ParseTemplate("template.html", templateData)
if err := r.ParseTemplate("template.html", templateData); err == nil {
ok, _ := r.SendEmail()
fmt.Println(ok)
}
}
//Request struct
type Request struct {
from string
to []string
subject string
body string
}
func NewRequest(to []string, subject, body string) *Request {
return &Request{
to: to,
subject: subject,
body: body,
}
}
func (r *Request) SendEmail() (bool, error) {
mime := "MIME-version: 1.0;\nContent-Type: text/plain; charset=\"UTF-8\";\n\n"
subject := "Subject: " + r.subject + "!\n"
msg := []byte(subject + mime + "\n" + r.body)
addr := "smtp.gmail.com:587"
if err := smtp.SendMail(addr, auth, "dhanush@geektrust.in", r.to, msg); err != nil {
return false, err
}
return true, nil
}
func (r *Request) ParseTemplate(templateFileName string, data interface{}) error {
t, err := template.ParseFiles(templateFileName)
if err != nil {
return err
}
buf := new(bytes.Buffer)
if err = t.Execute(buf, data); err != nil {
return err
}
r.body = buf.String()
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment