2

As a brief summary, I have a bunch of '.eml' files in a directory. I need to forward these emails back to 'email@example.com'.

The problem is that the field 'From' in the header of the '.eml' file contains another email address which doesn't match with 'email@example.com'.

I've searched for a way to parse the file and update the content of the header.

At first, I was using the following modules:

  • eml.parser to parse the file.
  • pyo365 to connect to MSGraph API

I was able to send the content of the body but when I would try to send the attachments, I had to decode from base64 and extract the attachments in a folder, then send everything. I did not need to change the content of the header.

I know this was a bad move because there is probably a way to send the attachments encoded.

Also, since MSGraph attachment's file size limit is 4mb per requests, I decided to try to change for:

  • smtplib to send the email
  • I tried mail-parser without success to update anything in the content since the updated values would not be permanent, for instance:

    mail = mailparser.parse_from_bytes(byte_mail) mail.from_ = [('My Name' , 'email@example.com')] print(mail.headers) #This would print the original header

I also tried with mail.update() and various method using this module without success.

I found a post Python: Modify Values in eml file (email header) which suggested to use Parser, replace_header and as_string from email but I was unable to make it work either as I wouldn't be able to call replace_header and as_string:

from email.message import EmailMessage #contains as_string
from email.parser import HeaderParser
file = open(filename, 'r')
h = HeaderParser().parse(file)

#stuck here

I know this is probably not only one question but the main goal is to send eml files back to a particular address, from 'email@example.com'.

tadman
  • 208,517
  • 23
  • 234
  • 262

1 Answers1

0

The issue was resolved by parsing the email with eml_parser. I created my own header, attached the HTML body content and the attachments.

from passlib.context import CryptContext
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.header import Header

def send(self, dst):
    try:
        self.m = MIMEMultipart()
        self.m['From'] = self.client_addr
        self.m['To'] = dst
        # Must have Header() in python 3.x otherwise you get UnicodeError
        self.m['Subject'] = Header(self.get_subject(),  'utf-8')

        #Attach HTML body with the right encoding
        self.m.attach(MIMEText(self.get_body().encode('utf-8'), 'html', 'utf-8'))

        # Extract attachments to self.attachment_path
        self.extract_attachments(self.parsed_eml)

        server = smtplib.SMTP('smtp.office365.com', 587)
        server.ehlo()
        server.starttls()

        # Compare hash in config.json file
        if self.pwd_context.verify(self.client_plain_secret, self.client_secret):

            server.login(self.client_addr, self.client_plain_secret)
            server.sendmail(self.m['From'], self.m['To'], self.m.as_string())
            server.quit()
    except:
        print("An error occured trying to send the email.")
    finally:
        self.clean_attachments()
insolor
  • 424
  • 8
  • 16