Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save_file() method error because symbols inside subject #329

Open
WU19-1 opened this issue Feb 8, 2023 · 1 comment
Open

save_file() method error because symbols inside subject #329

WU19-1 opened this issue Feb 8, 2023 · 1 comment

Comments

@WU19-1
Copy link

WU19-1 commented Feb 8, 2023

Hello, I want to report about an error inside the save_file() method if there is symbols inside the subject because:

def save_file(self, file_path):

        eml_content = self.build_email()

        file_name = str(self.message["Subject"]) + ".eml"

        eml_file_path = os.path.join(file_path, file_name)

        with codecs.open(eml_file_path, mode="wb+", encoding="utf-8") as eml_file:
            eml_file.write(eml_content)

        return eml_file_path

Meanwhile my email has a symbol that are prohibited for filenames < > : " / \ | ? * and inside the save_file() method, these symbols are not removed automatically.

Images:
Code and files inside
image
File without symbol inside the subject
image
File with symbol inside the subject
failed

This is my first time submitting an issue, if there's any mistake that I made, I apologize
Thank you very much ! :DDD

@fotis-ntouskas
Copy link

Until this gets resolved, here is a solution for cleansing a subject in case it contains OS unsafe characters:

from msg_parser import MsOxMessage
msg_obj = MsOxMessage(<eml_file>)
                    
msg_subject = msg_obj.get_properties()['Subject']
# msg_parser uses email Subject as filename. Since this is not safe, Subject should be cleansed
msg_subject = "".join([c for c in msg_subject if c.isalpha() or c.isdigit()]).rstrip()
# Assign cleansed subject back to email object
msg_obj.subject = msg_subject

msg_obj.save_email_file(<file_path>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants