1+ use anyhow:: Context ;
12use askama:: Template ;
23use aws_config:: BehaviorVersion ;
34use chrono:: { DateTime , NaiveTime , Utc } ;
@@ -13,13 +14,20 @@ use serde_json::Value;
1314use std:: collections:: HashMap ;
1415use std:: env;
1516use std:: sync:: Arc ;
16- use tracing:: { error, info, warn } ;
17+ use tracing:: { error, info} ;
1718
1819const 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 } )
0 commit comments