@@ -82,6 +82,14 @@ struct EmailPayload {
8282 // message_id: String,
8383 html : String ,
8484 text : String ,
85+ attachments : Vec < Attachment > ,
86+ }
87+
88+ #[ derive( Serialize , Clone , Debug ) ]
89+ struct Attachment {
90+ filename : String ,
91+ content_type : String ,
92+ data : Vec < u8 > ,
8593}
8694
8795fn parse ( raw : String ) -> EmailPayload {
@@ -135,28 +143,22 @@ fn parse(raw: String) -> EmailPayload {
135143 _ => None ,
136144 } ,
137145 subject : email. subject . unwrap ( ) . to_string ( ) ,
138- // date: format!(
139- // "{} {} {} {} {}",
140- // format!("{:?}", email.date.day_name.unwrap()),
141- // email.date.date.day,
142- // format!("{:?}", email.date.date.month),
143- // email.date.date.year,
144- // email.date.time.time.hour
145- // ),
146146 html : "" . into ( ) ,
147147 text : "" . into ( ) ,
148+ attachments : vec ! [ ] ,
148149 } ;
149150
151+
150152 match parsed {
151153 Entity :: Text { subtype, value, .. } => match subtype. to_string ( ) . as_str ( ) {
152154 "html" => payload. html = value. to_string ( ) ,
153155 "plain" => payload. text = value. to_string ( ) ,
154- _ => ( ) ,
156+ _ => println ! ( "unknown text subtype: {}" , subtype ) ,
155157 } ,
156158 // parsing content of multipart body, when user using markdown,
157159 // there will be a plain text and a html version of the email (maybe it sent from Laravel app)
158160 Entity :: Multipart { subtype, content } => {
159- println ! ( "{:?}" , subtype) ;
161+ println ! ( "multipart {:?}" , subtype) ;
160162 for entity in content {
161163 match entity. mime_type {
162164 ContentType :: Multipart => {
@@ -165,18 +167,14 @@ fn parse(raw: String) -> EmailPayload {
165167 match parsed {
166168 Entity :: Multipart { subtype, content } => {
167169 for entity in content {
168- println ! ( "before match {}" , subtype) ;
170+ println ! ( "multipart 2 {}" , subtype) ;
169171 match entity. subtype . to_string ( ) . as_str ( ) {
170172 "html" => {
171- payload. html =
172- String :: from_utf8 ( entity. value . to_vec ( ) ) . unwrap ( )
173+ payload. html = String :: from_utf8 ( entity. value . to_vec ( ) ) . unwrap ( )
173174 }
174-
175175 "plain" => {
176- payload. text =
177- String :: from_utf8 ( entity. value . to_vec ( ) ) . unwrap ( )
176+ payload. text = String :: from_utf8 ( entity. value . to_vec ( ) ) . unwrap ( )
178177 }
179-
180178 _ => println ! ( "unknown subtype: {}" , subtype) ,
181179 }
182180 println ! (
@@ -208,11 +206,20 @@ fn parse(raw: String) -> EmailPayload {
208206 _ => println ! ( "not text" ) ,
209207 }
210208 }
209+ ContentType :: Application => {
210+ let attachment = Attachment {
211+ filename : entity. parameters . get ( "name" ) . map_or_else ( || "" . to_string ( ) , |name| name. to_string ( ) ) ,
212+ content_type : entity. subtype . to_string ( ) ,
213+ data : entity. value . to_vec ( ) ,
214+ } ;
215+ payload. attachments . push ( attachment) ;
216+ }
211217 _ => println ! ( "not multipart" ) ,
212218 }
213219 }
214220 }
215- _ => println ! ( "other" ) ,
221+
222+ _ => println ! ( "other {:#?}" , parsed) ,
216223 }
217224
218225 payload
0 commit comments