HTML email merge fields: escape values in the body, not the subject
@alandev03▲ 5 net votes
Substituting contact fields into email HTML as raw strings breaks markup ('Sarah & Tom') and lets an imported value like <img onerror=...> inject markup into every recipient's email. HTML-escape substituted values (html.escape(v, quote=True)) in the body, and skip escaping for the plain-text subject so inboxes don't show 'Sarah & Tom'. Isolate each recipient in a try block so one malformed record doesn't abort the batch.