@@ -183,12 +183,9 @@ func (c *Client) greet() error {
183
183
c .conn .SetDeadline (time .Now ().Add (c .CommandTimeout ))
184
184
defer c .conn .SetDeadline (time.Time {})
185
185
186
- _ , _ , err := c .text . ReadResponse (220 )
186
+ _ , _ , err := c .readResponse (220 )
187
187
if err != nil {
188
188
c .text .Close ()
189
- if protoErr , ok := err .(* textproto.Error ); ok {
190
- return toSMTPErr (protoErr )
191
- }
192
189
return err
193
190
}
194
191
@@ -237,6 +234,14 @@ func (c *Client) Hello(localName string) error {
237
234
return c .hello ()
238
235
}
239
236
237
+ func (c * Client ) readResponse (expectCode int ) (int , string , error ) {
238
+ code , msg , err := c .text .ReadResponse (expectCode )
239
+ if protoErr , ok := err .(* textproto.Error ); ok {
240
+ err = toSMTPErr (protoErr )
241
+ }
242
+ return code , msg , err
243
+ }
244
+
240
245
// cmd is a convenience function that sends a command and returns the response
241
246
// textproto.Error returned by c.text.ReadResponse is converted into SMTPError.
242
247
func (c * Client ) cmd (expectCode int , format string , args ... interface {}) (int , string , error ) {
@@ -249,15 +254,8 @@ func (c *Client) cmd(expectCode int, format string, args ...interface{}) (int, s
249
254
}
250
255
c .text .StartResponse (id )
251
256
defer c .text .EndResponse (id )
252
- code , msg , err := c .text .ReadResponse (expectCode )
253
- if err != nil {
254
- if protoErr , ok := err .(* textproto.Error ); ok {
255
- smtpErr := toSMTPErr (protoErr )
256
- return code , smtpErr .Message , smtpErr
257
- }
258
- return code , msg , err
259
- }
260
- return code , msg , nil
257
+
258
+ return c .readResponse (expectCode )
261
259
}
262
260
263
261
// helo sends the HELO greeting to the server. It should be used only when the
@@ -555,10 +553,10 @@ func (d *dataCloser) Close() error {
555
553
if d .c .lmtp {
556
554
for expectedResponses > 0 {
557
555
rcpt := d .c .rcpts [len (d .c .rcpts )- expectedResponses ]
558
- if _ , _ , err := d .c .text . ReadResponse (250 ); err != nil {
559
- if protoErr , ok := err .(* textproto. Error ); ok {
556
+ if _ , _ , err := d .c .readResponse (250 ); err != nil {
557
+ if smtpErr , ok := err .(* SMTPError ); ok {
560
558
if d .statusCb != nil {
561
- d .statusCb (rcpt , toSMTPErr ( protoErr ) )
559
+ d .statusCb (rcpt , smtpErr )
562
560
}
563
561
} else {
564
562
return err
@@ -569,11 +567,8 @@ func (d *dataCloser) Close() error {
569
567
expectedResponses --
570
568
}
571
569
} else {
572
- _ , _ , err := d .c .text . ReadResponse (250 )
570
+ _ , _ , err := d .c .readResponse (250 )
573
571
if err != nil {
574
- if protoErr , ok := err .(* textproto.Error ); ok {
575
- return toSMTPErr (protoErr )
576
- }
577
572
return err
578
573
}
579
574
}
0 commit comments