Created
May 1, 2021 21:04
-
-
Save jeremykdev/a489d95e628b8217429c53624f225b50 to your computer and use it in GitHub Desktop.
Python script to create an eml file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prototype to create an eml file using python | |
import os | |
import uuid | |
from email import generator | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
# where to write the output file | |
directory = "C:\\Users\\Jeremy\Documents\\python\\email-prototype\\temp" | |
body = "Hello email!" | |
subject = "test" | |
toAddress = "[email protected]" | |
fromAddress = "[email protected]" | |
def create_message(): | |
msg = MIMEMultipart() | |
msg["To"] = toAddress | |
msg["From"] = fromAddress | |
msg["Subject"] = subject | |
msg.attach(MIMEText(body, "plain")) | |
return msg | |
def write_eml_file(msg): | |
os.chdir(directory) | |
filename = str(uuid.uuid4()) + ".eml" | |
with open(filename, 'w') as file: | |
emlGenerator = generator.Generator(file) | |
emlGenerator.flatten(msg) | |
def main(): | |
msg = create_message() | |
write_eml_file(msg) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment