Skip to content

Instantly share code, notes, and snippets.

@jeremykdev
Created May 1, 2021 21:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeremykdev/a489d95e628b8217429c53624f225b50 to your computer and use it in GitHub Desktop.
Save jeremykdev/a489d95e628b8217429c53624f225b50 to your computer and use it in GitHub Desktop.
Python script to create an eml file
# 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 = "joe@example.com"
fromAddress = "jane@example.com"
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