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

Setting email content from string in email.message?

$
0
0

I'd like to send an email using Python.

There's sendmail (Sending mail via sendmail from python), but also https://docs.python.org/3/library/smtplib.html. It recommends constructing a message based on https://docs.python.org/3/library/email.message.html and has a few examples https://docs.python.org/3/library/email.examples.html#email-examples which reads the message content from file:

# Open the plain text file whose name is in textfile for reading.
with open(textfile) as fp:
    # Create a text/plain message
    msg = EmailMessage()
    msg.set_content(fp.read())

I've tried

msg.set_content(b"test message sent locally")

but that results in TypeError: set_bytes_content() missing 2 required positional arguments: 'maintype' and 'subtype'. It seems https://docs.python.org/3/library/email.message.html#email.message.EmailMessage.set_content requires a context manager?

How can a string be used to construct the message body?


Viewing all articles
Browse latest Browse all 29745

Trending Articles



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