CSDF 01

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Name :Chabukswar Siddharth

Roll No:12
Batch: B1
CSDF (LP IV) Practical No 01- Write a program for Tracking Emails and Investigating Email Crimes. i.e.
Write aprogramto analyze e–mail header.

Code:
import email
from email.header import decode_header
# Function to parse email headers
def parse_email_headers(email_message):
headers = email_message.items()
parsed_headers = {}
for header, value in headers:
decoded_value, charset = decode_header(value)[0]
if charset:
decoded_value = decoded_value.decode(charset)
parsed_headers[header] = decoded_value
return parsed_headers

# Function to print parsed headers


def print_parsed_headers(headers):
for header, value in headers.items():
print(f"{header}: {value}")

def main():
# Replace 'email.txt' with the path to your email file
email_file =(r"C:\Users\SAIFUL\Desktop\email.txt")
try:
with open(email_file, 'r', encoding='utf-8') as f:
email_data = f.read()
email_message = email.message_from_string(email_data)
if email_message:
headers = parse_email_headers(email_message)
print_parsed_headers(headers)
else:
print("Error: Unable to parse the email message.")
except FileNotFoundError:
print(f"Error: File '{email_file}' not found.")
if name == " main ":
main()
Output:

PS C:/Users/SAIFUL/Documents/CSDF/1.py
from: sbthakare48@gmail.com
to: rohanwakhare@gmail.com
subject: Greeting...
Hello Rohan,
PS C:\Users\SAIFUL\Documents\CSDF>

You might also like