github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/helper/mail.go (about)

     1  package helper
     2  
     3  import (
     4  	"net/smtp"
     5  	"strings"
     6  )
     7  
     8  var (
     9  	SmtpHost     = "smtp.163.com"
    10  	SmtpPort     = "25"
    11  	MailUser     = "yougamcom@163.com" //发送邮件的邮箱
    12  	MailPassword = "Me87ga88mRpasW"    //发送邮件邮箱的密码
    13  	MailAdline   = "yougam.Com • 分享、探索和创造的地方."
    14  )
    15  
    16  /**
    17  * user : example@example.com login smtp server user
    18  * password: xxxxx login smtp server password
    19  * host: smtp.example.com:port   smtp.163.com:25
    20  * to: example@example.com;example1@163.com;example2@sina.com.cn;...
    21  * subject:The subject of mail
    22  * body: The content of mail
    23  * mailtype: mail type html or text
    24   */
    25  func SendMail(user, password, host, to, subject, body, mailtype string) error {
    26  	hp := strings.Split(host, ":")
    27  	auth := smtp.PlainAuth("", user, password, hp[0])
    28  	var content_type string
    29  
    30  	if mailtype == "html" {
    31  		content_type = "Content-Type: text/" + mailtype + "; charset=UTF-8"
    32  	} else if mailtype == "text" {
    33  		content_type = "Content-Type: text/plain" + "; charset=UTF-8"
    34  	} else {
    35  		content_type = "Content-Type: " + mailtype + "; charset=UTF-8"
    36  	}
    37  
    38  	msg := []byte("To: " + to + "\r\nFrom: " + user + "<" + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
    39  	send_to := strings.Split(to, ";")
    40  	err := smtp.SendMail(host, auth, user, send_to, msg)
    41  	return err
    42  }
    43  
    44  func SendEmail(to, subject, body, mailtype string) error {
    45  	return SendMail(MailUser, MailPassword, (SmtpHost + ":" + SmtpPort), to, subject, body, mailtype)
    46  }