Skip to content

Commit f3a818e

Browse files
authored
Add plaintext version of email (#66)
1 parent 2862491 commit f3a818e

File tree

4 files changed

+58
-19
lines changed

4 files changed

+58
-19
lines changed

src/digest_mailer.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ impl DigestMailer {
2323
pub async fn send_mail(
2424
&self,
2525
subject: &str,
26-
content: &str,
26+
html_content: &str,
27+
text_content: &str,
2728
recipient: &str,
2829
unsubscribe_url: &str,
2930
) -> Result<()> {
@@ -40,9 +41,16 @@ impl DigestMailer {
4041

4142
// Build email content
4243
let subject_content = Content::builder().data(subject).charset("UTF-8").build()?;
43-
let body_content = Content::builder().data(content).charset("UTF-8").build()?;
44+
let html_body = Content::builder()
45+
.data(html_content)
46+
.charset("UTF-8")
47+
.build()?;
48+
let text_body = Content::builder()
49+
.data(text_content)
50+
.charset("UTF-8")
51+
.build()?;
4452

45-
let body = Body::builder().html(body_content).build();
53+
let body = Body::builder().html(html_body).text(text_body).build();
4654
let message = Message::builder()
4755
.subject(subject_content)
4856
.body(body)

src/main.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::Context;
12
use askama::Template;
23
use aws_config::BehaviorVersion;
34
use chrono::{DateTime, NaiveTime, Utc};
@@ -13,13 +14,20 @@ use serde_json::Value;
1314
use std::collections::HashMap;
1415
use std::env;
1516
use std::sync::Arc;
16-
use tracing::{error, info, warn};
17+
use tracing::{error, info};
1718

1819
const MAX_CONCURRENT_EMAILS: usize = 10;
1920

2021
#[derive(Template)]
2122
#[template(path = "digest.html")]
22-
struct DigestTemplate<'a> {
23+
struct DigestHtmlTemplate<'a> {
24+
posts: &'a [Post],
25+
unsubscribe_url: &'a str,
26+
}
27+
28+
#[derive(Template)]
29+
#[template(path = "digest.txt")]
30+
struct DigestTextTemplate<'a> {
2331
posts: &'a [Post],
2432
unsubscribe_url: &'a str,
2533
}
@@ -150,24 +158,28 @@ async fn handler(_event: LambdaEvent<Value>) -> Result<(), Error> {
150158
base_url, subscriber.unsubscribe_token
151159
);
152160

153-
let tmpl = DigestTemplate {
161+
let html_content = DigestHtmlTemplate {
154162
posts,
155163
unsubscribe_url: &unsubscribe_url,
156-
};
157-
let content = match tmpl.render() {
158-
Ok(c) => c,
159-
Err(e) => {
160-
warn!(
161-
email = subscriber.email,
162-
error = %e,
163-
"Failed to render template"
164-
);
165-
return Err(anyhow::anyhow!("Template render failed: {}", e));
166-
}
167-
};
164+
}
165+
.render()
166+
.context("Failed to render HTML template")?;
167+
168+
let text_content = DigestTextTemplate {
169+
posts,
170+
unsubscribe_url: &unsubscribe_url,
171+
}
172+
.render()
173+
.context("Failed to render text template")?;
168174

169175
mailer
170-
.send_mail(subject, &content, &subscriber.email, &unsubscribe_url)
176+
.send_mail(
177+
subject,
178+
&html_content,
179+
&text_content,
180+
&subscriber.email,
181+
&unsubscribe_url,
182+
)
171183
.await
172184
}
173185
})

templates/digest.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
</head>
6+
<body>
17
Your daily Hacker News digest:
28
<br />
39
{% for post in posts %}
@@ -15,3 +21,5 @@
1521
<p style="font-size: 12px; color: #666;">
1622
<a href="{{ unsubscribe_url }}" style="color: #666;">Unsubscribe</a> from this digest.
1723
</p>
24+
</body>
25+
</html>

templates/digest.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Your daily Hacker News digest:
2+
3+
{% for post in posts %}
4+
{{ post.title }}
5+
{{ post.points }} points
6+
{% if let Some(url) = post.url %}Link: {{ url }}
7+
{% endif %}Comments: https://news.ycombinator.com/item?id={{ post.object_id }}
8+
9+
{% endfor %}
10+
---
11+
Unsubscribe: {{ unsubscribe_url }}

0 commit comments

Comments
 (0)