how to email   0   39
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.  



how to email   0   39

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:



© pythoncircle.com 2018-2019