Skip to content

Commit

Permalink
FIRST COMMIT
Browse files Browse the repository at this point in the history
  • Loading branch information
ycchong9 committed Jul 10, 2020
0 parents commit 61afc29
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# ENS with Go

Go library for Email, Notification, SMS

<hr />

## Installation

```ubuntu
$ go get github.com/smartblock/[email protected]
```

<hr />

## Usage
```go
package main

import (
"fmt"

"github.com/smartblock/pkgscens"
)

func main() {
smtpAuth := pkgscens.SMTPAuth{
Identity: "",
Username: "[email protected]",
Password: "your_password",
Host: "smtp.gmail.com",
}

sendMailInput := pkgscens.SendMailInput{
Addr: "smtp.gmail.com:587",
SMTPAuth: smtpAuth,
FromName: "Noreply Example",
FromMail: "[email protected]",
ToMail: []string{"[email protected]"},
ToName: []string{"ReceipientName"},
Subject: "First Test Email",
//HTML or TEXT
MsgType: "HTML",
Message: "<h1>My First Message</h1>",
}

err := pkgscens.SendMail(sendMailInput)

if err != nil {
//Error Return
fmt.Println(err)
}

//Send Success
fmt.Println("send success")
}
```
130 changes: 130 additions & 0 deletions email.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions emailpkg/emailService.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package emailpkg

//TestFunc func
func TestFunc() string {
return "TestFunc"
}
1 change: 1 addition & 0 deletions jpush.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package pkgscens

0 comments on commit 61afc29

Please sign in to comment.