// Attach attaches to the message an attachment as a file. Passing an empty
// content deletes the file with the same name if it was previously added.
Attach attaches to the message an attachment as a file. Passing an empty
content deletes the file with the same name if it was previously added.
// Attach attaches to the message an attachment as a file . Passing an empty // content deletes the file with the same name if it was previously added .
func (m *GoogleMessage) Attach(name string, data []byte, headers *textproto.MIMEHeader, inlineCID string) error {
	if len(data) == 0 {
		if m.parts != nil {
			delete(m.parts, name)
		}
		return nil
	}
	name = filepath.Base(name)
	switch name {
	case ".", "..", string(filepath.Separator):
		return fmt.Errorf("bad file name: %v", name)
	}
	var h = make(textproto.MIMEHeader)
	if headers != nil {
		h = *headers
	}
	var contentType = h.Get("Content-Type")
	if contentType == "" {
		if name == _bodyHTML {
			contentType = "text/html"
		} else {
			contentType = mime.TypeByExtension(filepath.Ext(name))
		}
		if contentType == "" {
			contentType = http.DetectContentType(data)
		}
		if contentType != "" {
			h.Set("Content-Type", contentType)
		}
	}
	var coding = h.Get("Content-Transfer-Encoding")
	if coding == "" || !((coding == "quoted-printable") || (coding == "base64")) {
		coding = "quoted-printable"
		if !strings.HasPrefix(contentType, "text") {
			if name == _body || name == _bodyHTML || name == _bodyTEXT {
				return fmt.Errorf("unsupported body content type: %v", contentType)
			}
			coding = "base64"
		}
		h.Set("Content-Transfer-Encoding", coding)
	}
	if name != _body && name != _bodyHTML && name != _bodyTEXT {
		disposition := h.Get("Content-Disposition")
		if disposition == "" {
			if inlineCID != "" {
				if cidRE.MatchString(inlineCID) == false {
					inlineCID = fmt.Sprintf("<%s>", inlineCID)
				}
				disposition = fmt.Sprintf("inline; filename=%s", name)
				h.Set("Content-ID", inlineCID)
			} else {
				disposition = fmt.Sprintf("attachment; filename=%s", name)
			}
			h.Set("Content-Disposition", disposition)
		}
	}
	if m.parts == nil {
		m.parts = make(map[string]*GooglePart)
	}
	m.parts[name] = &GooglePart{
		Header: h,
		Data:   data,
	}
	return nil
}
func ( m * GoogleMessage ) Attach ( name string , data [ ] byte , headers * textproto . MIMEHeader , inlineCID string ) error { if len ( data ) == 0 { if m . parts != nil { delete ( m . parts , name ) } return nil } name = filepath . Base ( name ) switch name { case " " , " " , string ( filepath . Separator ) : return fmt . Errorf ( " " , name ) } var h = make ( textproto . MIMEHeader ) if headers != nil { h = * headers } var contentType = h . Get ( " " ) if contentType == " " { if name == _bodyHTML { contentType = " " } else { contentType = mime . TypeByExtension ( filepath . Ext ( name ) ) } if contentType == " " { contentType = http . DetectContentType ( data ) } if contentType != " " { h . Set ( " " , contentType ) } } var coding = h . Get ( " " ) if coding == " " || ! ( ( coding == " " ) || ( coding == " " ) ) { coding = " " if ! strings . HasPrefix ( contentType , " " ) { if name == _body || name == _bodyHTML || name == _bodyTEXT { return fmt . Errorf ( " " , contentType ) } coding = " " } h . Set ( " " , coding ) } if name != _body && name != _bodyHTML && name != _bodyTEXT { disposition := h . Get ( " " ) if disposition == " " { if inlineCID != " " { if cidRE . MatchString ( inlineCID ) == false { inlineCID = fmt . Sprintf ( " " , inlineCID ) } disposition = fmt . Sprintf ( " " , name ) h . Set ( " " , inlineCID ) } else { disposition = fmt . Sprintf ( " " , name ) } h . Set ( " " , disposition ) } } if m . parts == nil { m . parts = make ( map [ string ] * GooglePart ) } m . parts [ name ] = & GooglePart { Header : h , Data : data , } return nil }
Attach attaches to the message an attachment as a file.
Attach attaches to the message an attachment as a file .