Working on ignoring certs

pull/146/head
Jordan Wright 2016-02-11 12:53:00 -06:00
parent 7f381f861e
commit bbe97f5602
2 changed files with 16 additions and 20 deletions

View File

@ -4,12 +4,13 @@ import "errors"
// SMTP contains the attributes needed to handle the sending of campaign emails // SMTP contains the attributes needed to handle the sending of campaign emails
type SMTP struct { type SMTP struct {
SMTPId int64 `json:"-" gorm:"column:smtp_id; primary_key:yes"` SMTPId int64 `json:"-" gorm:"column:smtp_id; primary_key:yes"`
CampaignId int64 `json:"-" gorm:"column:campaign_id"` CampaignId int64 `json:"-" gorm:"column:campaign_id"`
Host string `json:"host"` Host string `json:"host"`
Username string `json:"username,omitempty"` Username string `json:"username,omitempty"`
Password string `json:"password,omitempty" sql:"-"` Password string `json:"password,omitempty" sql:"-"`
FromAddress string `json:"from_address"` FromAddress string `json:"from_address"`
IgnoreCertErrors bool `json:"ignore_cert_errors"`
} }
// ErrFromAddressNotSpecified is thrown when there is no "From" address // ErrFromAddressNotSpecified is thrown when there is no "From" address

View File

@ -143,10 +143,6 @@ func SendTestEmail(s *models.SendTestEmailRequest) error {
Subject: s.Template.Subject, Subject: s.Template.Subject,
From: s.SMTP.FromAddress, From: s.SMTP.FromAddress,
} }
var auth smtp.Auth
if s.SMTP.Username != "" && s.SMTP.Password != "" {
auth = smtp.PlainAuth("", s.SMTP.Username, s.SMTP.Password, strings.Split(s.SMTP.Host, ":")[0])
}
f, err := mail.ParseAddress(s.SMTP.FromAddress) f, err := mail.ParseAddress(s.SMTP.FromAddress)
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
@ -190,7 +186,7 @@ func SendTestEmail(s *models.SendTestEmailRequest) error {
e.Subject = string(subjBuff.Bytes()) e.Subject = string(subjBuff.Bytes())
e.To = []string{s.Email} e.To = []string{s.Email}
Logger.Printf("Sending Email to %s\n", s.Email) Logger.Printf("Sending Email to %s\n", s.Email)
err = e.Send(s.SMTP.Host, auth) err = sendMail(e, s.SMTP)
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
// For now, let's split the error and return // For now, let's split the error and return
@ -204,7 +200,7 @@ func SendTestEmail(s *models.SendTestEmailRequest) error {
// sendEmail is a copy of the net/smtp#SendMail function // sendEmail is a copy of the net/smtp#SendMail function
// that has the option to ignore TLS errors // that has the option to ignore TLS errors
// TODO: Find a more elegant way (maybe in the email lib?) to do this // TODO: Find a more elegant way (maybe in the email lib?) to do this
func sendMail(e email.Email, s models.SMTP, tlsConfig *tls.Config) error { func sendMail(e email.Email, s models.SMTP) error {
var auth smtp.Auth var auth smtp.Auth
if s.Username != "" && s.Password != "" { if s.Username != "" && s.Password != "" {
auth = smtp.PlainAuth("", s.Username, s.Password, strings.Split(s.Host, ":")[0]) auth = smtp.PlainAuth("", s.Username, s.Password, strings.Split(s.Host, ":")[0])
@ -239,18 +235,17 @@ func sendMail(e email.Email, s models.SMTP, tlsConfig *tls.Config) error {
return err return err
} }
defer c.Close() defer c.Close()
if err = c.Hello(); err != nil { if err = c.Hello("localhost"); err != nil {
return err return err
} }
// Use TLS if available // Use TLS if available
if ok, _ := c.Extension("STARTTLS"); ok { if ok, _ := c.Extension("STARTTLS"); ok {
host, _, _ := net.SplitHostPort(s.Host) host, _, _ := net.SplitHostPort(s.Host)
/* config := &tls.Config{
config := &tls.Config{ ServerName: host,
ServerName: host, InsecureSkipVerify: s.IgnoreCertErrors,
InsecureSkipVerify: s.IgnoreCertErrors, }
}*/ if err = c.StartTLS(config); err != nil {
if err = c.StartTLS(tlsConfig); err != nil {
return err return err
} }
} }
@ -262,7 +257,7 @@ func sendMail(e email.Email, s models.SMTP, tlsConfig *tls.Config) error {
} }
} }
} }
if err = c.Mail(from); err != nil { if err = c.Mail(from.Address); err != nil {
return err return err
} }
for _, addr := range to { for _, addr := range to {