diff options
-rw-r--r-- | main.go | 18 |
1 files changed, 15 insertions, 3 deletions
@@ -12,6 +12,7 @@ import ( "net/smtp" "net/textproto" "strings" + "time" "github.com/mhale/smtpd" "github.com/pkg/errors" @@ -85,9 +86,20 @@ func forward(targetEmail string, data []byte) error { builder.WriteString("\r\n") builder.Write(msgData) - err = smtp.SendMail(mxes[0].Host+":25", nil, `forwarder@localnet.cc`, []string{targetEmail}, builder.Bytes()) - if err != nil { - return errors.Wrap(err, `failed targetEmail send mail via smtp`) + var retryCount = 5 + for retryCount > 0 { + err = smtp.SendMail(mxes[0].Host+":25", nil, `forwarder@localnet.cc`, []string{targetEmail}, builder.Bytes()) + if err, ok := err.(*textproto.Error); ok { + if 400 <= err.Code && err.Code < 500 { + log.Printf(`retry sleep 120s count=%v code=%v`, retryCount, err.Code) + time.Sleep(120 * time.Second) + retryCount-- + continue + } + } + if err != nil { + return errors.Wrap(err, `failed targetEmail send mail via smtp`) + } } log.Printf("forwarded targetEmail=%v", targetEmail) |