Emails

send_mail

send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None,connection=None, html_message=None)

  • from_email

    • The sender’s address. Both [email protected] and kimi.huang <[email protected]> forms are legal. If omitted, the DEFAULT_FROM_EMAIL setting is used.

      # Refer: https://github.com/django/django/blob/master/django/core/mail/message.py#L290
      self.from_email = from_email or settings.DEFAULT_FROM_EMAIL
      
    • Must be same as EMAIL_HOST_USER, if not:

      SMTPSenderRefused: (501, 'mail from address must be same as authorization user', u'[email protected]')
      
      • EMAIL_HOST_USER is only used for authentication on the mail server.
  • recipient_list

    • Must be a list or tuple, if not:

      TypeError: "to" argument must be a list or tuple
      
  • html_message
    • If html_message is provided, the resulting email will be a multipart/alternative email withmessage as the text/plain content type and html_message as the text/html content type.
  • The return value will be the number of successfully delivered messages (which can be 0 or 1 since it can only send one message).

send_mass_mail

send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None, connection=None)

  • datatuple is a tuple in which each element is in this format:

    message1 = (subject, message, from_email, recipient_list)
    message2 = (subject, message, from_email, recipient_list)
    datatuple = (message1, message2)
    
  • send_mass_mail() vs. send_mail()

    The main difference between send_mass_mail() and send_mail() is that send_mail() opens a connection to the mail server each time it’s executed, while send_mass_mail() uses a single connection for all of its messages. This makes send_mass_mail() slightly more efficient.

  • The return value will be the number of successfully delivered messages.

mail_admins

mail_admins(subject, message, fail_silently=False, connection=None, html_message=None)

mail_managers

mail_managers(subject, message, fail_silently=False, connection=None, html_message=None)

Attach File

Multi Alternatives

  • send_mail(…, message, ..., html_message=None)

PS: The message and html_message will not display at the same time. Only recipients can not handle an alternative content type. The message will display.

Global Settings

# Email address that error messages come from.
SERVER_EMAIL = '[email protected]'

# The email backend to use. For possible shortcuts see django.core.mail.
# The default is to use the SMTP backend.
# Third-party backends can be specified by providing a Python path
# to a module that defines an EmailBackend class.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

# Host for sending email.
EMAIL_HOST = 'smtp.exmail.qq.com'

# Port for sending email.
EMAIL_PORT = 25

# Optional SMTP authentication information for EMAIL_HOST.
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = '<^_^>pwd<^_^>'
EMAIL_USE_TLS = False
EMAIL_USE_SSL = False
EMAIL_SSL_CERTFILE = None
EMAIL_SSL_KEYFILE = None
EMAIL_TIMEOUT = None

# Default email address to use for various automated correspondence from
# the site managers.
DEFAULT_FROM_EMAIL = 'kimi.huang <[email protected]>'

# People who get code error notifications.
# In the format [('Full Name', '[email protected]'), ('Full Name', '[email protected]')]
ADMINS = [('Kimi.Huang', '[email protected]'), ('QIMIN', '[email protected]')]

# Not-necessarily-technical managers of the site. They get broken link
# notifications and other various emails.
MANAGERS = ADMINS

# Subject-line prefix for email messages send with django.core.mail.mail_admins
# or ...mail_managers.  Make sure to include the trailing space.
EMAIL_SUBJECT_PREFIX = u'[Django] '

Email Reports Server Error

When DEBUG is False, 
Django will email the users listed in the ADMINS setting whenever your code raises an unhandled exception and results in an internal server error (HTTP status code 500). 
This gives the administrators immediate notification of any errors. 
The ADMINS will get a description of the error, a complete Python traceback, and details about the HTTP request that caused the error.

References

[1] [email protected], Sending email

[2] [email protected], Core Settings Topical Index — email

[3] [email protected], Django Global Settings

results matching ""

    No results matching ""