Created
September 2, 2024 20:59
-
-
Save aigarius/b7eccfbed4908b27d941761d93e66083 to your computer and use it in GitHub Desktop.
Send email messages from local /var/mail spool folder to IMAP folder (on GMail) avoiding all spam filtering. Script user must have "mail" group.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import imaplib | |
import logging | |
import mailbox | |
import netrc | |
import getpass | |
from datetime import datetime, timezone | |
logging.basicConfig(level=logging.DEBUG) | |
# Find GMail login and password in .netrc | |
M = imaplib.IMAP4_SSL(host="imap.gmail.com", timeout=10) | |
login, _, password = netrc.netrc().authenticators("imap.gmail.com") | |
# Take messages from normal local mailbox | |
mbox = mailbox.mbox("/var/mail/" + getpass.getuser()) | |
M.login(login, password) | |
M.select() | |
try: | |
for msg in mbox.keys(): | |
# Append the message as raw bytes to GMail | |
# inbox via IMAP | |
M.append( | |
"INBOX", | |
"", | |
datetime.now(timezone.utc), | |
message=mbox.get_bytes(msg), | |
) | |
# And remove it from local mbox | |
mbox.remove(msg) | |
finally: | |
# Unlocks and flushes the mailbox | |
mbox.close() | |
M.close() | |
M.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment