2017-06-02 18:18:52 +09:00

97 lines
1.4 KiB
Go

package email
import (
"fmt"
"net"
"net/smtp"
"crypto/tls"
"log"
"net/mail"
)
const (
FROM = "geek@loafle.com"
SERVERNAME = "smtp.worksmobile.com:465"
)
type EmailService struct {
}
func EmailSendForAuth(email string) (error){
to := mail.Address{"",email}
from := mail.Address{"", FROM}
subj := "This is the Test Email"
body := "This is an Example Email\n with two lines"
// Setup headers
headers := make(map[string]string)
headers["From"] = from.String()
headers["To"] = to.String()
headers["Subject"] = subj
//Setup Message
message := ""
for k, v := range headers {
message += fmt.Sprintf("%s: %s\r\n",k,v)
}
message += "\r\n" + body
host, _, _ := net.SplitHostPort(SERVERNAME)
auth := smtp.PlainAuth("",FROM, "@loafle@5795", host)
// TLS config
tlsconfig := &tls.Config {
InsecureSkipVerify: true,
ServerName: host,
}
conn, err := tls.Dial("tcp", SERVERNAME, tlsconfig)
if err != nil {
log.Panic(err)
}
c, err := smtp.NewClient(conn, host)
if err != nil {
log.Panic(err)
}
// Auth
if err = c.Auth(auth); err != nil {
log.Panic(err)
}
// To && From
if err = c.Mail(from.Address); err != nil {
log.Panic(err)
}
if err = c.Rcpt(to.Address); err != nil {
log.Panic(err)
}
// Data
w, err := c.Data()
if err != nil {
log.Panic(err)
}
_, err = w.Write([]byte(message))
if err != nil {
log.Panic(err)
}
err = w.Close()
if err != nil {
log.Panic(err)
}
c.Quit()
return err
}