email gmail   0   2231
Sending Emails Using Python and Gmail

We already know how to send email using office 365 or how to send bulk emails using mailgun api.

Here we will see how to send email from your python script or Django App using Gmail account.

Gmail have some daily limits on the number of emails you can send in a day. If you need to send bulk emails, use mailgun api.


Code:

Import dependencies:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from django.utils.html import strip_tags

Create message:

msg = MIMEMultipart('alternative')
msg['Subject'] = "Test Mail : ThePythonDjango.Com"
msg['From'] = "sender-email-id@gmail.com"
msg['To'] = "receivers-email-id@gmail.com"


Create message body and attach to message:

html_text = '<div style="border:1px solid black">This is your message body in HTML format.</div>'
plain_text = strip_tags(html_text)

part1 = MIMEText(plain_text, 'plain')
part2 = MIMEText(html_text, 'html')

msg.attach(part1)
msg.attach(part2)


Send the message:

host = "smtp.gmail.com"
port = 587
mail = smtplib.SMTP(host, port, timeout=60)
mail.ehlo()
mail.starttls()

recepient = [msg["To"]]

username = "your-user-name"
password = "*****"
mail.login(username, password)
mail.sendmail(msg["From"], recepient, msg.as_string())
mail.quit()
 

Complete code with comments:


# import dependencies

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import email
import email.mime.application
from django.utils.html import strip_tags

# create message
msg = MIMEMultipart('alternative')
msg['Subject'] = "Test Mail : ThePythonDjango.Com"
msg['From'] = "sender-email-id@gmail.com"
msg['To'] = "receivers-email-id@gmail.com"

# create body
html_text = '<div style="border:1px solid black">This is your message body in HTML format.</div>'
plain_text = strip_tags(html_text)

# 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(plain_text, 'plain')
part2 = MIMEText(html_text, 'html')

# Attach image if any

# 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.
msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
host = "smtp.gmail.com"
port = 587
mail = smtplib.SMTP(host, port, timeout=60)
mail.ehlo()
mail.starttls()

# Add recepiens, cc or bcc if any
recepient = [msg["To"]]

# username and password of gmail id which will be used to send email
username = "your-user-name"
password = "*****"

# login using credentials
mail.login(username, password)

# send email
mail.sendmail(msg["From"], recepient, msg.as_string())
mail.quit()

print("\nSent\n")
 

Available on Gitlab as well. Please comment your views.

Host your Django Application for free on PythonAnyWhere Servers.  



email gmail   0   2231

Related Articles:
Adding Email Subscription Feature in Django Application
email subscription feature in django, sending email subscription confirmation mail in django, sending email unsubscribe email in Django, Add subscription feature in Django...
How to Track Email Opens Sent From Django App
How to track email opens. Tracking email sent from django app. Finding the email open rate in Python Django. Email behaviour of users in Python Django. Finding when email is opened by user in python-django....
Python Script 2 : Crawling all emails from a website
Website crawling for email address, web scraping for emails, data scraping and fetching email adress, python code to scrape all emails froma websites, automating the email id scraping using python script, collect emails using python script...
How to send bulk emails for free using Mailgun and Python Django
How to send bulk emails for free using Mailgun and Python-Django, How to send free promotional emails from your Django application. Free email Api....

0 thoughts on 'Sending Emails Using Python And Gmail'
Leave a comment:


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


SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.


Recent Posts:






© 2017-2019 Python Circle   Contact Us   Advertise with Us