Email Handling in Django
1. Reference
1.1. API
Module: django.core.mail
send_mail() : Send a single mail
send_mass_mail(): Send mass mail
mail_admins(): Send mail to site admins
mail_managers(): Send mail to site managers
class EmailMessage : Create email messages ( Ref )
- Lets you add bcc addresses, headers, attachments
class SMTPConnection: Manage SMTP connection ( Ref)
- Lets you reuse SMTP connection for multiple email messages
class EmailMultiAlternatives : include multiple versions of the content in an e-mail (text + html ) (Ref)
- Exceptions
smtplib.SMTPException : SMTP errors
django.core.mail.BadHeaderError : Header invalidity e.g. newlines in headers
2. Usage
1 from django.core.mail import send_mail
2
3 send_mail('Subject',
4 'Message body',
5 'from@email.address',
6 ['to@email.address'],
7 fail_silently=False, #optional
8 auth_user=None, #optional
9 auth_password=None #optional
10 )
11
12 ####
13 # In settings.py
14
15 EMAIL_HOST='smtp.server'
16 EMAIL_PORT='smtp port'
17 EMAIL_HOST_USER='smtp auth username' #optional
18 EMAIL_HOST_PASSWORD='smtp auth password' #optional
19 EMAIL_USE_TLS='False' # Setting controls whether a secure connection is used.
20
