我需要将收件箱a@example.com
中的电子邮件重新发送到b@example.org
。创建新电子邮件时,是否可以发送有效载荷而不对其进行解码和编码?无论是文本,HTML还是附件。您只需要从另一个邮箱发送原始信件,而无需转发通知。
mail = imaplib.IMAP4_SSL(server)
mail.login(login, password)
mail.list()
mail.select("inbox")
result, data = mail.search(None, "UNSEEN")
if result == 'OK':
for num in data[0].split():
result, data = mail.fetch(num, "(RFC822)")
raw_email = data[0][1]
try:
oldmsg = email.message_from_string(raw_email)
except TypeError:
oldmsg = email.message_from_bytes(raw_email)
newmsg = email.mime.multipart.MIMEMultipart(boundary="----")
newmsg['Subject'] = decode_mime_words(oldmsg['Subject'])
newmsg['From'] = login
newmsg['Date'] = strftime("%a, %d %b %Y %H:%M:%S %z")
newmsg['User-Agent'] = "Roundcube Webmail"
newmsg['To'] = to
#part = MIMEBase('application', 'octet-stream')
if oldmsg.is_multipart():
for oldpayload in oldmsg.get_payload():
print(oldpayload.get_payload())
print("-_________________-")
else:
print(oldmsg.get_payload())
# get attachments
for part in oldmsg.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()
if (fileName[:11] == '=?koi8-r?B?') or (fileName[:11] == '=?KOI8-R?B?'):
fileName = base64.b64decode(fileName[11:]).decode(
'KOI8-R')
elif (fileName[:10] == '=?utf-8?B?') or (fileName[:10] == '=?UTF-8?B?'):
fileName = base64.b64decode(fileName[10:])
fileName = fileName.decode('utf-8')
# name attachemnts
if bool(fileName):
filePath = os.path.join('attach', fileName)
if not os.path.isfile(filePath):
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
part = MIMEApplication(open(filePath, 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename=fileName)
newmsg.attach(part)
fp.close()