Problem opening and closing file to send over email python

5 replies
I've been dabbling in cyber security and doing some research on parenting monitoring tools and thought a key-logger/email sender would be a good project to learn how to build.

So the code I have sends an email correctly, and also sends the generated text file as an attachment.

The problems I'm having and need help with are: 1. As the key logger continues to log keystrokes, the email attachment does not update the file, it just sends the original file in the original path.
#closing #email #file #opening #problem #python #send
  • Profile picture of the author Jeff Polaski
    Try to resolve it by implementing a mechanism to update the file and attach the most recent version to the email.
    {{ DiscussionBoard.errors[11758355].message }}
  • Hello,

    I am new here. I am sorry, but I cannot make a thread. So I will ask here.


    Lately, we update the main menu on our website. But request to contact are now 1 per day(before was 10-20 requests per day). We have organic traffic around 20K per month. So there are always some requests even if most of them are fake.

    I saw a bug that main menu was not shown, but I cannot reproduce it again.(even if I clean cookies)

    So, I am here to ask you to go to website and see if the main Manu in top works properly(Products, Solutions, Customers, Pricing, Resources and Free Trial Button).

    I would really appreciate your help.

    Thank you in advance
    {{ DiscussionBoard.errors[11761044].message }}
  • Profile picture of the author guinemojo23
    Hi!
    It's great that you're exploring cybersecurity! To address your issue, ensure the keylogger program appends keystrokes to the text file and consistently reads the latest version just before attaching it to the email. Incorporate periodic updates or event triggers to send emails with current data. Remember to use these skills responsibly and ethically
    {{ DiscussionBoard.errors[11764816].message }}
  • Profile picture of the author Brenda Gray
    The issue you're encountering with the keylogger not updating the email attachment is likely due to the fact that you're attaching the original file at the start of your program, and it doesn't dynamically update. To achieve this, you can follow these steps:

    Open the file in "append" mode when logging keystrokes, so new data gets appended to the end of the file instead of overwriting it.

    Periodically (e.g., every few minutes), read the contents of the file and send them as an email attachment.

    After sending the email, you can truncate or clear the file to remove the logged keystrokes you've already sent.
    {{ DiscussionBoard.errors[11769017].message }}
  • Profile picture of the author instailyacademy
    1. The reason why the email attachment does not update the file is because you are using the same file name and path for each email. When you create the text file that contains the keystrokes, you need to give it a unique name or location, otherwise it will overwrite the previous file or send the same file again. One way to do this is to use the date-time module to append the current date and time to the file name, such as "log_2023_11_01_10_35_59.txt". This way, each file will have a different name and will not be overwritten or duplicated.

    2. Another way to solve this problem is to use the email.mime module to create a MIME object that contains the keystrokes as a string, instead of a text file. This way, you do not need to save the keystrokes to a file, and you can send them directly as an email attachment. You can use the email.mime.text.MIMEText class to create a MIME object from a string, and then attach it to the email message using the email.mime.multipart.MIMEMultipart class.
    {{ DiscussionBoard.errors[11774434].message }}

Trending Topics