Skip to content

Instantly share code, notes, and snippets.

@jossef
Last active June 22, 2021 22:56
Show Gist options
  • Save jossef/2a4a46a899820d5d57b4 to your computer and use it in GitHub Desktop.
Save jossef/2a4a46a899820d5d57b4 to your computer and use it in GitHub Desktop.
python gmail smtp mail sender with images
from email.mime.image import MIMEImage
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class MailSender(object):
def __init__(self, username, password, server='smtp.gmail.com', port=587, use_tls=True):
self.username = username
self.password = password
self.server = server
self.port = port
self.use_tls = use_tls
def send(self, sender, recipients, subject, message_plain='', message_html='', images=None):
'''
:param sender: str
:param recipients: [str]
:param subject: str
:param message_plain: str
:param message_html: str
:param images: [{id:str, path:str}]
:return: None
'''
msg_related = MIMEMultipart('related')
msg_related['Subject'] = subject
msg_related['From'] = sender
msg_related['To'] = ', '.join(recipients)
msg_related.preamble = 'This is a multi-part message in MIME format.'
msg_alternative = MIMEMultipart('alternative')
msg_related.attach(msg_alternative)
plain_part = MIMEText(message_plain, 'plain')
html_part = MIMEText(message_html, 'html')
msg_alternative.attach(plain_part)
msg_alternative.attach(html_part)
if images:
for image in images:
with open(image['path'], 'rb') as f:
msg_image = MIMEImage(f.read())
msg_image.add_header('Content-ID', '<{0}>'.format(image['id']))
msg_related.attach(msg_image)
# Sending the mail
server = smtplib.SMTP('{0}:{1}'.format(self.server, self.port))
try:
if self.use_tls:
server.starttls()
server.login(self.username, self.password)
server.sendmail(sender, recipients, msg_related.as_string())
finally:
server.quit()
#! /usr/bin/python
# -*- coding: utf-8 -*-
from mailsender import MailSender
username = 'username@gmail.com'
password = 'password'
sender = username
images = list()
images.append({
'id': 'logo',
'path': 'logo.png'
})
with open('template.html') as template_html, open('template.txt') as template_plain:
message_html = template_html.read()
message_plain = template_plain.read()
mail_sender = MailSender(username, password)
mail_sender.send(sender, ['destination1@gmail.com', 'destination2@gmail.com'], 'Subject Subject!', message_html=message_html, message_plain=message_plain, images=images)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<img width="200" height="59" src="cid:logo">
</body>
</html>
this is a plain text message
@getprakashkc
Copy link

Google is not allowing you to log in via smtplib because it has flagged this sort of login as "less secure", so what you have to do is go to this link while you're logged in to your google account, and allow the access:

https://www.google.com/settings/security/lesssecureapps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment