diff --git a/src/email.ts b/src/email.ts index 0e4bd88..cf588f4 100644 --- a/src/email.ts +++ b/src/email.ts @@ -159,10 +159,10 @@ const fetchMessages = ( */ type EmailMatcher = (email: Email) => (message: FetchMessageObject) => boolean; const matchesEmail: EmailMatcher = (email) => (message) => { - const subjectMatches = email.subject === message.envelope.subject; + const subjectMatches = email.subject === message.envelope?.subject; const bodyMatches = - message.bodyParts.get("text")?.toString().trim() === email.text.trim(); - const headers = message.headers.toLocaleString(); + message.bodyParts?.get("text")?.toString().trim() === email.text.trim(); + const headers = message.headers?.toLocaleString(); const fromMatches = headers.includes(`Return-Path: <${email.from}>`); const toMatches = headers.includes(`Delivered-To: ${email.to}`); return subjectMatches && bodyMatches && fromMatches && toMatches;