From c440fdac0c6637069d41fc8fd3506ec31e52a469 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Thu, 18 Feb 2021 23:36:31 +0100 Subject: retry --- main.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 37ebedc..d32fb01 100644 --- a/main.go +++ b/main.go @@ -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) -- cgit v1.2.1