@@ -20,6 +20,8 @@ const (
2020 MaxLineLength = 76 // MaxLineLength is the maximum line length pre RFC 2045
2121 DefaultContentType = "text/plain; charset=us-ascii" // email.ContentType is email default Content-Type according to RFC 2045, section 5.2
2222
23+ StrFileNameParam = "filename" // mime request file name param
24+
2325 StrContentType = "Content-Type"
2426 StrContentDisposition = "Content-Disposition"
2527 StrReplyTo = "Reply-To"
@@ -129,7 +131,7 @@ func NewEmailFromReader(r io.Reader) (*Email, error) {
129131 if err != nil {
130132 return em , err
131133 }
132- fileName , fileNameDefined := params ["fileName" ]
134+ fileName , fileNameDefined := params [StrFileNameParam ]
133135 if cd == "attachment" || (cd == "inline" && fileNameDefined ) {
134136 _ , err := em .Attach (bytes .NewReader (p .body ), fileName , ct )
135137 if err != nil {
@@ -153,14 +155,21 @@ func NewEmailFromReader(r io.Reader) (*Email, error) {
153155// Required params include an io.Reader, the desired fileName for the attachment, and the Content-Type
154156// the func will return the create Attachment for reference, as well as nil for the error, if successful.
155157func (e * Email ) Attach (r io.Reader , fileName string , contentType string ) (a * Attachment , err error ) {
158+ return e .AttachWithHeaders (r , fileName , contentType , textproto.MIMEHeader {})
159+ }
160+
161+ // AttachWithHeaders is used to attach content from an io.Reader to the email. Required parameters include an io.Reader,
162+ // the desired filename for the attachment, the Content-Type and the original MIME headers.
163+ // The function will return the created Attachment for reference, as well as nil for the error, if successful.
164+ func (e * Email ) AttachWithHeaders (r io.Reader , fileName string , contentType string , headers textproto.MIMEHeader ) (a * Attachment , err error ) {
156165 var buffer bytes.Buffer
157166 if _ , err = io .Copy (& buffer , r ); err != nil {
158167 return
159168 }
160169 at := & Attachment {
161170 FileName : fileName ,
162171 ContentType : contentType ,
163- Header : textproto. MIMEHeader {} ,
172+ Header : headers ,
164173 Content : buffer .Bytes (),
165174 }
166175 e .Attachments = append (e .Attachments , at )
0 commit comments