Sending email in Divio Cloud applications¶
Divio Cloud does not provide mail services. To send mail from your Django applications, you willneed to provide the appropriate configuration.
Django provides email wrappers around Python’s smtplib
module.
Configuration¶
The following configuration settings can be provided:
- Basic settings
EMAIL_HOST
andEMAIL_PORT
(defaults to25
)- Authentications settings
EMAIL_HOST_USER
andEMAIL_HOST_PASSWORD
- Secure authentication
EMAIL_USE_TLS
andEMAIL_USE_SSL
Using EMAIL_URL environment variable¶
However, the preferred way to provide these is via an EMAIL_URL
environment variable, so that your local, Test and Live servers can use their own configuration.
The EMAIL_URL
is the recommended way of combining the settings into a single variable. Forexample, suppose you have:
- EMAIL_HOST = smtp.example.com
- EMAIL_PORT = 25
- EMAIL_HOST_USER = janeausten
- EMAIL_HOST_PASSWORD = password
you can instead use:
- smtp://janeausten:password@smtp.example.com:25
For:
- TLS, add ?tls=True (and use port 587)
- SSL, add ?ssl=True (and use port 465)
to the URL. Note that TLS is preferred, and you can’t use both.
The URL is parsed using the dj-email-url library.
Additional Django email settings¶
Some additional email settings are available in Django. These can be provided as environmentvariables.
DEFAULT_FROM_EMAIL
- Allows you to specify a default
From
address for general automated messages from yourwebsite. SERVER_EMAIL
- Specifies a default
From
address for error messages from your site.
Usage, testing and troubleshooting¶
It’s beyond the scope of this document to discuss usage in detail. The official Djangodocumentation has more information.
It’s useful to be able to test your configuration. You can do this in your project’s local shell or Cloud shell.
Once in the shell, launch the Django shell:
- python manage.py shell
Import the Django send_mail
function:
- from django.core.mail import send_mail
and try sending a message:
- send_mail(
- "Welcome to Divio Cloud",
- "It's great!",
- "[[email protected]](http://docs.divio.com/cdn-cgi/l/email-protection)",
- ["[[email protected]](http://docs.divio.com/cdn-cgi/l/email-protection)"],
- fail_silently=False,
- )
The email settings will be taken from the EMAIL_URL environment variable, butcan be overwritten in the shell - for example:
- EMAIL_USE_TLS = True
原文: http://docs.divio.com/en/latest/reference/coding-sending-email.html