all repos — navani @ b0009eb8c652bd24d944a649479a8a53ed8719d7

forlater's primary mail processing service

Rework MIME handling
Anirudh Oppiliappan x@icyphox.sh
Tue, 09 Nov 2021 11:42:22 +0530
commit

b0009eb8c652bd24d944a649479a8a53ed8719d7

parent

f31c9400f01882bebd7c151acfa37619a33d446b

2 files changed, 52 insertions(+), 11 deletions(-)

jump to
M mail/send.gomail/send.go

@@ -20,30 +20,71 @@ log.Fatal("error loading .env file")

} } -func SendArticle(article *reader.Article, to string, readable bool) error { - var EMAIL_FROM = os.Getenv("EMAIL_FROM") +func htmlMail(article *reader.Article) (*mail.Email, error) { htmlContent, err := RenderTemplate("html.tpl", article) if err != nil { - return err + return &mail.Email{}, err } htmlAbs, err := rel2abs.Convert(htmlContent, article.URL.String()) if err != nil { - htmlAbs = htmlContent + return &mail.Email{}, err } plainContent, err := reader.MakePlaintext(htmlAbs) if err != nil { - return fmt.Errorf("making plaintext: %w\n", err) + 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 { - email.SetSubject(article.Title) - email.SetBodyData(mail.TextPlain, plainContent) - email.AddAlternative(mail.TextHTML, string(htmlContent)) + 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(
M main.gomain.go

@@ -38,8 +38,8 @@ log.Printf("reader fetch: %v\n", err)

} article, err := reader.Readable(resp.Body, parsedURL) - if (err == nil) && (resp.MIMEType == "text/html" || resp.MIMEType == "text/plain" || resp.MIMEType == "html") { - err = mail.SendArticle(&article, m.From, true) + if err == nil { + err = mail.SendArticle(&article, resp.MIMEType, m.From, true) if err != nil { log.Printf("error sending mail to: %s: %v\n", m.From, err) } else {

@@ -47,7 +47,7 @@ log.Printf("sent mail to %s: %s\n", m.From, article.Title)

} } else { log.Printf("not readable: %s: %s\n", article.URL.String(), resp.MIMEType) - err := mail.SendArticle(&article, m.From, false) + err := mail.SendArticle(&article, resp.MIMEType, m.From, false) if err != nil { log.Printf("error sending mail to: %s: %v\n", m.From, err) }