File: email_edit.py

package info (click to toggle)
imap-tools 1.10.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,624 kB
  • sloc: python: 4,709; makefile: 5
file content (16 lines) | stat: -rw-r--r-- 613 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
"""
Email modification is not common case operation
Most likely it will not be added to lib

Also, I have tried to add public attribute - MailMessage.orig_bytes (raw_message_data)
But it have increased memory consumption: ~10%
"""

import email
from imap_tools import MailBox, A

with MailBox('imap.mail.com').login('test@mail.com', 'password') as mailbox:
    for msg in mailbox.fetch(A(subject='[some]')):
        altered_msg = email.message_from_bytes(msg.obj.as_bytes())
        altered_msg.replace_header('Subject', '[my own subject extension] ' + msg.subject)
        mailbox.append(altered_msg.as_bytes())