本文整理汇总了Golang中mime/multipart.Writer.SetBoundary方法的典型用法代码示例。如果您正苦于以下问题:Golang Writer.SetBoundary方法的具体用法?Golang Writer.SetBoundary怎么用?Golang Writer.SetBoundary使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mime/multipart.Writer
的用法示例。
在下文中一共展示了Writer.SetBoundary方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: sendMail
func sendMail(to []string, cc []string, bcc []string, msg *Message) error {
from := Config.DefaultFrom
server := Config.MailServer
if msg.Server != "" {
server = msg.Server
}
if msg.From != "" {
from = msg.From
}
if from == "" {
return errNoFrom
}
var auth smtp.Auth
cram, username, password, server := parseServer(server)
if username != "" || password != "" {
if cram {
auth = smtp.CRAMMD5Auth(username, password)
} else {
auth = smtp.PlainAuth("", username, password, server)
}
}
var buf bytes.Buffer
headers := msg.Headers
if headers == nil {
headers = make(Headers)
}
if msg.Subject != "" {
headers["Subject"] = msg.Subject
}
headers["From"] = from
if msg.ReplyTo != "" {
headers["Reply-To"] = msg.ReplyTo
}
var err error
if len(to) > 0 {
headers["To"], err = joinAddrs(to)
if err != nil {
return err
}
for ii, v := range to {
if v == Admin {
if Config.AdminEmail == "" {
return errNoAdminEmail
}
to[ii] = Config.AdminEmail
}
}
}
if len(cc) > 0 {
headers["Cc"], err = joinAddrs(cc)
if err != nil {
return err
}
}
if len(bcc) > 0 {
headers["Bcc"], err = joinAddrs(bcc)
if err != nil {
return err
}
}
hk := make([]string, 0, len(headers))
for k := range headers {
hk = append(hk, k)
}
sort.Strings(hk)
for _, k := range hk {
buf.WriteString(fmt.Sprintf("%s: %s\r\n", k, headers[k]))
}
buf.WriteString("MIME-Version: 1.0\r\n")
mw := multipart.NewWriter(&buf)
mw.SetBoundary(makeBoundary())
// Create a multipart mixed first
fmt.Fprintf(&buf, "Content-Type: multipart/mixed;\r\n\tboundary=%q\r\n\r\n", mw.Boundary())
var bodyWriter *multipart.Writer
if msg.TextBody != "" && msg.HTMLBody != "" {
boundary := makeBoundary()
outerHeader := make(textproto.MIMEHeader)
// First part is a multipart/alternative, which contains the text and html bodies
outerHeader.Set("Content-Type", fmt.Sprintf("multipart/alternative; boundary=%q", boundary))
iw, err := mw.CreatePart(outerHeader)
if err != nil {
return err
}
bodyWriter = multipart.NewWriter(iw)
bodyWriter.SetBoundary(boundary)
} else {
bodyWriter = mw
}
if msg.TextBody != "" {
textHeader := make(textproto.MIMEHeader)
textHeader.Set("Content-Type", "text/plain; charset=UTF-8")
tpw, err := bodyWriter.CreatePart(textHeader)
if err != nil {
return err
}
if _, err := io.WriteString(tpw, msg.TextBody); err != nil {
return err
}
tpw.Write(crlf)
tpw.Write(crlf)
//.........这里部分代码省略.........