{"id":23665,"library":"email","title":"email (standalone)","description":"Standalone email package version 4.0.2. Originally part of Python's stdlib, now available as a standalone backport with improvements for modern Python. Provides classes for parsing, generating, and sending email messages, including MIME support. Release cadence is low (latest 4.0.2 from 2024).","status":"active","version":"4.0.2","language":"python","source_language":"en","source_url":"https://github.com/python/cpython/tree/main/Lib/email","tags":["email","mime","smtp","message","backport"],"install":[{"cmd":"pip install email","lang":"bash","label":"Install from PyPI"}],"dependencies":[],"imports":[{"note":"Direct import of 'Message' from 'email' module is wrong; use EmailMessage or MIMEText.","wrong":"from email import Message","symbol":"Message","correct":"from email.message import EmailMessage"},{"note":"MIMEText is in email.mime.text, not top-level email.","wrong":"from email import MIMEText","symbol":"MIMEText","correct":"from email.mime.text import MIMEText"}],"quickstart":{"code":"from email.message import EmailMessage\n\nmsg = EmailMessage()\nmsg.set_content('Hello, world!')\nmsg['Subject'] = 'Test'\nmsg['From'] = 'you@example.com'\nmsg['To'] = 'recipient@example.com'\n\nprint(msg.as_string())","lang":"python","description":"Create and print a simple email message using EmailMessage."},"warnings":[{"fix":"If you are using Python 3.6+, avoid installing the standalone 'email' package unless necessary, as it can shadow the stdlib module.","message":"In Python 3.6+, the 'email' stdlib module changed significantly. The standalone 'email' package (backport) may conflict with stdlib imports. Prefer using stdlib unless you need features from the backport.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Replace 'from email.Message import Message' with 'from email.message import EmailMessage'.","message":"The old 'email.Message' class is deprecated. Use 'email.message.EmailMessage' instead.","severity":"deprecated","affected_versions":">=4.0.0"},{"fix":"Use msg.add_header('Received', 'from localhost') for repeated headers.","message":"When setting email headers, do not use the 'Message.__setitem__' method directly for multiple headers of the same name (e.g., 'Received'). Use 'Message.add_header' to add multiple values.","severity":"gotcha","affected_versions":"all"},{"fix":"Example: part = MIMEBase('application', 'octet-stream'); part.set_payload(data); encode_base64(part).","message":"When attaching binary files, ensure you handle encoding correctly. For Python 3, use 'email.mime.base.MIMEBase' with 'Content-Transfer-Encoding: base64'.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"msg = EmailMessage(); msg.set_content('body'); print(msg.as_string())","cause":"Calling as_string() on a string (the message body) instead of the EmailMessage object.","error":"AttributeError: 'str' object has no attribute 'as_string'"},{"fix":"Use Python 3.6+ or install the 'email' backport and use 'from email.message import EmailMessage'.","cause":"Using an old Python version (pre-3.6) where EmailMessage does not exist in stdlib.","error":"ImportError: cannot import name 'EmailMessage' from 'email'"},{"fix":"Convert header values to strings before assignment.","cause":"Passing a non-string value to an email header (e.g., integer).","error":"TypeError: expected string or bytes-like object"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}