统一提取邮件字段,兼容 raw MIME 和不同 Worker 返回格式。
(self, mail: Dict[str, Any])
| 193 | return unescape("\n".join(part for part in parts if part).strip()) |
| 194 | |
| 195 | def _extract_mail_fields(self, mail: Dict[str, Any]) -> Dict[str, str]: |
| 196 | """统一提取邮件字段,兼容 raw MIME 和不同 Worker 返回格式。""" |
| 197 | sender = str( |
| 198 | mail.get("source") |
| 199 | or mail.get("from") |
| 200 | or mail.get("from_address") |
| 201 | or mail.get("fromAddress") |
| 202 | or "" |
| 203 | ).strip() |
| 204 | subject = str(mail.get("subject") or mail.get("title") or "").strip() |
| 205 | body_text = str( |
| 206 | mail.get("text") |
| 207 | or mail.get("body") |
| 208 | or mail.get("content") |
| 209 | or mail.get("html") |
| 210 | or "" |
| 211 | ).strip() |
| 212 | raw = str(mail.get("raw") or "").strip() |
| 213 | |
| 214 | if raw: |
| 215 | try: |
| 216 | message = message_from_string(raw, policy=email_policy) |
| 217 | sender = sender or self._decode_mime_header(message.get("From", "")) |
| 218 | subject = subject or self._decode_mime_header(message.get("Subject", "")) |
| 219 | parsed_body = self._extract_body_from_message(message) |
| 220 | if parsed_body: |
| 221 | body_text = f"{body_text}\n{parsed_body}".strip() if body_text else parsed_body |
| 222 | except Exception as e: |
| 223 | logger.debug(f"解析 TempMail raw 邮件失败: {e}") |
| 224 | body_text = f"{body_text}\n{raw}".strip() if body_text else raw |
| 225 | |
| 226 | body_text = unescape(re.sub(r"<[^>]+>", " ", body_text)) |
| 227 | return { |
| 228 | "sender": sender, |
| 229 | "subject": subject, |
| 230 | "body": body_text, |
| 231 | "raw": raw, |
| 232 | } |
| 233 | |
| 234 | def _is_openai_otp_mail(self, sender: str, subject: str, body: str, raw: str) -> bool: |
| 235 | """ |
no test coverage detected