Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29745

Create Draft Email with Python's email.generator package

$
0
0

I am currently using this code to generate an email with Python:

from email import generator
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def Create_Email():
    msg            = MIMEMultipart('alternative')
    msg['Subject'] = 'My Subject'
    msg['To']      = 'test@gmail.com'

    html = """\
    <html>
        <head></head>
        <body>hello world</body>
    </html>"""

    part = MIMEText(html, 'html')
    msg.attach(part)

    outfile_name = r'C:\Downloads\email_sample.eml'
    with open(outfile_name, 'w') as outfile:
        gen = generator.Generator(outfile)
        gen.flatten(msg)

Create_Email()

But when I then open the file with outlook, it appears as an already-sent email:

     enter image description here

How can I change this so that the saved file will be treated as a draft, which I can still edit and then send? Like so:

      enter image description here

If email.generator cannot do this, I'd be happy to use an alternative package.


Viewing all articles
Browse latest Browse all 29745

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>