Sfoglia il codice sorgente

Refactor Messenger/Emailer to accept attachments

Kailash Nadh 6 anni fa
parent
commit
81d3046374
5 ha cambiato i file con 36 aggiunte e 9 eliminazioni
  1. 1 1
      campaigns.go
  2. 1 1
      manager/manager.go
  3. 21 5
      messenger/emailer.go
  4. 11 1
      messenger/messenger.go
  5. 2 1
      notifications.go

+ 1 - 1
campaigns.go

@@ -509,7 +509,7 @@ func sendTestMessage(sub *models.Subscriber, camp *models.Campaign, app *App) er
 			fmt.Sprintf("Error rendering message: %v", err))
 	}
 
-	if err := app.Messenger.Push(camp.FromEmail, []string{sub.Email}, camp.Subject, m.Body); err != nil {
+	if err := app.Messenger.Push(camp.FromEmail, []string{sub.Email}, camp.Subject, m.Body, nil); err != nil {
 		return err
 	}
 

+ 1 - 1
manager/manager.go

@@ -238,7 +238,7 @@ func (m *Manager) SpawnWorkers() {
 					msg.from,
 					[]string{msg.to},
 					msg.Campaign.Subject,
-					msg.Body)
+					msg.Body, nil)
 				if err != nil {
 					m.logger.Printf("error sending message in campaign %s: %v",
 						msg.Campaign.Name, err)

+ 21 - 5
messenger/emailer.go

@@ -66,7 +66,7 @@ func (e *emailer) Name() string {
 }
 
 // Push pushes a message to the server.
-func (e *emailer) Push(fromAddr string, toAddr []string, subject string, m []byte) error {
+func (e *emailer) Push(fromAddr string, toAddr []string, subject string, m []byte, atts []*Attachment) error {
 	var key string
 
 	// If there are more than one SMTP servers, send to a random
@@ -77,12 +77,28 @@ func (e *emailer) Push(fromAddr string, toAddr []string, subject string, m []byt
 		key = e.serverNames[0]
 	}
 
+	// Are there attachments?
+	var files []*email.Attachment
+	if atts != nil {
+		files = make([]*email.Attachment, 0, len(atts))
+		for _, f := range atts {
+			a := &email.Attachment{
+				Filename: f.Name,
+				Header:   f.Header,
+				Content:  make([]byte, len(f.Content)),
+			}
+			copy(a.Content, f.Content)
+			files = append(files, a)
+		}
+	}
+
 	srv := e.servers[key]
 	err := srv.mailer.Send(&email.Email{
-		From:    fromAddr,
-		To:      toAddr,
-		Subject: subject,
-		HTML:    m,
+		From:        fromAddr,
+		To:          toAddr,
+		Subject:     subject,
+		HTML:        m,
+		Attachments: files,
 	}, srv.SendTimeout)
 
 	return err

+ 11 - 1
messenger/messenger.go

@@ -1,10 +1,20 @@
 package messenger
 
+import "net/textproto"
+
 // Messenger is an interface for a generic messaging backend,
 // for instance, e-mail, SMS etc.
 type Messenger interface {
 	Name() string
 
-	Push(fromAddr string, toAddr []string, subject string, message []byte) error
+	Push(fromAddr string, toAddr []string, subject string, message []byte, atts []*Attachment) error
 	Flush() error
 }
+
+// Attachment represents a file or blob attachment that can be
+// sent along with a message by a Messenger.
+type Attachment struct {
+	Name    string
+	Header  textproto.MIMEHeader
+	Content []byte
+}

+ 2 - 1
notifications.go

@@ -22,7 +22,8 @@ func sendNotification(tpl, subject string, data map[string]interface{}, app *App
 	err = app.Messenger.Push(app.Constants.FromEmail,
 		app.Constants.NotifyEmails,
 		subject,
-		b.Bytes())
+		b.Bytes(),
+		nil)
 	if err != nil {
 		app.Logger.Printf("error sending admin notification (%s): %v", subject, err)
 		return err