Django Send Mail

JunePyo Suh·2020년 6월 29일
0

Sending Emails with Django

Setting up

SMTP, Simple Mail Transfer Protocol, is a communication protocol for transmitting emails in electronic mail servers. Using SMTP server, we can send, receive, or relay outgoing mails.
Gmail SMTP server is a free SMTP service which anyone who has a Gmail account can use to send emails. To use SMTP server of an email service, write the following in your settings.py.

// EMAIL_BACKEND tells Django which custom or predefined email backend will work with EMAIL_HOST.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = 'username@gmail.com'
EMAIL_HOST_PASSWORD = 'your-gmail-password'
EMAIL_PORT = 587
// Choose the way to encrypt the mail and protect your user account.
// It is likely that your email provider explicitly tells which option to use.
EMAIL_USE_TLS = True
// EMAIL_USE_SSL = None
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

In case of Gmail, you should also choose "use IMAP" option in Gmail settings.

IMAP, or Internet Message Access Protocol, is a way of managing incoming email messages in which the user isn't actually downloading or storing an email when he or she reads the email; instead, the emails are stored on servers, so whenever you check your inbox, your email client contacts the server to connect you with your messages. As a result, IMAP makes it possible to check your email from several different devices without missing a thing.
(POP, on the other hand, works by contacting your email server and downloading all of your new messages from it. Once they are downloaded, they disappear from the server)

Then, allow access from less secure apps in Gmail support.

How to send emails via SMTP

Import send_mail or send_mass_mail function from django.core.mail.
send_mail uses a separate connection for each message, while send_mass_mail opens a single connect to the mail server and is mostly intended to handle mass emailing.

from django.core.mail import send_mail

send_mail(
    subject = 'That’s your subject'
    message = 'That’s your message body'
    from_email =from@yourdjangoapp.com’
    recipient_list = ['to@youruser.com',]
    auth_user = 'Login'
    auth_password = 'Password'	
    // fail_silently controls how the backend should handle errors; if True, exceptions will be silently ignored, while setting it as False will raise smtplib.SMTPException error.
    fail_silently = False,
)

What is EmailMessage for?

Email backend handles the email sending; the EmailMessage class answers for the message creation. To use some advanced features like BCC or an attachment, EmailMessage is desirable.
Some optional parameters for EmailMessage objects are:

  • connection: assigns an email backend instance for multiple messages
  • attachments
  • headers: describes extra headers, such as Message-ID or CC for the message
  • cc: specifies email addresses used in the 'CC' header

Sending attachments can be done by using attach or attach_file methods.
message.attach('Attachment.pdf', file_to_be_sent, 'file/pdf')
message.attach_file('/documents/Attachment.pdf')

Sending an HTML email

From Django 1.7, send_mail could send a message with HTML content.
mail.send_mail(subject, plain_message, from_email, [to], html_message=html_message)

Some Django email libraries

  • django-anymail
  • django-mailer
  • django-post_office
  • django-templated-email
  • django-mailbox

For more detailed information, check out this blog that I referenced.

0개의 댓글