Answer the question
In order to leave comments, you need to log in
How to add your part to the received email and redirect to a new address?
Good afternoon.
Tell. There is a task.
1. Get a mail message.
2. Get the required fields "From", "To", "Subject"
3. Add your data to the beginning and end of the received message. Something like "Message received at [email protected] and contains the right word"
4. Send to the right mail.
All this was done.
imap_descprt = get_connect_to_server_IMAP("[email protected]", global_check_email_login, global_check_email_password, global_IMAP_port)
imap_descprt.select('INBOX')
status, res = imap_descprt.uid("search", None, "(UNSEEN)")
res = b''.join(res).decode("UTF-8")
if res and status == "OK":
body = str()
for res_now in res.split(" "):
email_from_ = str()
result, data = imap_descprt.uid("fetch",res_now,"(RFC822)")
full_mail = email.message_from_bytes(data[0][1], policy=policy.default)
if full_mail.is_multipart():
for part in full_mail.get_payload():
ctype = str()
charset_type = str(part.get_content_charset())
ctype = part.get_content_type()
if ctype == "text/plain" or ctype == "text/html":
body += part.get_payload(decode = True).decode(charset_type) # decode
#body += part.get_payload(decode = False) # No decode
else:
charset_type = str(full_mail.get_content_charset())
body = full_mail.get_payload(decode = True).decode(charset_type) # decode
#body = full_mail.get_payload(decode = False) # No decode
msg = MIMEMultipart("mixed")
msg['To'] = email_from
msg['From'] = sender_email
msg['Subject'] = Header(str("REsend: " + email_subject), 'UTF-8')
msg.attach( MIMEText(body_to_send_part_1, "html", _charset='UTF-8') )
msg.attach( MIMEText(body, "plain", _charset='UTF-8') )
Answer the question
In order to leave comments, you need to log in
Thank you all =)
In general, the fool himself. No need to parse the message body.
We simply shove it into a new message after the data we need.
msg = MIMEMultipart("mixed")
msg['To'] = email_from
msg['From'] = sender_email
msg['Subject'] = Header(str("REsend: " + email_subject), 'UTF-8')
msg.attach( MIMEText(body_to_send_part_1, "html", _charset='UTF-8') )
msg.attach(full_mail)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question