summaryrefslogtreecommitdiffstats
path: root/modules/mailer
diff options
context:
space:
mode:
authorPhilip Couling <couling@gmail.com>2016-12-25 13:55:22 +0000
committerKim "BKC" Carlbäcker <kim.carlbacker@gmail.com>2016-12-25 14:55:22 +0100
commitd4924d45d6d4e991240d207a834d8d6709781449 (patch)
treebdf6c450d7712ba17c2b06fba45cdeb0579090ec /modules/mailer
parent8de8ec027d1ca44f889b89a69b26c3a9b599bbb6 (diff)
downloadgitea-d4924d45d6d4e991240d207a834d8d6709781449.tar.gz
gitea-d4924d45d6d4e991240d207a834d8d6709781449.zip
Implement sendmail (#355)
* Implemented sendmail. This piggybacks on existing configuration to keep the change simple * Changed privicy of new sendSMTP and sendSendmail functions * Fixed Lint errors * Seperated SMTP and sendmail into their own senders * Making new structs private as they should not be used externally now * Added sendmail setting to ini file * Minor code cleanup
Diffstat (limited to 'modules/mailer')
-rw-r--r--modules/mailer/mailer.go60
1 files changed, 54 insertions, 6 deletions
diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go
index 3dc2c4e531..3201075f3f 100644
--- a/modules/mailer/mailer.go
+++ b/modules/mailer/mailer.go
@@ -11,6 +11,7 @@ import (
"net"
"net/smtp"
"os"
+ "os/exec"
"strings"
"time"
@@ -87,12 +88,12 @@ func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
return nil, nil
}
-// Sender mail sender
-type Sender struct {
+// Sender SMTP mail sender
+type smtpSender struct {
}
// Send send email
-func (s *Sender) Send(from string, to []string, msg io.WriterTo) error {
+func (s *smtpSender) Send(from string, to []string, msg io.WriterTo) error {
opts := setting.MailService
host, port, err := net.SplitHostPort(opts.Host)
@@ -195,14 +196,51 @@ func (s *Sender) Send(from string, to []string, msg io.WriterTo) error {
return client.Quit()
}
-func processMailQueue() {
- sender := &Sender{}
+// Sender sendmail mail sender
+type sendmailSender struct {
+}
+
+// Send send email
+func (s *sendmailSender) Send(from string, to []string, msg io.WriterTo) error {
+ var err error
+ var closeError error
+ var waitError error
+
+ args := []string{"-F", from, "-i"}
+ args = append(args, to...)
+ log.Trace("Sending with: %s %v", setting.MailService.SendmailPath, args)
+ cmd := exec.Command(setting.MailService.SendmailPath, args...)
+ pipe, err := cmd.StdinPipe()
+
+ if err != nil {
+ return err
+ }
+
+ if err = cmd.Start(); err != nil {
+ return err
+ }
+
+ _,err = msg.WriteTo(pipe)
+ // we MUST close the pipe or sendmail will hang waiting for more of the message
+ // Also we should wait on our sendmail command even if something fails
+ closeError = pipe.Close()
+ waitError = cmd.Wait()
+ if err != nil {
+ return err
+ } else if closeError != nil {
+ return closeError
+ } else {
+ return waitError
+ }
+}
+
+func processMailQueue() {
for {
select {
case msg := <-mailQueue:
log.Trace("New e-mail sending request %s: %s", msg.GetHeader("To"), msg.Info)
- if err := gomail.Send(sender, msg.Message); err != nil {
+ if err := gomail.Send(Sender, msg.Message); err != nil {
log.Error(3, "Fail to send emails %s: %s - %v", msg.GetHeader("To"), msg.Info, err)
} else {
log.Trace("E-mails sent %s: %s", msg.GetHeader("To"), msg.Info)
@@ -213,6 +251,9 @@ func processMailQueue() {
var mailQueue chan *Message
+// Sender sender for sending mail synchronously
+var Sender gomail.Sender
+
// NewContext start mail queue service
func NewContext() {
// Need to check if mailQueue is nil because in during reinstall (user had installed
@@ -222,6 +263,13 @@ func NewContext() {
return
}
+
+ if setting.MailService.UseSendmail {
+ Sender = &sendmailSender{}
+ } else {
+ Sender = &smtpSender{}
+ }
+
mailQueue = make(chan *Message, setting.MailService.QueueLength)
go processMailQueue()
}