You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/mail/mail.go

57 lines
1.1 KiB
Go

4 years ago
package mail
import (
"crypto/tls"
"fmt"
"net/smtp"
"strings"
)
4 years ago
// Send 寄送信件
4 years ago
func Send(host string, port int, username, password, mailto, subject, content string) error {
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", host, port), &tls.Config{InsecureSkipVerify: true, ServerName: "smtp.gmail.com"})
if err != nil {
return err
}
client, err := smtp.NewClient(conn, "smtp.gmail.com")
if err != nil {
return err
}
if err = client.Auth(smtp.PlainAuth("", username, password, host)); err != nil {
return err
}
if err = client.Mail(username); err != nil {
return err
}
if err = client.Rcpt(mailto); err != nil {
return err
}
w, err := client.Data()
if err != nil {
return err
}
messages := []string{
"Content-Type: text/html; charset=\"UTF-8\";",
fmt.Sprintf("From: %s", username),
fmt.Sprintf("To: %s", mailto),
fmt.Sprintf("Subject: %s", subject),
content,
}
if _, err = w.Write([]byte(strings.Join(messages, "\r\n"))); err != nil {
return err
}
if err = w.Close(); err != nil {
return err
}
return client.Quit()
}