how-to email   0   1592
How to send email from Python and Django using Office 365

To send email using python script via Office 365, use below code. This code is tried and tested.

import smtplib, os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def send_email():
        # Create message container - the correct MIME type is multipart/alternative.
        msg = MIMEMultipart('alternative')
        msg['From'] = ""
        msg['To'] = ""        
        msg['Subject'] = "test subject"
        # see the code below to use template as body
        body_text = "Hi this is body text of email"
        body_html = "<p>Hi this is body text of email</p>
        # Create the body of the message (a plain-text and an HTML version).
        # Record the MIME types of both parts - text/plain and text/html.
        part1 = MIMEText(body_text, 'plain')
        part2 = MIMEText(body_html, 'html')

        # Attach parts into message container.
        # According to RFC 2046, the last part of a multipart message, in this case
        # the HTML message, is best and preferred.
        # Send the message via local SMTP server.
        mail = smtplib.SMTP("", 587, timeout=20)

        # if tls = True                

        recepient = []
        mail.login(<login>, <password>)        
        mail.sendmail("", recepient, msg.as_string())        
    except Exception as e:
        raise e

Email contains two body part, html and text. You can use Django template in email.

from django.utils.html import strip_tags
from django.template import Context, Template
from django.template.loader import get_template

template = get_template("myapp/sample_template.html")
context = Context(context_data)
body_html = template.render(context_data)			
body_text = strip_tags(body_html)

It is always advisable to store credentials in separate config file and not to commit that file on Github.

Same code can be used to send email via Gmail and Google Apps with slight modification.

Use below settings:

Gmail :

Host :
Port : 587
tls : True

Google Apps:

Host :
Port : 465
tls : False

Warning: Do not send too many mail using python script via Gmail. Your account may be blocked. Daily email limit is 500 or 1000 emails. 

If you are sending more than 500 emails daily, I recommend using Mailgun. They provide nice api and you can send 20000 emails per month for free.

Read also : How to send bulk emails using Mailgun and Python-Django.

Please comment in case of any query.

how-to email   0   1592

Related Articles:
Python Script 3: Validate, format and Beautify JSON string Using Python
Validating json using python code, format and beautify json file using python, validate json file using python, how to validate, format and beautify json...
Encryption-Decryption in Python Django
How to encrypt and decrypt the content in DJango. Encrypting the critical information in Django App. Encrypting username, email and password in Django...
Adding Robots.txt file to Django Application
Adding robots.txt file in your Django application. Easiest way to add robots.txt file. Django application robots.txt file. Why should you add robots.txt file in your Django Application....
Scraping 10000 tweets in 60 seconds using celery, RabbitMQ and Docker cluster with rotating proxy
Scrapping large amount of tweets within minutes using celery, RabbitMQ and docker cluster. Scraping huge data quickly using docker cluster with TOR....

0 thoughts on 'How To Send Email From Python And Django Using Office 365'
Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

Please subscribe to get the latest articles in your mailbox.

Recent Posts:

© 2018-2019