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"
"log"
"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 (
FROM = "geek@loafle.com"
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 {
userUrl string `json:"userUrl"`
}
func NewEmailService() *EmailService{
e := &EmailService{}
return e
return &EmailService{}
}
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
}
func EmailSendForAuth(email string) (error){
to := mail.Address{"Park Byung Eun",email}
from := mail.Address{"Overflow", FROM}
subj := "This is the Test Email"
body := "This is an Example Email\n with two lines \n http://localhost:8080/v1/overflow/services"
func (es *EmailService) checkError(err error) {
if err != nil {
log.Panic(err)
}
}
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
headers := make(map[string]string)
headers["From"] = from.String()
headers["To"] = to.String()
headers["Subject"] = subj
headers["Subject"] = e.Subj
//Setup Message
message := ""
for k, v := range headers {
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)
auth := smtp.PlainAuth("",FROM, "@loafle@5795", host)
auth := smtp.PlainAuth("",FROM, SERVER_PASS, host)
// TLS config
tlsconfig := &tls.Config {
@ -60,49 +134,56 @@ func EmailSendForAuth(email string) (error){
ServerName: host,
}
//Todo auth token generation
e.AuthToken = es.generationAuthToken(e)
e.IsInvalid = false
conn, err := tls.Dial("tcp", SERVER_NAME, tlsconfig)
if err != nil {
log.Panic(err)
}
es.checkError(err)
c, err := smtp.NewClient(conn, host)
if err != nil {
log.Panic(err)
}
es.checkError(err)
// Auth
if err = c.Auth(auth); err != nil {
log.Panic(err)
}
err = c.Auth(auth)
es.checkError(err)
// To && From
if err = c.Mail(from.Address); err != nil {
log.Panic(err)
}
err = c.Mail(from.Address)
es.checkError(err)
if err = c.Rcpt(to.Address); err != nil {
log.Panic(err)
}
err = c.Rcpt(to.Address)
es.checkError(err)
// Data
w, err := c.Data()
if err != nil {
log.Panic(err)
}
es.checkError(err)
_, err = w.Write([]byte(message))
if err != nil {
log.Panic(err)
}
es.checkError(err)
err = w.Close()
if err != nil {
log.Panic(err)
}
es.checkError(err)
c.Quit()
// Todo Save Email For DB
es.saveEmail(e)
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 (
"testing"
"log"
"fmt"
"git.loafle.net/overflow/overflow_proxy_service/proxy/member"
"encoding/json"
)
func TestEmailSendForAuth(t *testing.T) {
err := EmailSendForAuth("geek@loafle.com")
func getEmailObj() (*Email,*EmailService) {
es := NewEmailService()
m := member.Member{
Email:"geek@loafle.com",
Name:"geek",
Company:"loafle",
Id:json.Number("4"),
}
e := NewEmail(m, "Hello Oveflow")
log.Println(err)
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)
}