Sending Email with Attachments
Red Mail also provides a convenient way to include attachments to the emails, for example:
import pandas as pd
from pathlib import Path
email.send(
subject='Some attachments',
receivers=['first.last@example.com'],
attachments={
'data.csv': Path('path/to/file.csv'),
'data.xlsx': pd.DataFrame(...),
'raw_file.html': '<h1>Just some HTML</h1>',
}
)
As seen, Red Mail allows passing various objects to the attachments. You may pass a list, a single object or a dict. If you pass a dict, the key is used to determine the name of the attachment and possibly the type.
Here is a list of supported value formats if dict is passed:
Value type |
Key as |
Value as |
|---|---|---|
|
Attachment name |
Turned to CSV, XLSX, HTML etc. depending on file extension in key |
|
Attachment name |
Turned to CSV, XLSX, HTML etc. depending on file extension in key |
|
Attachment name |
Attachment content as raw text |
|
Attachment name |
Attachment content as raw bytes |
|
Attachment name |
Content of the file read as the content of the attachment |
Here is a list of supported value formats if list or single object is passed:
Type |
Attachment name |
Value |
|---|---|---|
|
From file name |
Content of the file read as the content of the attachment |
|
From file name |
Considered as file path, handled the same as file path |