128 lines
4.0 KiB
Go
128 lines
4.0 KiB
Go
package googleapi
|
|
|
|
import (
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
"mime"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func (c *client) SendMail(from string, recipients []string, cc []string, bcc []string, subject string, body MailBody, attachments []MailAttachment) error {
|
|
|
|
data := make([]string, 0, 32)
|
|
|
|
data = append(data, "Date: "+time.Now().Format(time.RFC1123Z))
|
|
data = append(data, "From: "+mime.QEncoding.Encode("UTF-8", from))
|
|
data = append(data, "To: "+strings.Join(langext.ArrMap(recipients, func(v string) string { return mime.QEncoding.Encode("UTF-8", v) }), ", "))
|
|
if len(cc) > 0 {
|
|
data = append(data, "To: "+strings.Join(langext.ArrMap(cc, func(v string) string { return mime.QEncoding.Encode("UTF-8", v) }), ", "))
|
|
}
|
|
if len(bcc) > 0 {
|
|
data = append(data, "Bcc: "+strings.Join(langext.ArrMap(bcc, func(v string) string { return mime.QEncoding.Encode("UTF-8", v) }), ", "))
|
|
}
|
|
data = append(data, "Subject: "+mime.QEncoding.Encode("UTF-8", subject))
|
|
|
|
hasAttachments := len(attachments) > 0
|
|
hasInlineAttachments := langext.ArrAny(attachments, func(v MailAttachment) bool { return v.IsInline })
|
|
hasPlain := body.Plain != nil
|
|
hasHTML := body.HTML != nil
|
|
|
|
mixedBoundary := "--------------" + langext.MustRawHexUUID()
|
|
relatedBoundary := "--------------" + langext.MustRawHexUUID()
|
|
altBoundary := "--------------" + langext.MustRawHexUUID()
|
|
|
|
inlineAttachments := langext.ArrFilter(attachments, func(v MailAttachment) bool { return v.IsInline })
|
|
normalAttachments := langext.ArrFilter(attachments, func(v MailAttachment) bool { return !v.IsInline })
|
|
|
|
if hasInlineAttachments {
|
|
// "mixed+related"
|
|
|
|
data = append(data, "Content-Type: multipart/mixed; boundary="+mixedBoundary)
|
|
data = append(data, "")
|
|
data = append(data, mixedBoundary)
|
|
|
|
data = append(data, "Content-Type: multipart/related; boundary="+relatedBoundary)
|
|
data = append(data, "")
|
|
|
|
data = append(data, c.dumpHTMLBody(body, hasInlineAttachments, hasAttachments, relatedBoundary, altBoundary)...)
|
|
data = append(data, "")
|
|
|
|
for i, attachment := range inlineAttachments {
|
|
data = append(data, relatedBoundary)
|
|
data = append(data, attachment.dump()...)
|
|
|
|
if i < len(inlineAttachments)-1 {
|
|
data = append(data, "")
|
|
}
|
|
}
|
|
|
|
data = append(data, relatedBoundary)
|
|
|
|
for i, attachment := range normalAttachments {
|
|
data = append(data, mixedBoundary)
|
|
data = append(data, attachment.dump()...)
|
|
|
|
if i < len(inlineAttachments)-1 {
|
|
data = append(data, "")
|
|
}
|
|
}
|
|
|
|
data = append(data, mixedBoundary)
|
|
|
|
} else if hasAttachments {
|
|
// "mixed"
|
|
|
|
//TODO https://github.dev/muratgozel/MIMEText/blob/master/src/MIMEMessage.ts
|
|
|
|
} else if hasPlain && hasHTML {
|
|
// "alternative"
|
|
|
|
//TODO https://github.dev/muratgozel/MIMEText/blob/master/src/MIMEMessage.ts
|
|
|
|
} else {
|
|
// "plain"
|
|
|
|
//TODO https://github.dev/muratgozel/MIMEText/blob/master/src/MIMEMessage.ts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (c *client) dumpHTMLBody(body MailBody, hasInlineAttachments bool, hasAttachments bool, boundary string, boundaryAlt string) []string {
|
|
|
|
data := make([]string, 0, 16)
|
|
|
|
if body.HTML != nil && body.Plain != nil && hasInlineAttachments {
|
|
data = append(data, boundary)
|
|
data = append(data, *body.HTML)
|
|
} else if body.HTML != nil && body.Plain != nil && hasAttachments {
|
|
data = append(data, boundary)
|
|
data = append(data, "Content-Type: multipart/alternative; boundary="+boundaryAlt)
|
|
data = append(data, "")
|
|
data = append(data, boundaryAlt)
|
|
data = append(data, *body.Plain)
|
|
data = append(data, "")
|
|
data = append(data, boundaryAlt)
|
|
data = append(data, *body.HTML)
|
|
data = append(data, "")
|
|
data = append(data, boundaryAlt)
|
|
} else if body.HTML != nil && body.Plain != nil {
|
|
data = append(data, boundary)
|
|
data = append(data, *body.Plain)
|
|
data = append(data, "")
|
|
data = append(data, boundary)
|
|
data = append(data, *body.HTML)
|
|
} else if body.HTML != nil {
|
|
data = append(data, boundary)
|
|
data = append(data, *body.HTML)
|
|
} else if body.Plain != nil {
|
|
data = append(data, boundary)
|
|
data = append(data, *body.Plain)
|
|
} else {
|
|
data = append(data, boundary)
|
|
data = append(data, "") // no content ?!?
|
|
}
|
|
|
|
return data
|
|
}
|