package services

import (
	"gopkg.in/gomail.v2"

	"mengpost-backend/models"
)

// SendMail 通过指定账户发送邮件. body 支持纯文本或 HTML.
func SendMail(acc *models.Account, to, subject, body string, html bool) error {
	m := gomail.NewMessage()
	m.SetHeader("From", acc.Email)
	m.SetHeader("To", to)
	m.SetHeader("Subject", subject)
	if html {
		m.SetBody("text/html", body)
	} else {
		m.SetBody("text/plain", body)
	}
	d := gomail.NewDialer(acc.SMTPHost, acc.SMTPPort, acc.Email, acc.Password)
	d.SSL = acc.UseTLS && acc.SMTPPort == 465
	return d.DialAndSend(m)
}
