goext/googleapi/attachment.go

47 lines
976 B
Go
Raw Permalink Normal View History

2023-12-01 18:33:04 +01:00
package googleapi
import (
"encoding/base64"
"fmt"
)
type MailAttachment struct {
IsInline bool
2023-12-04 13:48:11 +01:00
ContentType string
Filename string
2023-12-01 18:33:04 +01:00
Data []byte
}
func (a MailAttachment) dump() []string {
res := make([]string, 0, 4)
2023-12-04 13:48:11 +01:00
if a.ContentType != "" {
res = append(res, "Content-Type: "+a.ContentType+"; charset=UTF-8")
2023-12-01 18:33:04 +01:00
}
res = append(res, "Content-Transfer-Encoding: base64")
if a.IsInline {
2023-12-04 13:48:11 +01:00
if a.Filename != "" {
res = append(res, fmt.Sprintf("Content-Disposition: inline;filename=\"%s\"", a.Filename))
2023-12-01 18:33:04 +01:00
} else {
res = append(res, "Content-Disposition: inline")
}
} else {
2023-12-04 13:48:11 +01:00
if a.Filename != "" {
res = append(res, fmt.Sprintf("Content-Disposition: attachment;filename=\"%s\"", a.Filename))
2023-12-01 18:33:04 +01:00
} else {
res = append(res, "Content-Disposition: attachment")
}
}
2023-12-04 13:48:11 +01:00
b64 := base64.StdEncoding.EncodeToString(a.Data)
for i := 0; i < len(b64); i += 80 {
res = append(res, b64[i:min(i+80, len(b64))])
}
res = append(res)
2023-12-01 18:33:04 +01:00
return res
}