email
Emailing with optional limiter. It will send no more then limiterMax emails during limiterPeriod.
Install
go get github.com/kaibox-git/email
Usage
import (
"github.com/kaibox-git/email"
"github.com/kaibox-git/email/smtpclient"
)
...
host := `localhost:25`
connTimeout := time.Second // for local smtp server
/*
Only 20 emails per 30 minutes. The rest is ignored.
This is useful for notification of errors, but has a limitation if emailing is too often.
In this case send info to log file.
*/
emailNumber := 20
period := 30 * time.Minute
emailSender, err := smtpclient.New(host, connTimeout, emailNumber, period)
if err != nil {
println(err.Error())
os.Exit(1)
}
// plain text with limiter
emailSender.Send(&email.Data{
From: mail.Address{
Name: `Robot`,
Address: `robot@domain.com`,
},
To: []mail.Address{
{
Name: `Test address`,
Address: `test@domain.com`,
},
},
Subject: `test subject`,
Body: `test message`,
WithLimiter: true,
})
// html body for 2 addresses with no limiter
body := `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Emailing HTML format</title>
</head>
<body>
<h1>Test HTML format</h1>
<p>This is a test body.</p>
</body>
</html>`
emailSender.Send(&email.Data{
From: mail.Address{
Name: `Robot`,
Address: `robot@domain.com`,
},
To: []mail.Address{
{
Name: `Test address`,
Address: `test@domain.com`,
},
{
Name: `Test address 2`,
Address: `test2@domain.com`,
},
},
Subject: `test subject`,
Body: body,
WithLimiter: false,
})