attachments.rst 2.1 KB

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