Add sendWarning test

This commit is contained in:
Roland Shoemaker 2015-07-24 14:53:50 -07:00
parent 960af78955
commit a69021f918
2 changed files with 14 additions and 10 deletions

View File

@ -37,7 +37,7 @@ type mailer struct {
stats statsd.Statter
log *blog.AuditLogger
dbMap *gorp.DbMap
Mailer *mail.Mailer
Mailer mail.Mailer
EmailTemplate *template.Template
WarningDays []int
}
@ -86,8 +86,7 @@ func (m *mailer) findExpiringCertificates() error {
if err != nil {
return err
}
expiresIn := int(time.Now().Sub(parsedCert.NotAfter).Hours() / 24)
err = m.sendWarning(parsedCert, reg, expiresIn)
err = m.sendWarning(parsedCert, reg.Contact)
if err != nil {
return err
}
@ -133,9 +132,10 @@ func (m *mailer) findExpiringCertificates() error {
return err
}
func (m *mailer) sendWarning(parsedCert *x509.Certificate, reg core.Registration, expiresIn int) error {
func (m *mailer) sendWarning(parsedCert *x509.Certificate, contacts []core.AcmeURL) error {
expiresIn := int(parsedCert.NotAfter.Sub(time.Now()).Hours() / 24)
emails := []string{}
for _, contact := range reg.Contact {
for _, contact := range contacts {
if contact.Scheme == "mailto" {
emails = append(emails, contact.Opaque)
}

View File

@ -10,8 +10,12 @@ import (
"net/smtp"
)
// Mailer defines a mail transfer agent to use for sending mail
type Mailer struct {
type Mailer interface {
SendMail([]string, string) error
}
// MailerImpl defines a mail transfer agent to use for sending mail
type MailerImpl struct {
Server string
Port string
Auth smtp.Auth
@ -20,9 +24,9 @@ type Mailer struct {
// NewMailer constructs a Mailer to represent an account on a particular mail
// transfer agent.
func NewMailer(server, port, username, password string) Mailer {
func NewMailer(server, port, username, password string) MailerImpl {
auth := smtp.PlainAuth("", username, password, server)
return Mailer{
return MailerImpl{
Server: server,
Port: port,
Auth: auth,
@ -32,7 +36,7 @@ func NewMailer(server, port, username, password string) Mailer {
// SendMail sends an email to the provided list of recipients. The email body
// is simple text.
func (m *Mailer) SendMail(to []string, msg string) (err error) {
func (m *MailerImpl) SendMail(to []string, msg string) (err error) {
err = smtp.SendMail(net.JoinHostPort(m.Server, m.Port), m.Auth, m.From, to, []byte(msg))
return
}