mail/send.go (view raw)
1package mail
2
3import (
4 "fmt"
5 "log"
6 "os"
7
8 "git.icyphox.sh/forlater/navani/reader"
9 "github.com/joegrasse/mail"
10 "github.com/joho/godotenv"
11)
12
13func init() {
14 err := godotenv.Load()
15 if err != nil {
16 log.Fatal("error loading .env file")
17 }
18}
19
20func SendArticle(article *reader.Article, to string) error {
21 var (
22 EMAIL_USER_SECRET = os.Getenv("EMAIL_USER_SECRET")
23 EMAIL_PASSWORD = os.Getenv("EMAIL_PASSWORD")
24 EMAIL_FROM = os.Getenv("EMAIL_FROM")
25 SMTP_HOST = os.Getenv("SMTP_HOST")
26 SMTP_PORT = os.Getenv("SMTP_PORT")
27 )
28
29 htmlContent, err := RenderTemplate("html.tpl", article)
30 if err != nil {
31 return err
32 }
33
34 plainContent, err := reader.MakePlaintext(htmlContent)
35
36 email := mail.New()
37 email.Encryption = mail.EncryptionTLS
38 email.SetFrom(fmt.Sprintf("saved forlater <%s>", EMAIL_FROM))
39 email.AddTo(to)
40 email.SetSubject(article.Title)
41 email.SetBody("text/plain", string(plainContent))
42 email.AddAlternative("text/html", string(htmlContent))
43 email.Username = EMAIL_USER_SECRET
44 email.Password = EMAIL_PASSWORD
45
46 err = email.Send(SMTP_HOST + ":" + SMTP_PORT)
47 if err != nil {
48 return err
49 }
50
51 return nil
52}