Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change e-mail package from gomail to net/smtp #211

Merged
merged 1 commit into from
Oct 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "from@address.com"
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 = "from@address.com"
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 = "from@address.com"
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 = "from@address.com"
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 = "from@address.com"
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