mail/send.go (view raw)
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
package mail import ( "fmt" "io" "log" "mime" "os" "git.icyphox.sh/forlater/navani/reader" "git.icyphox.sh/rel2abs" "github.com/joho/godotenv" mail "github.com/xhit/go-simple-mail/v2" ) func init() { err := godotenv.Load() if err != nil { log.Fatal("error loading .env file") } } func htmlMail(article *reader.Article) (*mail.Email, error) { htmlContent, err := RenderTemplate("html.tpl", article) if err != nil { return &mail.Email{}, err } htmlAbs, err := rel2abs.Convert(htmlContent, article.URL.String()) if err != nil { return &mail.Email{}, err } plainContent, err := reader.MakePlaintext(htmlAbs) if err != nil { return &mail.Email{}, fmt.Errorf("making plaintext: %w\n", err) } email := mail.NewMSG() email.SetBodyData(mail.TextPlain, plainContent) email.AddAlternative(mail.TextHTML, string(htmlContent)) return email, nil } func plainMail(article *reader.Article) (*mail.Email, error) { email := mail.NewMSG() email.SetBodyData(mail.TextPlain, []byte(article.TextContent)) return email, nil } func SendArticle(article *reader.Article, mime string, to string, readable bool) error { var ( EMAIL_FROM = os.Getenv("EMAIL_FROM") err error email *mail.Email ) switch mime { case "text/html": email, err = htmlMail(article) if err != nil { return err } case "html": // Exception for weird sites email, err = htmlMail(article) if err != nil { return err } case "text/plain": email, err = plainMail(article) if err != nil { return err } default: readable = false } email.SetFrom(fmt.Sprintf("saved forlater <%s>", EMAIL_FROM)) email.AddTo(to) if readable { if article.Title != "" { email.SetSubject(article.Title) } else { email.SetSubject(article.URL.String()) } } else { email.SetSubject("[forlater.email] Unable to read your link") email.SetBody(mail.TextPlain, fmt.Sprintf( "We were unable to parse your link: %s", article.URL.String(), )) } c, err := mailClient() if err != nil { return fmt.Errorf("mail: %w\n", err) } err = email.Send(c) if err != nil { return err } return nil } func SendAttachment(r reader.Response, to string, url string) error { var EMAIL_FROM = os.Getenv("EMAIL_FROM") email := mail.NewMSG() email.SetFrom(fmt.Sprintf("saved forlater <%s>", EMAIL_FROM)) email.AddTo(to) b, err := io.ReadAll(r.Body) if err != nil { return fmt.Errorf("read attachment: %w\n", err) } fmt.Println(len(b)) ext, _ := mime.ExtensionsByType(r.MIMEType) var name string if ext != nil { name = "file" + ext[0] } else { name = "file" } email.SetSubject(url) email.Attach(&mail.File{MimeType: r.MIMEType, Data: b, Name: name, Inline: true}) email.SetBody(mail.TextPlain, fmt.Sprintf(`That didn't look like a HTML page; we found %s. We've attached it to this email.`, r.MIMEType)) c, err := mailClient() if err != nil { return fmt.Errorf("mail: %w\n", err) } err = email.Send(c) if err != nil { return err } return nil } |