Sends the email message `message` with mail and envelope headers for from `from_address_` to `to_address` with `subject`. Additional email headers can be specified with the dictionary `headers. Optionally cc, bcc and attachments can be specified as keyword arguments. Attach
(from_address, to_address, subject, message, headers=None, **kw)
| 1402 | |
| 1403 | |
| 1404 | def sendmail(from_address, to_address, subject, message, headers=None, **kw): |
| 1405 | """ |
| 1406 | Sends the email message `message` with mail and envelope headers |
| 1407 | for from `from_address_` to `to_address` with `subject`. |
| 1408 | Additional email headers can be specified with the dictionary |
| 1409 | `headers. |
| 1410 | |
| 1411 | Optionally cc, bcc and attachments can be specified as keyword arguments. |
| 1412 | Attachments must be an iterable and each attachment can be either a |
| 1413 | filename or a file object or a dictionary with filename, content and |
| 1414 | optionally content_type keys. |
| 1415 | |
| 1416 | If `web.config.smtp_server` is set, it will send the message |
| 1417 | to that SMTP server. Otherwise it will look for |
| 1418 | `/usr/sbin/sendmail`, the typical location for the sendmail-style |
| 1419 | binary. To use sendmail from a different path, set `web.config.sendmail_path`. |
| 1420 | """ |
| 1421 | attachments = kw.pop("attachments", []) |
| 1422 | mail = _EmailMessage(from_address, to_address, subject, message, headers, **kw) |
| 1423 | |
| 1424 | for a in attachments: |
| 1425 | if isinstance(a, dict): |
| 1426 | mail.attach(a["filename"], a["content"], a.get("content_type")) |
| 1427 | elif hasattr(a, "read"): # file |
| 1428 | filename = os.path.basename(getattr(a, "name", "")) |
| 1429 | content_type = getattr(a, "content_type", None) |
| 1430 | mail.attach(filename, a.read(), content_type) |
| 1431 | elif isinstance(a, str): |
| 1432 | f = open(a, "rb") |
| 1433 | content = f.read() |
| 1434 | f.close() |
| 1435 | filename = os.path.basename(a) |
| 1436 | mail.attach(filename, content, None) |
| 1437 | else: |
| 1438 | raise ValueError("Invalid attachment: %s" % repr(a)) |
| 1439 | |
| 1440 | mail.send() |
| 1441 | |
| 1442 | |
| 1443 | class _EmailMessage: |
no test coverage detected