diff --git a/README.md b/README.md new file mode 100644 index 0000000..9f76b9b --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +# ENS with Go + +Go library for Email, Notification, SMS + +
+ +## Installation + +```ubuntu +$ go get github.com/smartblock/pkgscens@1.0.0 +``` + +
+ +## Usage +```go +package main + +import ( + "fmt" + + "github.com/smartblock/pkgscens" +) + +func main() { + smtpAuth := pkgscens.SMTPAuth{ + Identity: "", + Username: "example@gmail.com", + Password: "your_password", + Host: "smtp.gmail.com", + } + + sendMailInput := pkgscens.SendMailInput{ + Addr: "smtp.gmail.com:587", + SMTPAuth: smtpAuth, + FromName: "Noreply Example", + FromMail: "example@gmail.com", + ToMail: []string{"receipient@gmail.com"}, + ToName: []string{"ReceipientName"}, + Subject: "First Test Email", + //HTML or TEXT + MsgType: "HTML", + Message: "

My First Message

", + } + + err := pkgscens.SendMail(sendMailInput) + + if err != nil { + //Error Return + fmt.Println(err) + } + + //Send Success + fmt.Println("send success") +} +``` diff --git a/email.go b/email.go new file mode 100644 index 0000000..7159ad5 --- /dev/null +++ b/email.go @@ -0,0 +1,130 @@ +package pkgscens + +import ( + "encoding/base64" + "fmt" + "net/smtp" + "strings" +) + +//SMTPAuth def +type SMTPAuth struct { + Identity string + Username string + Password string + Host string +} + +//SendMailInput struct +type SendMailInput struct { + Addr string + SMTPAuth SMTPAuth + FromName string + FromMail string + ToMail []string + ToName []string + Subject string + MsgType string + Message string +} + +//SendMail func +func SendMail(sendMailInput SendMailInput) error { + auth := smtp.PlainAuth( + "", + sendMailInput.SMTPAuth.Username, + sendMailInput.SMTPAuth.Password, + sendMailInput.SMTPAuth.Host, + ) + + if sendMailInput.FromMail == "" { + return &PkgError{ + Msg: "empty from mail", + } + } + + if len(sendMailInput.ToMail) != len(sendMailInput.ToName) { + return &PkgError{ + Msg: "email_to_name_must_match_with_to_email", + } + } + + if sendMailInput.Subject == "" { + return &PkgError{ + Msg: "Empty Subject", + } + } + + msgType := "" + + if sendMailInput.MsgType != "TEXT" && sendMailInput.MsgType != "HTML" { + if sendMailInput.MsgType == "" { + msgType = "TEXT" + } else { + return &PkgError{ + Msg: "MsgType Error", + } + } + } else { + msgType = sendMailInput.MsgType + } + + var recipientMail []string + var toHeaderMail []string + + // to email process + if len(sendMailInput.ToMail) != 0 { + for index, to := range sendMailInput.ToMail { + str := sendMailInput.ToName[index] + " <" + to + ">" + toHeaderMail = append(toHeaderMail, str) + recipientMail = append(recipientMail, to) + } + } + + toHeader := strings.Join(toHeaderMail, ",") + + header := make(map[string]string) + header["MIME-Version"] = "1.0" + header["Content-Transfer-Encoding"] = "base64" + header["From"] = sendMailInput.FromName + " <" + sendMailInput.FromMail + ">" + header["Subject"] = sendMailInput.Subject + + if msgType == "HTML" { + header["Content-Type"] = "text/html; charset=\"utf-8\"" + } else { + header["Content-Type"] = "text/plain; charset=\"utf-8\"" + } + + if toHeader != "" { + header["To"] = toHeader + } + + msg := "" + for k, v := range header { + msg += fmt.Sprintf("%s: %s\r\n", k, v) + } + msg += "\r\n" + base64.StdEncoding.EncodeToString([]byte(sendMailInput.Message)) + + err := smtp.SendMail( + sendMailInput.Addr, // server:port + auth, // auth + sendMailInput.FromMail, // from email_address + recipientMail, // to []email_address + []byte(msg), // msg content_here + ) + + if err != nil { + return err + } + + return nil +} + +//PkgError Type +type PkgError struct { + Msg string +} + +func (m *PkgError) Error() string { + return m.Msg +} diff --git a/emailpkg/emailService.go b/emailpkg/emailService.go new file mode 100644 index 0000000..8269d81 --- /dev/null +++ b/emailpkg/emailService.go @@ -0,0 +1,6 @@ +package emailpkg + +//TestFunc func +func TestFunc() string { + return "TestFunc" +} diff --git a/jpush.go b/jpush.go new file mode 100644 index 0000000..cba1a5c --- /dev/null +++ b/jpush.go @@ -0,0 +1 @@ +package pkgscens