├── config.go ├── README.md └── mailer.go /config.go: -------------------------------------------------------------------------------- 1 | package mail 2 | 3 | type Config struct { 4 | Host string 5 | Username string 6 | Password string 7 | Port string 8 | From string 9 | } 10 | 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-mailer 2 | :mailbox_with_mail: Mailer for Go 3 | 4 | ```go 5 | package main 6 | 7 | import ( 8 | "github.com/onerciller/go-mailer" 9 | ) 10 | 11 | var config = mail.Config{ 12 | Host: "smtp.yandex.com.tr", 13 | Username: "onerciller@yandex.com.tr", 14 | Password: "PASSWORD", 15 | Port: "587", 16 | From: "onerciller@yandex.com.tr", 17 | } 18 | 19 | func main() { 20 | mail := mail.New(config) 21 | mail.SetTo("onerciller@gmail.com", "onerciller@yandex.com.tr") 22 | mail.SetSubject("this is subject") 23 | mail.SetBody("this is body") 24 | mail.Send() 25 | } 26 | 27 | ``` 28 | -------------------------------------------------------------------------------- /mailer.go: -------------------------------------------------------------------------------- 1 | package mail 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/smtp" 7 | "strings" 8 | ) 9 | 10 | type Email struct { 11 | Config 12 | To []string 13 | Subject string 14 | Body string 15 | } 16 | 17 | func New(config Config) *Email { 18 | return &Email{ 19 | Config: Config{ 20 | Host: config.Host, 21 | Port: config.Port, 22 | Username: config.Username, 23 | Password: config.Password, 24 | From: config.From, 25 | }, 26 | } 27 | } 28 | 29 | func Header(e *Email) map[string]string { 30 | header := make(map[string]string) 31 | header["From"] = e.Config.From 32 | header["To"] = strings.Join(e.To, ",") 33 | header["subject"] = e.Subject 34 | header["MIME-Version"] = "1.0" 35 | header["Content-Type"] = "text/html; charset\"utf-8\"" 36 | header["Content-Transfer-Encoding"] = "base64" 37 | return header 38 | } 39 | 40 | func (c *Email) Send() { 41 | msg := "" 42 | for key, val := range Header(c) { 43 | msg += fmt.Sprintf("%s: %s\r\n", key, val) 44 | } 45 | msg += "\r\n" 46 | msg += c.Body 47 | err := smtp.SendMail(c.Config.Host+":"+c.Config.Port, 48 | smtp.PlainAuth("", c.Config.Username, c.Config.Password, c.Config.Host), 49 | c.Config.From, c.To, []byte(msg)) 50 | if err != nil { 51 | log.Printf("Error: %s", err) 52 | return 53 | } 54 | log.Print("message sent") 55 | } 56 | 57 | func (e *Email) SetTo(to ...string) { 58 | e.To = to 59 | } 60 | 61 | func (e *Email) SetSubject(subject string) { 62 | e.Subject = subject 63 | } 64 | 65 | func (e *Email) SetBody(body string) { 66 | e.Body = body 67 | } 68 | --------------------------------------------------------------------------------