Skip to content

flagUpDown/sendmail

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sendmail

sendmail是一个发送邮件的Golang包。没有使用外部依赖,易于安装

下载

go get github.com/flagUpDown/sendmail

使用

引入包

import "github.com/flagUpDown/sendmail"

创建一封信邮件

mail := sendmail.NewMail()

编写信封

mail.SetFromEmail("[email protected]", "FromEmail") // 设置发件人
mail.AddRecipient("[email protected]", "Recipient") // 添加发送人
mail.AddCarbonCopy("[email protected]", "CarbonCopy") // 添加抄送人
mail.AddBlindCarbonCopy("[email protected]", "BlindCarbonCopy") // 添加暗抄送人
mail.SetSubject("subject") // 设置邮件主题
mail.SetContent("<h1>hello word</h1>", true) // 设置邮件内容,可选择是普通文本还是HTML格式的文本
mail.AddAttachment("/path/filename.ext", "file name") // 添加邮件附件

连接远程smtp服务器,可选择是否使用TLS连接。但即使是普通连接,当发现服务器端存在STARTTLS扩展,仍然会启用TLS连接

c, _ := sendmail.Dial("smtp.host.com", 25, false)

设置认证所需的用户名和口令

c.SetAuth("[email protected]", "password")

发送邮件

c.Send(mail)

关闭客户端连接

c.Close()