summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorYves Fischer <yvesf+git@xapek.org>2021-02-18 23:36:31 +0100
committerYves Fischer <yvesf+git@xapek.org>2021-02-18 23:36:31 +0100
commitc440fdac0c6637069d41fc8fd3506ec31e52a469 (patch)
tree25e6d07092d883d87409d7e5d3a8948c36ac5707 /main.go
parent3fea46a388d0b4db27ae214259ba085528e03afa (diff)
downloadsmtp-forward-c440fdac0c6637069d41fc8fd3506ec31e52a469.tar.gz
smtp-forward-c440fdac0c6637069d41fc8fd3506ec31e52a469.zip
retry
Diffstat (limited to 'main.go')
-rw-r--r--main.go18
1 files changed, 15 insertions, 3 deletions
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)