email test code ing...

This commit is contained in:
geek 2017-06-06 20:52:51 +09:00
parent c9c1e55724
commit 003cc08d19
2 changed files with 148 additions and 42 deletions

View File

@ -7,52 +7,126 @@ import (
"crypto/tls" "crypto/tls"
"log" "log"
"net/mail" "net/mail"
"strings"
"github.com/google/uuid"
"encoding/json"
"git.loafle.net/overflow/overflow_proxy_service/proxy"
"git.loafle.net/overflow/overflow_proxy_service/proxy/member"
) )
const ( const (
FROM = "geek@loafle.com" FROM = "geek@loafle.com"
SERVER_NAME = "smtp.worksmobile.com:465" SERVER_NAME = "smtp.worksmobile.com:465"
SUBJECT = "This is the Test Email"
BODY_MSG = "This is an Example Email\n with two lines \n http://localhost:8080/v1/overflow/services"
SERVER_PASS = "@loafle@5795"
) )
type Email struct {
Id json.Number `json:"id,Number,omitempty"`
Member member.Member `json:"member"`
From string `json:"_"`
Subj string `json:"_"`
AuthToken string `json:"authToken"`
IsInvalid bool `json:"isInvalid"`
SmtpServer string `json:"_"`
BodyMsg string `json:"_"`
}
func NewEmail(member member.Member, subject string) *Email {
if subject == ""{
subject = SUBJECT
}
return &Email{
Member:member,
From:FROM,
Subj:subject,
SmtpServer:SERVER_NAME,
}
}
type EmailService struct { type EmailService struct {
userUrl string `json:"userUrl"`
} }
func NewEmailService() *EmailService{ func NewEmailService() *EmailService{
e := &EmailService{} return &EmailService{}
return e
} }
func (e *EmailService) CheckEmail(url string) bool { func (es *EmailService) CheckAuthURL(e *Email) bool {
//Todo Query from the database with an authentication token.
memMap := make(map[string]string)
str, err := json.Marshal(e)
if err != nil {
log.Fatal("Json Marshal Error: ", err)
}
memMap["com.loafle.overflow.email.model.EmailAuth"] = string(str)
re := proxy.InvokeDB("emailAuth", "find", memMap)
tempEmail := &Email{}
json.Unmarshal([]byte(re), tempEmail)
r := strings.Compare(e.AuthToken, tempEmail.AuthToken)
if r == 0 {
return true
}
return false return false
} }
func EmailSendForAuth(email string) (error){
to := mail.Address{"Park Byung Eun",email} func (es *EmailService) checkError(err error) {
from := mail.Address{"Overflow", FROM} if err != nil {
subj := "This is the Test Email" log.Panic(err)
body := "This is an Example Email\n with two lines \n http://localhost:8080/v1/overflow/services" }
}
func (es *EmailService) getSendMailMessage(e *Email) (string) {
to := mail.Address{"Park Byung Eun",e.Member.Email}
from := mail.Address{"Overflow", e.From}
//body := "This is an Example Email\n with two lines \n http://localhost:8080/v1/overflow/services"
// Setup headers // Setup headers
headers := make(map[string]string) headers := make(map[string]string)
headers["From"] = from.String() headers["From"] = from.String()
headers["To"] = to.String() headers["To"] = to.String()
headers["Subject"] = subj headers["Subject"] = e.Subj
//Setup Message
message := "" message := ""
for k, v := range headers { for k, v := range headers {
message += fmt.Sprintf("%s: %s\r\n",k,v) message += fmt.Sprintf("%s: %s\r\n",k,v)
} }
message += "\r\n" + body message += "\r\n" + BODY_MSG
return message
}
func (es *EmailService) generationAuthToken(e *Email) string {
var tempToken string
uuid,_ := uuid.NewRandom()
tempToken += strings.ToUpper(uuid.String())
return tempToken
}
func (es *EmailService)SendEmailForAuth(e *Email) (error){
to := mail.Address{"Park Byung Eun",e.Member.Email}
from := mail.Address{"Overflow", e.From}
message := es.getSendMailMessage(e)
host, _, _ := net.SplitHostPort(SERVER_NAME) host, _, _ := net.SplitHostPort(SERVER_NAME)
auth := smtp.PlainAuth("",FROM, "@loafle@5795", host) auth := smtp.PlainAuth("",FROM, SERVER_PASS, host)
// TLS config // TLS config
tlsconfig := &tls.Config { tlsconfig := &tls.Config {
@ -60,49 +134,56 @@ func EmailSendForAuth(email string) (error){
ServerName: host, ServerName: host,
} }
//Todo auth token generation
e.AuthToken = es.generationAuthToken(e)
e.IsInvalid = false
conn, err := tls.Dial("tcp", SERVER_NAME, tlsconfig) conn, err := tls.Dial("tcp", SERVER_NAME, tlsconfig)
if err != nil { es.checkError(err)
log.Panic(err)
}
c, err := smtp.NewClient(conn, host) c, err := smtp.NewClient(conn, host)
if err != nil { es.checkError(err)
log.Panic(err)
}
// Auth // Auth
if err = c.Auth(auth); err != nil { err = c.Auth(auth)
log.Panic(err) es.checkError(err)
}
// To && From // To && From
if err = c.Mail(from.Address); err != nil { err = c.Mail(from.Address)
log.Panic(err) es.checkError(err)
}
if err = c.Rcpt(to.Address); err != nil { err = c.Rcpt(to.Address)
log.Panic(err) es.checkError(err)
}
// Data // Data
w, err := c.Data() w, err := c.Data()
if err != nil { es.checkError(err)
log.Panic(err)
}
_, err = w.Write([]byte(message)) _, err = w.Write([]byte(message))
if err != nil { es.checkError(err)
log.Panic(err)
}
err = w.Close() err = w.Close()
if err != nil { es.checkError(err)
log.Panic(err)
}
c.Quit() c.Quit()
// Todo Save Email For DB
es.saveEmail(e)
return err return err
} }
func (es *EmailService) saveEmail(e *Email) {
memMap := make(map[string]string)
str, err := json.Marshal(e)
fmt.Println(string(str))
if err != nil {
log.Fatal("Json Marshal Error: ", err)
}
memMap["com.loafle.overflow.email.model.EmailAuth"] = string(str)
proxy.InvokeDB("emailAuth", "create", memMap)
}

View File

@ -2,11 +2,36 @@ package email
import ( import (
"testing" "testing"
"log" "fmt"
"git.loafle.net/overflow/overflow_proxy_service/proxy/member"
"encoding/json"
) )
func TestEmailSendForAuth(t *testing.T) { func getEmailObj() (*Email,*EmailService) {
err := EmailSendForAuth("geek@loafle.com") es := NewEmailService()
m := member.Member{
log.Println(err) Email:"geek@loafle.com",
Name:"geek",
Company:"loafle",
Id:json.Number("4"),
}
e := NewEmail(m, "Hello Oveflow")
return e,es
}
func TestSendEmailForAuth(t *testing.T) {
e, es := getEmailObj()
es.SendEmailForAuth(e)
}
func TestEmailService_CheckAuthURL(t *testing.T) {
}
func TestAuthTokenGeneration(t *testing.T) {
e, es := getEmailObj()
tt := es.generationAuthToken(e)
fmt.Println(tt)
} }