package email

import (
	"gopkg.in/gomail.v2"
	"fmt"
	"chain33-pai/pkg/setting"
)

func SendToMail(message string, toAddr string) error {
	m := gomail.NewMessage()
	m.SetAddressHeader("From", setting.EmailSetting.Sender, setting.EmailSetting.Name) // 发件人
	m.SetHeader("To",                                        // 收件人
		m.FormatAddress(toAddr, ""),
	)
	m.SetHeader("Subject", "节点监控异常")   // 主题
	m.SetBody("text/html", message)        // 正文
	d := gomail.NewPlainDialer(setting.EmailSetting.Host, setting.EmailSetting.Port, setting.EmailSetting.Sender, setting.EmailSetting.Password) // 发送邮件服务器、端口、发件人账号、发件人密码
	if err := d.DialAndSend(m); err != nil {
		fmt.Println("发送失败", err)
		return err
	}
	return nil
}
