Skip to content

Commit

Permalink
Merge pull request #211 from sadayuki-matsuno/fast_mail_package
Browse files Browse the repository at this point in the history
change e-mail package from gomail to net/smtp
  • Loading branch information
kotakanbe authored Oct 11, 2016
2 parents a8dc886 + cfc9e06 commit fbfc14d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ notifyUsers = ["@username"]
[mail]
smtpAddr = "smtp.gmail.com"
smtpPort = "465"
smtpPort = "587"
user = "username"
password = "password"
from = "[email protected]"
Expand Down Expand Up @@ -455,7 +455,7 @@ host = "172.31.4.82"
```
[mail]
smtpAddr = "smtp.gmail.com"
smtpPort = "465"
smtpPort = "587"
user = "username"
password = "password"
from = "[email protected]"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ notifyUsers = ["@username"]
[mail]
smtpAddr = "smtp.gmail.com"
smtpPort = "465"
smtpPort = "587"
user = "username"
password = "password"
from = "[email protected]"
Expand Down Expand Up @@ -457,7 +457,7 @@ You can customize your configuration using this template.
```
[mail]
smtpAddr = "smtp.gmail.com"
smtpPort = "465"
smtpPort = "587"
user = "username"
password = "password"
from = "[email protected]"
Expand Down
2 changes: 1 addition & 1 deletion commands/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ notifyUsers = ["@username"]
[mail]
smtpAddr = "smtp.gmail.com"
smtpPort = "465"
smtpPort = "587"
user = "username"
password = "password"
from = "[email protected]"
Expand Down
2 changes: 0 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,3 @@ import:
- package: golang.org/x/net
subpackages:
- context
- package: gopkg.in/gomail.v2
59 changes: 38 additions & 21 deletions report/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package report

import (
"crypto/tls"
"fmt"
"strconv"
"net"
"net/mail"
"net/smtp"
"strings"

"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/models"
"gopkg.in/gomail.v2"
)

// MailWriter send mail
Expand All @@ -33,37 +34,53 @@ type MailWriter struct{}
func (w MailWriter) Write(scanResults []models.ScanResult) (err error) {
conf := config.Conf
for _, s := range scanResults {
m := gomail.NewMessage()
m.SetHeader("From", conf.Mail.From)
m.SetHeader("To", conf.Mail.To...)
m.SetHeader("Cc", conf.Mail.Cc...)
to := strings.Join(conf.Mail.To[:], ", ")
cc := strings.Join(conf.Mail.Cc[:], ", ")
mailAddresses := append(conf.Mail.To, conf.Mail.Cc...)
if _, err := mail.ParseAddressList(strings.Join(mailAddresses[:], ", ")); err != nil {
return fmt.Errorf("Failed to parse mail addresses: %s", err)
}

subject := fmt.Sprintf("%s%s %s",
conf.Mail.SubjectPrefix,
s.ServerInfo(),
s.CveSummary(),
)
m.SetHeader("Subject", subject)

headers := make(map[string]string)
headers["From"] = conf.Mail.From
headers["To"] = to
headers["Cc"] = cc
headers["Subject"] = subject

var message string
for k, v := range headers {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}

var body string
if body, err = toPlainText(s); err != nil {
return err
}
m.SetBody("text/plain", body)
port, _ := strconv.Atoi(conf.Mail.SMTPPort)
d := gomail.NewPlainDialer(
conf.Mail.SMTPAddr,
port,
conf.Mail.User,
conf.Mail.Password,
)
message += "\r\n" + body

d.TLSConfig = &tls.Config{
InsecureSkipVerify: true,
}
smtpServer := net.JoinHostPort(conf.Mail.SMTPAddr, conf.Mail.SMTPPort)

err := smtp.SendMail(
smtpServer,
smtp.PlainAuth(
"",
conf.Mail.User,
conf.Mail.Password,
conf.Mail.SMTPAddr,
),
conf.Mail.From,
conf.Mail.To,
[]byte(message),
)

if err := d.DialAndSend(m); err != nil {
panic(err)
if err != nil {
return fmt.Errorf("Failed to send mails: %s", err)
}
}
return nil
Expand Down

0 comments on commit fbfc14d

Please sign in to comment.