Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Message objects and binary data to smtplib.sendmail #54530

bitdancer opened this issue Nov 5, 2010 · 4 comments

Add support for Message objects and binary data to smtplib.sendmail #54530

bitdancer opened this issue Nov 5, 2010 · 4 comments
stdlib Python modules in the Lib dir type-feature A feature request or enhancement


Copy link

BPO 10321
Nosy @warsaw, @pitrou, @giampaolo, @bitdancer
  • sendmail_message.patch
  • sendmail_message_2.patch
  • sendmail_message_3.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = ''
    closed_at = <Date 2010-11-08.17:18:43.998>
    created_at = <Date 2010-11-05.09:07:40.428>
    labels = ['type-feature', 'library']
    title = 'Add support for Message objects and binary data to smtplib.sendmail'
    updated_at = <Date 2010-11-08.17:18:43.996>
    user = '' fields:

    activity = <Date 2010-11-08.17:18:43.996>
    actor = 'r.david.murray'
    assignee = 'r.david.murray'
    closed = True
    closed_date = <Date 2010-11-08.17:18:43.998>
    closer = 'r.david.murray'
    components = ['Library (Lib)']
    creation = <Date 2010-11-05.09:07:40.428>
    creator = 'r.david.murray'
    dependencies = []
    files = ['19502', '19507', '19508']
    hgrepos = []
    issue_num = 10321
    keywords = ['patch']
    message_count = 4.0
    messages = ['120475', '120544', '120549', '120778']
    nosy_count = 7.0
    nosy_names = ['barry', 'exarkun', 'pitrou', 'giampaolo.rodola', 'ccgus', 'r.david.murray', 'Allison.Vollmann']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = ''
    versions = ['Python 3.2']

    Copy link
    Member Author

    The attached patch adds support to smtplib.SMTP.sendmail for the 'msg' argument to be, in addition to the currently accepted ASCII-only string, either a bytes string or a Message object. It also adds support for byte strings to

    Binary support is straightforward: if a byte string is passed, it is subject to leading '.' quoting but otherwise transmitted as is (that is, no \r\n transformation is done, unlike the string case).

    For Message object support, the Message is serialized via BytesGenerator, meaning that a message parsed from a bytes source can be successfully re-transmitted via smtplib. In addition to_addrs and from_addr can be set to None, in which case the addresses are obtained from the appropriate Message object headers (and, for safety, any Bcc header is deleted).

    Although this patch is complete with docs, I'm not convinced this is the correct API.

    First is the question of whether or not Message object support should be added. It is in the patch because I started the work with the idea that serializing a Message via BytesGenerator was the "right way" to get binary data into smtplib, but as the implementation fell out I in fact ended up adding support for arbitrary binary data to sendmail (and data). So in fact the Message object handling is not required in smtplib.

    The feature is, however, clearly useful. Perhaps, however, it should live in email as one or more helper methods instead. I prefer having it in smtplib, but would like the opinions of others.

    The second question is whether or not either functionality should be added to the existing sendmail method, or whether new methods should be created instead. The alternative API might be:

        send_bytes(from_addr, to_addrs, msg, mail_options, rcpt_options)
        send_message(msg, mail_options, rcpt_options, from_addr=None, to_addrs=None)

    Having completed the patch I'm neutral on this API refactoring, and again welcome other opinions. 'send_bytes' doesn't really seem like the right name, since it implies sending arbitrary bytes when in fact what is happening is a complete message transaction. 'send_bytesmail'? Ugly :(

    (See also bpo-8050 and bpo-4403.)

    Note that I'd like to get some variation of this in (that at a minimum at least supports passing binary data to sendmail) before beta1, since it is the logical compliment to the new bytes support in the email package.

    @bitdancer bitdancer self-assigned this Nov 5, 2010
    @bitdancer bitdancer added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Nov 5, 2010
    Copy link
    Member Author

    New patch that takes a middle ground on the API: sendmail accepts string and bytes, and a new method send_message accepts a Message object with a more convenient signature. I think send_message does belong in smtplib since it would be awkward and unintuitive to put the helper function in email, since one needs to create an SMTP server to call sendmail.

    I am satisfied with this version; the delta against the existing code and docs is smaller and the API feels clean.

    The new patch also updates a couple of the email package examples that use sendmail to use send_message (one of the sendmail examples is left untouched).

    If there are no objections I'll commit this this weekend sometime.

    Copy link
    Member Author

    One more patch update. This one includes the versionadded/version changed and a minimal What's New entry.

    Copy link
    Member Author

    Committed in r86327.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    None yet

    No branches or pull requests

    1 participant