-
Notifications
You must be signed in to change notification settings - Fork 0
/
smtpemail.go
46 lines (40 loc) · 1000 Bytes
/
smtpemail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package smtpemail
import (
"bytes"
"crypto/tls"
"fmt"
"net/smtp"
"net/textproto"
"os"
"github.com/jordan-wright/email"
"github.com/yuin/goldmark"
)
const SMTPHost = "smtp-relay.sendinblue.com"
const SMTPPort = "465"
func Send(to, from, subject, markdown, attachment string) (err error) {
smtpAuth := os.Getenv("SMTPAUTH")
smtpPass := os.Getenv("SMTPPASS")
if smtpAuth == "" || smtpPass == "" {
err = fmt.Errorf("Must define environmental variables SMTPAUTH and SMTPPASS")
}
var buf bytes.Buffer
if err = goldmark.Convert([]byte(markdown), &buf); err != nil {
return
}
e := &email.Email{
To: []string{to},
From: from,
Subject: subject,
Text: []byte(markdown),
HTML: buf.Bytes(),
Headers: textproto.MIMEHeader{},
}
if attachment != "" {
e.AttachFile(attachment)
}
err = e.SendWithTLS(SMTPHost+":"+SMTPPort, smtp.PlainAuth("", smtpAuth, smtpPass, SMTPHost), &tls.Config{
InsecureSkipVerify: true,
ServerName: SMTPHost,
})
return
}