203 lines
4.3 KiB
Go
203 lines
4.3 KiB
Go
package email
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"fmt"
|
|
"git.loafle.net/overflow/commons_go/model/timestamp"
|
|
"git.loafle.net/overflow/overflow_service/proxy/member"
|
|
"git.loafle.net/overflow/overflow_service/proxy/utils"
|
|
"github.com/google/uuid"
|
|
"log"
|
|
"net"
|
|
"net/mail"
|
|
"net/smtp"
|
|
"strings"
|
|
)
|
|
|
|
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:"_"`
|
|
EmailAuthKey string `json:"EmailAuthKey"`
|
|
CreateDate timestamp.Timestamp `json:"createDate,omitempty"`
|
|
authConfirmDate timestamp.Timestamp `json:"authConfirmDate,omitempty"`
|
|
SmtpServer string `json:"_"`
|
|
BodyMsg string `json:"_"`
|
|
}
|
|
|
|
func NewEmail(id json.Number, email string, companyName string, memberName string, subject string) *Email {
|
|
|
|
if subject == "" {
|
|
subject = SUBJECT
|
|
}
|
|
|
|
return &Email{
|
|
Member: &member.Member{
|
|
Id: id,
|
|
Email: email,
|
|
CompanyName: companyName,
|
|
Name: memberName,
|
|
},
|
|
From: FROM,
|
|
Subj: subject,
|
|
SmtpServer: SERVER_NAME,
|
|
}
|
|
}
|
|
|
|
type EmailService struct {
|
|
}
|
|
|
|
func NewEmailService() *EmailService {
|
|
return &EmailService{}
|
|
}
|
|
|
|
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"] = e.Subj
|
|
|
|
message := ""
|
|
|
|
for k, v := range headers {
|
|
message += fmt.Sprintf("%s: %s\r\n", k, v)
|
|
}
|
|
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, SERVER_PASS, host)
|
|
|
|
// TLS config
|
|
tlsconfig := &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
ServerName: host,
|
|
}
|
|
|
|
//Todo auth token generation
|
|
e.EmailAuthKey = es.generationAuthToken(e)
|
|
|
|
conn, err := tls.Dial("tcp", SERVER_NAME, tlsconfig)
|
|
es.checkError(err)
|
|
|
|
c, err := smtp.NewClient(conn, host)
|
|
es.checkError(err)
|
|
|
|
// Auth
|
|
err = c.Auth(auth)
|
|
es.checkError(err)
|
|
|
|
// To && From
|
|
err = c.Mail(from.Address)
|
|
es.checkError(err)
|
|
|
|
err = c.Rcpt(to.Address)
|
|
es.checkError(err)
|
|
|
|
// Data
|
|
w, err := c.Data()
|
|
es.checkError(err)
|
|
|
|
_, err = w.Write([]byte(message))
|
|
es.checkError(err)
|
|
|
|
err = w.Close()
|
|
es.checkError(err)
|
|
|
|
c.Quit()
|
|
|
|
// Todo Save Email For DB
|
|
es.saveEmail(e)
|
|
|
|
return err
|
|
}
|
|
|
|
func (es *EmailService) saveEmail(e *Email) {
|
|
|
|
rr, err := utils.InvokeDBByModel("emailAuth", "save", e, utils.MODEL_EMAIL_AUTH)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Println(rr)
|
|
|
|
}
|
|
|
|
func (es *EmailService) CheckAuthURL(e *Email) bool {
|
|
|
|
//Query from the database with an authentication token.
|
|
|
|
re, err := utils.InvokeDBByModel("emailAuth", "findByEmailAuthKey", e.EmailAuthKey, utils.MODEL_STRING)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
tempEmail := &Email{}
|
|
json.Unmarshal([]byte(re), tempEmail)
|
|
|
|
r := strings.Compare(e.EmailAuthKey, tempEmail.EmailAuthKey)
|
|
|
|
//Todo Check for valid
|
|
|
|
log.Println(tempEmail)
|
|
if r == 0 {
|
|
// Todo isConfirm change true and db update
|
|
tempEmail.authConfirmDate = timestamp.Now()
|
|
|
|
es.modifyEmailAuth(tempEmail)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (es *EmailService) modifyEmailAuth(e *Email) {
|
|
|
|
re, err := utils.InvokeDBByModel("emailAuth", "save", e, utils.MODEL_EMAIL_AUTH)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Println(re)
|
|
}
|