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 and EMAIL_PORT (defaults to 25)
Authentications settings
EMAIL_HOST_USER and EMAIL_HOST_PASSWORD
Secure authentication
EMAIL_USE_TLS and EMAIL_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:

  1. EMAIL_HOST = smtp.example.com
  2. EMAIL_PORT = 25
  3. EMAIL_HOST_USER = janeausten
  4. EMAIL_HOST_PASSWORD = password

you can instead use:

  1. 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:

  1. python manage.py shell

Import the Django send_mail function:

  1. from django.core.mail import send_mail

and try sending a message:

  1. send_mail(
  2. "Welcome to Divio Cloud",
  3. "It's great!",
  4. "[[email protected]](http://docs.divio.com/cdn-cgi/l/email-protection)",
  5. ["[[email protected]](http://docs.divio.com/cdn-cgi/l/email-protection)"],
  6. fail_silently=False,
  7. )

The email settings will be taken from the EMAIL_URL environment variable, butcan be overwritten in the shell - for example:

  1. EMAIL_USE_TLS = True

原文: http://docs.divio.com/en/latest/reference/coding-sending-email.html