Przeglądaj źródła

Email fixed message body

Signed-off-by: Vishal Rana <vr@labstack.com>
Vishal Rana 9 lat temu
rodzic
commit
0ede4d8d77
2 zmienionych plików z 28 dodań i 2 usunięć
  1. 8 2
      email/email.go
  2. 20 0
      email/email_test.go

+ 8 - 2
email/email.go

@@ -64,7 +64,6 @@ func (m *Message) writeFile(f *File, disposition string) {
 }
 
 func (e *Email) Send(m *Message) error {
-	// Construct message
 	m.buffer = new(bytes.Buffer)
 	m.boundary = random.String(16)
 	m.buffer.WriteString("MIME-Version: 1.0\r\n")
@@ -72,16 +71,23 @@ func (e *Email) Send(m *Message) error {
 	m.buffer.WriteString(fmt.Sprintf("Subject: %s\r\n", m.Subject))
 	m.buffer.WriteString(fmt.Sprintf("Content-Type: multipart/mixed; boundary=%s\r\n", m.boundary))
 	m.buffer.WriteString("\r\n")
+
+	// Message body
 	if m.TemplateName != "" {
 		buf := new(bytes.Buffer)
 		if err := e.Template.ExecuteTemplate(buf, m.TemplateName, m.TemplateData); err != nil {
 			return err
 		}
 		m.writeText(buf.String(), "text/plain")
-	} else {
+	} else if m.Text != "" {
 		m.writeText(m.Text, "text/plain")
+	} else if m.HTML != "" {
 		m.writeText(m.HTML, "text/html")
+	} else {
+		// TODO:
 	}
+
+	// Attachments / inlines
 	for _, f := range m.Inlines {
 		m.writeFile(f, "inline")
 	}

+ 20 - 0
email/email_test.go

@@ -1 +1,21 @@
 package email
+
+import (
+	"net/smtp"
+	"testing"
+)
+
+func TestSend(t *testing.T) {
+	// e := New("smtp.gmail.com:465")
+	e := New("smtp.elasticemail.com:2525")
+	// e.Auth = smtp.PlainAuth("", "vr@labstack.com", "Dream1980", "smtp.gmail.com")
+	e.Auth = smtp.PlainAuth("", "vr@labstack.com", "54643b5b-3284-4f33-89bf-d228951c527f", "smtp.elasticemail.com")
+	// err := smtp.SendMail("smtp.elasticemail.com:2525", e.Auth, "vr@labstack.com", []string{"ranavishal@gmail.com"}, []byte("body"))
+	// fmt.Println(err)
+	e.Send(&Message{
+		From:    "no-reply@labstack.com",
+		To:      "ranavishal@gmail.com",
+		Subject: "test",
+		Text:    "xxxxxxxxxxxxxxxxxxxxxxxxxx",
+	})
+}