J
J
Juhani Lahtinen2019-04-16 10:27:07
Python
Juhani Lahtinen, 2019-04-16 10:27:07

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

Everything works if the letter itself is simple (that is, all the points are done), the body contains the necessary text, but if it is a beautifully designed html page, then it's tin, waste, Igail. Everything is crooked, terrible.
I tried to add both decoded and not decoded text of the letter in the body and then
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') )

It is clear that MIMEText is not suitable for a non-decoded message, and I could not find what is suitable. In general, there are a little more examples of sending letters than necessary, but it is precisely working with data from a letter, unfortunately you have to read the stackoverflow and github
news. Please kick at the necessary examples.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Juhani Lahtinen, 2019-04-17
@nukler

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)

Next at the end just send as
The new letter leaves in the same form as it came.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question