storj/internal/mail/sender.go
Yaroslav Vorobiov 62b5598596
Smtp mail sending library (#1254)
* init

* fix typos and remove console log

* fix linter and typos

* move mail to internal

* naming enhancements and remove gmailauth

* add message

* fix linter

* fix comment tabulation

* fix comment

* change flow

* escape subject

* add q encoding to msg fields

* fix typo

* fix comment tabulation

* lil comment fix
2019-02-11 11:54:07 +02:00

78 lines
1.3 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
package mail
import (
"crypto/tls"
"net"
"net/mail"
"net/smtp"
"github.com/zeebo/errs"
)
// SMTPSender is smtp sender
type SMTPSender struct {
ServerAddress string
From mail.Address
Auth smtp.Auth
}
// SendEmail sends email message to the given recipient
func (sender *SMTPSender) SendEmail(msg *Message) error {
host, _, err := net.SplitHostPort(sender.ServerAddress)
if err != nil {
return err
}
client, err := smtp.Dial(sender.ServerAddress)
if err != nil {
return err
}
// close underlying connection
defer func() {
err = errs.Combine(err, client.Close())
}()
// send smtp hello or ehlo msg and establish connection over tls
err = client.StartTLS(&tls.Config{ServerName: host})
if err != nil {
return err
}
err = client.Auth(sender.Auth)
if err != nil {
return err
}
err = client.Mail(sender.From.Address)
if err != nil {
return err
}
// add recipients
for _, to := range msg.To {
err = client.Rcpt(to.Address)
if err != nil {
return err
}
}
data, err := client.Data()
if err != nil {
return err
}
defer func() {
err = errs.Combine(err, data.Close())
}()
_, err = data.Write(msg.Bytes())
if err != nil {
return err
}
return client.Quit()
}