attachments.rst 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. .. meta::
  2. :description: Send email with attachment in Python.
  3. :keywords: send, email, Python, attachment
  4. .. _attachments:
  5. Sending Email with Attachments
  6. ------------------------------
  7. Red Mail also provides a convenient way to include
  8. attachments to the emails, for example:
  9. .. code-block:: python
  10. import pandas as pd
  11. from pathlib import Path
  12. email.send(
  13. subject='Some attachments',
  14. receivers=['first.last@example.com'],
  15. attachments={
  16. 'data.csv': Path('path/to/file.csv'),
  17. 'data.xlsx': pd.DataFrame(...),
  18. 'raw_file.html': '<h1>Just some HTML</h1>',
  19. }
  20. )
  21. As seen, Red Mail allows passing various objects to the
  22. attachments. You may pass a list, a single object or
  23. a dict. If you pass a dict, the key is used to determine
  24. the name of the attachment and possibly the type.
  25. Here is a list of supported value formats if ``dict`` is passed to attachments:
  26. ================ =============== =================================================================
  27. Value type Dict key Dict value
  28. ================ =============== =================================================================
  29. ``pd.Series`` Attachment name Turned to CSV, XLSX, HTML etc. depending on file extension in key
  30. ``pd.DataFrame`` Attachment name Turned to CSV, XLSX, HTML etc. depending on file extension in key
  31. ``str`` Attachment name Attachment content as raw text
  32. ``bytes`` Attachment name Attachment content as raw bytes
  33. ``pathlib.Path`` Attachment name Path to a file that is attached (using key as the file name)
  34. ================ =============== =================================================================
  35. Here is a list of supported value formats if ``list`` or single object is passed to attachments:
  36. ================ =============== =========================================================
  37. Type Attachment name Value
  38. ================ =============== =========================================================
  39. ``pathlib.Path`` From file name Content of the file read as the content of the attachment
  40. ``str`` From file name Considered as file path, handled the same as above
  41. ================ =============== =========================================================