0

Sorry for my english. I need get mail from gmail(this mail can contains attachments) and forward this mail from another email. Bellow my code for forward mail:

            for email_id in reversed(items):
                status, data = self.imap.fetch(email_id, "(RFC822)")
                if status == 'OK':

                    if count == 2: // this message contains attachment
                        message = email.message_from_bytes(data[0][1])
                        message.replace_header("From", FROM_ADDR)
                        message.replace_header("To", TO_ADDR)

                        try:
                            smtp = smtplib.SMTP('smtp.gmail.com', 587)
                            smtp.starttls()
                            smtp.login(CLIENT_MAIL, CLIENT_PASSWORD)
                            smtp.sendmail(FROM_ADDR, TO_ADDR, message.as_string())
                            smtp.quit()
                            print("send mail")
                        except BaseException as e:
                            print(e)

                    count += 1

This code work if mail contains only text(without attachment)

nesalexy
  • 848
  • 2
  • 9
  • 30

1 Answers1

1

What's your error message?

Also, if you're using Python 3.2+, have you tried using smtp.send_message(message, FROM_ADDR, TO_ADDR) instead of smtp.sendmail()? It looks like it tries hard to do things more correctly, especially regarding character encoding in the SMTP connection.

Schnouki
  • 7,527
  • 3
  • 33
  • 38
  • i have not any error. I change `smtp.sendmail()` to `smtp.send_message(message, FROM_ADDR, TO_ADDR)` but message do not come in gmail – nesalexy Aug 30 '18 at 14:11