Mail Messaging
Cement defines a Mail Interface, as well as the default DummyMailHandler that implements the interface as a placeholder but does not actually send any mail.
Cement often includes multiple handler implementations of an interface that may or may not have additional features or functionality than the interface requires. The documentation below only references usage based on the interface and default handler (not the full capabilities of an implementation).
Cement Extensions that Provide Mail Handlers:
API References:
Option | Description |
mail_handler | The handler that implements the mail interface. |
Example: Working with Mail Messages
cli
from cement import App
with App('myapp') as app:
app.run()
# send a message using the defined mail handler
app.mail.send("Test mail message",
subject='My Subject',
to=['[email protected]'],
from_addr='noreply@localhost',
)
python myapp.py
=============================================================================
DUMMY MAIL MESSAGE
-----------------------------------------------------------------------------
From: noreply@localhost
CC:
BCC:
Subject: My Subject
---
Test mail message
-----------------------------------------------------------------------------
The default
dummy
mail handler simply prints the message to console, and does not send anything. You can override the mail handler via App.Meta.mail_handler
, for example using the SMTP Extension.All interfaces in Cement can be overridden with your own implementation. This can be done either by sub-classing
MailHandler
itself, or by sub-classing an existing extension's handlers in order to alter their functionality.Example: Creating a Mail Handler
myapp.py
from cement import App
from cement.core.mail import MailHandler
class MyMailHandler(MailHandler):
class Meta:
label = 'my_mail_handler'
# do something to implement the interface
class MyApp(App):
class Meta:
label = 'myapp'
mail_handler = 'my_mail_handler'
handlers = [
MyMailHandler,
]
Last modified 5yr ago