1+ // Package email allows to send email messages.
2+ // The package is used internally by the mail package.
13package email
24
35import (
@@ -26,8 +28,10 @@ import (
2628)
2729
2830const (
29- MaxLineLength = 76 // MaxLineLength is the maximum line length per RFC 2045
30- DefaultContentType = "text/plain; charset=us-ascii" // DefaultContentType is the default Content-Type according to RFC 2045, section 5.2
31+ // MaxLineLength is the maximum line length per RFC 2045
32+ MaxLineLength = 76
33+ // DefaultContentType is the default Content-Type according to RFC 2045, section 5.2
34+ DefaultContentType = "text/plain; charset=us-ascii"
3135)
3236
3337var maxBigInt = big .NewInt (math .MaxInt64 )
@@ -144,10 +148,10 @@ func NewFromReader(r io.Reader) (*Email, error) {
144148 continue
145149 }
146150 }
147- switch {
148- case contentType == "text/plain" :
151+ switch contentType {
152+ case "text/plain" :
149153 msg .Text = p .body
150- case contentType == "text/html" :
154+ case "text/html" :
151155 msg .HTML = p .body
152156 }
153157 }
@@ -174,11 +178,11 @@ func (e *Email) Attach(r io.Reader, filename string, contentType string) (*Attac
174178// AttachFile is used to attach content to the email.
175179// It attempts to open the file referenced by filename and, if successful, creates an Attachment.
176180func (e * Email ) AttachFile (filename string ) (* Attachment , error ) {
177- f , err := os .Open (filename )
181+ f , err := os .Open (filepath . Clean ( filename ) )
178182 if err != nil {
179183 return nil , apperror .NewError ("could not open attachment file" ).AddError (err )
180184 }
181- defer f .Close ( )
185+ defer apperror . Catch ( f .Close , "could not close attachment file" )
182186
183187 ct := mime .TypeByExtension (filepath .Ext (filename ))
184188 basename := filepath .Base (filename )
@@ -301,7 +305,10 @@ func (e *Email) Bytes() ([]byte, error) {
301305 }
302306
303307 if isMixed || isAlternative {
304- relatedWriter .Close ()
308+ err = relatedWriter .Close ()
309+ if err != nil {
310+ return nil , apperror .NewError ("could not close multipart/related part" ).AddError (err )
311+ }
305312 }
306313 }
307314 }
@@ -370,7 +377,7 @@ func (e *Email) Send(address string, auth smtp.Auth, helo string) error {
370377 if err != nil {
371378 return apperror .NewError ("could not dial SMTP connection" ).AddError (err )
372379 }
373- defer conn .Close ( )
380+ defer apperror . Catch ( conn .Close , "could not close SMTP connection" )
374381
375382 // Send custom HELO
376383 err = conn .Hello (helo )
@@ -447,12 +454,12 @@ func (e *Email) SendWithTLS(address string, auth smtp.Auth, config *tls.Config,
447454 if err != nil {
448455 return apperror .NewError ("could not dial TLS connection" ).AddError (err )
449456 }
450- defer conn .Close ( )
457+ defer apperror . Catch ( conn .Close , "could not close TLS connection" )
451458 c , err := smtp .NewClient (conn , strings .Split (address , ":" )[0 ])
452459 if err != nil {
453460 return apperror .NewError ("could not create SMTP client" ).AddError (err )
454461 }
455- defer c .Quit ( )
462+ defer apperror . Catch ( c .Quit , "could not quit SMTP session" )
456463
457464 // Send custom HELO if provided (after connection but before auth)
458465 if helo != "" {
@@ -524,7 +531,7 @@ func (e *Email) SendWithStartTLS(address string, auth smtp.Auth, config *tls.Con
524531 if err != nil {
525532 return apperror .NewError ("could not dial SMTP connection" ).AddError (err )
526533 }
527- defer conn .Close ( )
534+ defer apperror . Catch ( conn .Close , "could not close SMTP connection" )
528535
529536 // Send custom HELO if provided (before STARTTLS)
530537 if helo != "" {
@@ -836,13 +843,13 @@ func headerToBytes(buff io.Writer, header textproto.MIMEHeader) error {
836843 if err != nil {
837844 return apperror .NewError ("could not write header field separator" ).AddError (err )
838845 }
839- switch {
840- case field == "Content-Type" || field == "Content-Disposition" :
846+ switch field {
847+ case "Content-Type" , "Content-Disposition" :
841848 _ , err := buff .Write ([]byte (subval ))
842849 if err != nil {
843850 return apperror .NewError ("could not write header field value" ).AddError (err )
844851 }
845- case field == "From" || field == "To" || field == "Cc" || field == "Bcc" :
852+ case "From" , "To" , "Cc" , "Bcc" :
846853 _ , err := buff .Write ([]byte (subval ))
847854 if err != nil {
848855 return apperror .NewError ("could not write header field value" ).AddError (err )
0 commit comments