// Attach a file to the email - may be a relative path. The file name that is sent in // the email will be the base file name. func (this *EM) Attach(fn string) *EM { this.doAltSetup() bfn := filepath.Base(fn) ext := filepath.Ext(fn) ct := mime.TypeByExtension(ext) attch := mailbuilder.NewSimplePart() attch.AddHeader("Content-Type", ct) attch.AddHeader("Content-Transfer-Encoding", "base64") attch.AddHeader("Content-Disposition", fmt.Sprintf(`attachment; filename=%q`, bfn)) //read and encode attachment content, _ := ioutil.ReadFile(fn) encoded := base64.StdEncoding.EncodeToString(content) //split the encoded file in lines (doesn't matter, but low enough not to hit a max limit) nbrLines := len(encoded) / this.lineMaxLength var buf bytes.Buffer for i := 0; i < nbrLines; i++ { buf.WriteString(encoded[i*this.lineMaxLength:(i+1)*this.lineMaxLength] + "\n") } buf.WriteString(encoded[nbrLines*this.lineMaxLength:]) attch.Content = buf.String() this.Mixed.AddPart(attch) return this }
// Send a HTML body with the email. func (this *EM) HtmlBody(s string) *EM { this.doAltSetup() html := mailbuilder.NewSimplePart() // add content/headers to html and text // html.AddHeader("Content-Type", "text/html; charset=utf8") // html.AddHeader("Content-Transfer-Encoding", "quoted-printable") html.AddHeader("Content-Type", "text/html; charset=us-ascii") html.Content = s this.Alt.AddPart(html) return this }