Search code examples
pythonemailsmtplibmime-message

send an email from a message string in python


this is probably a trivial question but I couldn't find an answer anywhere.

I need to write a script that receives an entire MIME message in a string, as such:

From: Jerry Peek <[email protected]>
To: [email protected]
Subject: New edition of "MH & xmh" covers MIME and MH-E
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="----- =_aaaaaaaaaa0"
Content-ID: <[email protected]>

------- =_aaaaaaaaaa0
Content-Type: text/plain; charset="us-ascii"
Content-ID: <[email protected]>
We've just released the new third edition of "MH & xmh: Email for
    Users & Programmers."  Changes include:
        - MIME (Multimedia) mail
        - The popular MH-E GNU Emacs front-end to MH
            ...omitted...

------- =_aaaaaaaaaa0--

and passes it on to an SMTP server.

I saw alot of examples that force me to parse the message and fetch the to, from and message data. is there a method that allows me to send the string as is?

thanks


Solution

  • It might exists, but I do not think so. For the SMTP protocol (RFC 2821 - Simple Mail Transfer Protocol), you have 4 different elements : the SMTP server to which to connect, the MAIL-FROM enveloppe email address, the RCPT-TO destination address(es) and the DATA. All the headers are included in the DATA in SMTP sense.

    So when using smtplib you have either to give a text message, a from address and one or more recipient addresses, or a parsed Message to allow smtplib to find from address and recipients in the headers. That's the reason why I think the short answer to you question is no.