├── README.md ├── app.go ├── config.go ├── config.toml ├── mailer.go └── templates └── template.html /README.md: -------------------------------------------------------------------------------- 1 | # Tutorial 2 | 3 | * [Sending HTML email using Golang](http://www.blog.labouardy.com/sending-html-email-using-go/) 4 | -------------------------------------------------------------------------------- /app.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | var config = Config{} 4 | 5 | func init() { 6 | config.Read() 7 | } 8 | 9 | func main() { 10 | subject := "Get latest Tech News directly to your inbox" 11 | destination := "mohamed.labouardy@gmail.com" 12 | r := NewRequest([]string{destination}, subject) 13 | r.Send("templates/template.html", map[string]string{"username": "Conor"}) 14 | } 15 | -------------------------------------------------------------------------------- /config.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | 6 | "github.com/BurntSushi/toml" 7 | ) 8 | 9 | type Config struct { 10 | Server string 11 | Port int 12 | Email string 13 | Password string 14 | } 15 | 16 | func (c *Config) Read() { 17 | if _, err := toml.DecodeFile("config.toml", &c); err != nil { 18 | log.Fatal(err) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /config.toml: -------------------------------------------------------------------------------- 1 | server="smtp.gmail.com" 2 | port=587 3 | email="" 4 | password="" 5 | -------------------------------------------------------------------------------- /mailer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "html/template" 7 | "log" 8 | "net/smtp" 9 | ) 10 | 11 | type Request struct { 12 | from string 13 | to []string 14 | subject string 15 | body string 16 | } 17 | 18 | const ( 19 | MIME = "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" 20 | ) 21 | 22 | func NewRequest(to []string, subject string) *Request { 23 | return &Request{ 24 | to: to, 25 | subject: subject, 26 | } 27 | } 28 | 29 | func (r *Request) parseTemplate(fileName string, data interface{}) error { 30 | t, err := template.ParseFiles(fileName) 31 | if err != nil { 32 | return err 33 | } 34 | buffer := new(bytes.Buffer) 35 | if err = t.Execute(buffer, data); err != nil { 36 | return err 37 | } 38 | r.body = buffer.String() 39 | return nil 40 | } 41 | 42 | func (r *Request) sendMail() bool { 43 | body := "To: " + r.to[0] + "\r\nSubject: " + r.subject + "\r\n" + MIME + "\r\n" + r.body 44 | SMTP := fmt.Sprintf("%s:%d", config.Server, config.Port) 45 | if err := smtp.SendMail(SMTP, smtp.PlainAuth("", config.Email, config.Password, config.Server), config.Email, r.to, []byte(body)); err != nil { 46 | return false 47 | } 48 | return true 49 | } 50 | 51 | func (r *Request) Send(templateName string, items interface{}) { 52 | err := r.parseTemplate(templateName, items) 53 | if err != nil { 54 | log.Fatal(err) 55 | } 56 | if ok := r.sendMail(); ok { 57 | log.Printf("Email has been sent to %s\n", r.to) 58 | } else { 59 | log.Printf("Failed to send the email to %s\n", r.to) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /templates/template.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Subscribe To My Blog - M.Labouardy 7 | 60 | 61 | 62 | 63 | 64 | 67 | 68 | 69 | 75 | 76 | 77 | 86 | 87 | 88 | 91 | 92 |
65 | It's worth it ! 66 |
70 |

71 | Hi {{ .username }},
72 | I'm Mohamed. I write about DevOps, ChatOps, Android and much more. If you don't find me super annoying, subscribe to my mailing list in order to make sure you don't miss my newest blog posts. 73 |

74 |
93 | 94 | 95 | --------------------------------------------------------------------------------