not sure, but you might want to add a `if last_email.text_part` (or something similar) to check if there actually is a text part, because mails may not have text_parts.
(same with html_part below. but you might check if at least one of the conditions matched)