Email

To send an email using Appier just do the following in Model or Controller:

self.email(
    "template.html.tpl",
    subject = "Test email",
    sender = "Sender Name <sender@email.com>",
    subject = "Test email",
    sender = "sender@email.com",
    receivers = ["receiver@email.com"]
)

The previous example uses the Configuration to figure out which SMTP server should be used and how. The following configuration variables must be set:

NameTypeDescription
SMTP_HOSTstrThe host where the SMTP server is running.
SMTP_PORTintThe port where the SMTP server is listening (default: 25).
SMTP_USERstrThe username used to authenticate with the SMTP server.
SMTP_PASSWORDstrThe password used to authenticate with the SMTP server.
SMTP_STARTTLSboolFlag used to tell the server that the client supports Transport Layer Security (default: True).

These configurations can also be provided directly to the email method (they will override the previously described app settings):

self.email(
    "template.html.tpl",
    subject = "Test email",
    sender = "Sender Name <sender@hive.pt>",
    receivers = ["receiver@hive.pt"],
    host = "hostname.com",
    port = 25,
    username = "username",
    password = "password"
    stls = True
)

Templates

Emails are sent out as a MIME multipart message, with a rich version and plain text version. The mandatory template argument is used to create the rich version, and by default, the result of that template being processed will be stripped out of its HTML tags automatically to create the plain text version. In order to specify a specific template for the plain text version, just send plain_template keyword argument with the template file name:

self.email(
    "template.html.tpl",
    plain_template = "plain_template.txt.tpl",
    subject = "Test email",
    sender = "Sender Name <sender@hive.pt>",
    receivers = ["receiver@hive.pt"]
)

The email is encoded in UTF-8 by default. If you want to use another encoding, just send the encoding keyword argument with the encoding you desire.

On a final note, remember that email URLs must not be relative. To make sure they are resolved to absolute URLs do the following:

<a href="{{ url_for('base.index', absolute = True) }}">The App</a>

The absolute named argument will make the resolved URL be prefixed with the value specified in the BASE_URL configuration setting. To learn more, read the Configuration documentation.