mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-06-17 08:13:56 +00:00
feat(email): attach media files to outbound SMTP messages
This commit is contained in:
parent
456ed77e79
commit
25bb053206
@ -3,6 +3,7 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import html
|
import html
|
||||||
import imaplib
|
import imaplib
|
||||||
|
import mimetypes
|
||||||
import re
|
import re
|
||||||
import smtplib
|
import smtplib
|
||||||
import ssl
|
import ssl
|
||||||
@ -213,6 +214,28 @@ class EmailChannel(BaseChannel):
|
|||||||
email_msg["Subject"] = subject
|
email_msg["Subject"] = subject
|
||||||
email_msg.set_content(msg.content or "")
|
email_msg.set_content(msg.content or "")
|
||||||
|
|
||||||
|
# Attach media files
|
||||||
|
for media_path in msg.media or []:
|
||||||
|
path = Path(media_path)
|
||||||
|
if not path.is_file():
|
||||||
|
self.logger.warning("Attachment not found, skipping: {}", media_path)
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
data = path.read_bytes()
|
||||||
|
ctype, encoding = mimetypes.guess_type(str(path))
|
||||||
|
if ctype is None:
|
||||||
|
ctype = "application/octet-stream"
|
||||||
|
maintype, subtype = ctype.split("/", 1)
|
||||||
|
email_msg.add_attachment(
|
||||||
|
data,
|
||||||
|
maintype=maintype,
|
||||||
|
subtype=subtype,
|
||||||
|
filename=path.name,
|
||||||
|
)
|
||||||
|
self.logger.info("Attached file: {}", path.name)
|
||||||
|
except Exception:
|
||||||
|
self.logger.exception("Failed to attach file {}", media_path)
|
||||||
|
|
||||||
in_reply_to = self._last_message_id_by_chat.get(to_addr)
|
in_reply_to = self._last_message_id_by_chat.get(to_addr)
|
||||||
if in_reply_to:
|
if in_reply_to:
|
||||||
email_msg["In-Reply-To"] = in_reply_to
|
email_msg["In-Reply-To"] = in_reply_to
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user