This commit is contained in:
geek 2017-06-08 11:20:16 +09:00
parent d75306d489
commit 3701881461

View File

@ -1,76 +1,72 @@
package email package email
import ( import (
"fmt"
"net"
"net/smtp"
"crypto/tls" "crypto/tls"
"log"
"net/mail"
"strings"
"github.com/google/uuid"
"encoding/json" "encoding/json"
"fmt"
"git.loafle.net/overflow/commons_go/model/timestamp"
"git.loafle.net/overflow/overflow_proxy_service/proxy" "git.loafle.net/overflow/overflow_proxy_service/proxy"
"git.loafle.net/overflow/overflow_proxy_service/proxy/member" "git.loafle.net/overflow/overflow_proxy_service/proxy/member"
"git.loafle.net/overflow/commons_go/model/timestamp" "github.com/google/uuid"
"log"
"net"
"net/mail"
"net/smtp"
"strings"
) )
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" SUBJECT = "This is the Test Email"
BODY_MSG = "This is an Example Email\n with two lines \n http://localhost:8080/v1/overflow/services" BODY_MSG = "This is an Example Email\n with two lines \n http://localhost:8080/v1/overflow/services"
SERVER_PASS = "@loafle@5795" SERVER_PASS = "@loafle@5795"
) )
type Email struct { type Email struct {
Id json.Number `json:"id,Number,omitempty"` Id json.Number `json:"id,Number,omitempty"`
Member member.Member `json:"member"` Member member.Member `json:"member"`
From string `json:"_"` From string `json:"_"`
Subj string `json:"_"` Subj string `json:"_"`
AuthToken string `json:"authToken"` AuthToken string `json:"authToken"`
IsInvalid bool `json:"isInvalid"` IsInvalid bool `json:"isInvalid"`
IsConfirm bool `json:"isConfirm"` IsConfirm bool `json:"isConfirm"`
CreateDate timestamp.Timestamp `json:"createDate,omitempty"` CreateDate timestamp.Timestamp `json:"createDate,omitempty"`
UpdateDate timestamp.Timestamp `json:"updateDate,omitempty"` UpdateDate timestamp.Timestamp `json:"updateDate,omitempty"`
ConfirmDate timestamp.Timestamp `json:"confirmDate,omitempty"` ConfirmDate timestamp.Timestamp `json:"confirmDate,omitempty"`
SmtpServer string `json:"_"` SmtpServer string `json:"_"`
BodyMsg string `json:"_"` BodyMsg string `json:"_"`
} }
func NewEmail(member member.Member, subject string) *Email { func NewEmail(member member.Member, subject string) *Email {
if subject == ""{ if subject == "" {
subject = SUBJECT subject = SUBJECT
} }
return &Email{ return &Email{
Member:member, Member: member,
From:FROM, From: FROM,
Subj:subject, Subj: subject,
SmtpServer:SERVER_NAME, SmtpServer: SERVER_NAME,
} }
} }
type EmailService struct { type EmailService struct {
} }
func NewEmailService() *EmailService{ func NewEmailService() *EmailService {
return &EmailService{} return &EmailService{}
} }
func (es *EmailService) checkError(err error) { func (es *EmailService) checkError(err error) {
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
} }
func (es *EmailService) getSendMailMessage(e *Email) (string) { func (es *EmailService) getSendMailMessage(e *Email) string {
to := mail.Address{"Park Byung Eun",e.Member.Email} to := mail.Address{"Park Byung Eun", e.Member.Email}
from := mail.Address{"Overflow", e.From} from := mail.Address{"Overflow", e.From}
//body := "This is an Example Email\n with two lines \n http://localhost:8080/v1/overflow/services" //body := "This is an Example Email\n with two lines \n http://localhost:8080/v1/overflow/services"
@ -83,7 +79,7 @@ func (es *EmailService) getSendMailMessage(e *Email) (string) {
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_MSG message += "\r\n" + BODY_MSG
@ -93,26 +89,25 @@ func (es *EmailService) getSendMailMessage(e *Email) (string) {
func (es *EmailService) generationAuthToken(e *Email) string { func (es *EmailService) generationAuthToken(e *Email) string {
var tempToken string var tempToken string
uuid,_ := uuid.NewRandom() uuid, _ := uuid.NewRandom()
tempToken += strings.ToUpper(uuid.String()) tempToken += strings.ToUpper(uuid.String())
return tempToken return tempToken
} }
func (es *EmailService) SendEmailForAuth(e *Email) error {
func (es *EmailService)SendEmailForAuth(e *Email) (error){ to := mail.Address{"Park Byung Eun", e.Member.Email}
to := mail.Address{"Park Byung Eun",e.Member.Email}
from := mail.Address{"Overflow", e.From} from := mail.Address{"Overflow", e.From}
message := es.getSendMailMessage(e) message := es.getSendMailMessage(e)
host, _, _ := net.SplitHostPort(SERVER_NAME) host, _, _ := net.SplitHostPort(SERVER_NAME)
auth := smtp.PlainAuth("",FROM, SERVER_PASS, host) auth := smtp.PlainAuth("", FROM, SERVER_PASS, host)
// TLS config // TLS config
tlsconfig := &tls.Config { tlsconfig := &tls.Config{
InsecureSkipVerify: true, InsecureSkipVerify: true,
ServerName: host, ServerName: host,
} }
//Todo auth token generation //Todo auth token generation
@ -188,7 +183,7 @@ func (es *EmailService) CheckAuthURL(e *Email) bool {
tempEmail := &Email{} tempEmail := &Email{}
json.Unmarshal([]byte(re), tempEmail) json.Unmarshal([]byte(re), tempEmail)
r := strings.Compare(e.AuthToken, tempEmail.AuthToken) r := strings.Compare(e.AuthToken, tempEmail.AuthToken)
log.Println(tempEmail) log.Println(tempEmail)
if r == 0 { if r == 0 {
@ -207,4 +202,4 @@ func (es *EmailService) modifyEmailAuth(e *Email) {
emMap := es.getEmailMap(e) emMap := es.getEmailMap(e)
proxy.InvokeDB("emailAuth", "update", emMap) proxy.InvokeDB("emailAuth", "update", emMap)
} }