Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python code for sending HTML email (Attachment + Multiple Recipients )
__author__ = 'srv'
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
username = '' # Email Address from the email you want to send an email
password = '' # Password
server = smtplib.SMTP('')
"""
SMTP Server Information
1. Gmail.com: smtp.gmail.com:587
2. Outlook.com: smtp-mail.outlook.com:587
3. Office 365: outlook.office365.com
Please verify your SMTP settings info.
"""
# Create the body of the message (a HTML version for formatting).
html = """Add you email body here"""
# Function that send email.
def send_mail(username, password, from_addr, to_addrs, msg):
server = smtplib.SMTP('smtp-mail.outlook.com', '587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username, password)
server.sendmail(from_addr, to_addrs, msg.as_string())
server.quit()
# Read email list txt
email_list = [line.strip() for line in open('email.txt')]
for to_addrs in email_list:
msg = MIMEMultipart()
msg['Subject'] = "Hello How are you ?"
msg['From'] = from_addr
msg['To'] = to_addrs
# Attach HTML to the email
body = MIMEText(html, 'html')
msg.attach(body)
# Attach Cover Letter to the email
cover_letter = MIMEApplication(open("file1.pdf", "rb").read())
cover_letter.add_header('Content-Disposition', 'attachment', filename="file1.pdf")
msg.attach(cover_letter)
# Attach Resume to the email
cover_letter = MIMEApplication(open("file2.pdf", "rb").read())
cover_letter.add_header('Content-Disposition', 'attachment', filename="file2.pdf")
msg.attach(cover_letter)
try:
send_mail(username, password, from_addr, to_addrs, msg)
print "Email successfully sent to", to_addrs
except SMTPAuthenticationError:
print 'SMTPAuthenticationError'
print "Email not sent to", to_addrs
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented May 22, 2017

Thank you, That is really helpful to me 👍

@devMlGUE

This comment has been minimized.

Copy link

@devMlGUE devMlGUE commented Jun 3, 2017

how can I set UTF-8 for the msg?

@rm17tink

This comment has been minimized.

Copy link

@rm17tink rm17tink commented Jun 20, 2017

If I get a timeout issue, can I work around the proxy setting(z-scaler)? Every other email retrieval and sender doesn't time out on me.

@lanreogun

This comment has been minimized.

Copy link

@lanreogun lanreogun commented Oct 8, 2017

Thanks this worked out perfectly for me!!!

@ldmontibeller

This comment has been minimized.

Copy link

@ldmontibeller ldmontibeller commented Nov 8, 2017

So handy, thanks!

@lanreogun

This comment has been minimized.

Copy link

@lanreogun lanreogun commented Nov 20, 2017

Perfect!

@lanreogun

This comment has been minimized.

Copy link

@lanreogun lanreogun commented Nov 29, 2017

This is as awesome

@wghot

This comment has been minimized.

Copy link

@wghot wghot commented Jan 11, 2018

What is "from_addr"? I can't find where we do define it.

@willrte

This comment has been minimized.

Copy link

@willrte willrte commented Jun 7, 2018

yes, the "from_addrs" and "to_addrs" are missing and i have a problem, "timeout error"

@zimdero

This comment has been minimized.

Copy link

@zimdero zimdero commented Jun 11, 2018

Thank you

@Birendra-Rai

This comment has been minimized.

Copy link

@Birendra-Rai Birendra-Rai commented Jul 26, 2018

Perfect and great

@Ravenagape

This comment has been minimized.

Copy link

@Ravenagape Ravenagape commented Oct 17, 2018

do you have an example of using this with Oauth2?

I need to get through the 2 factor authentication on our outlook office 365 accounts

@subhsaha

This comment has been minimized.

Copy link

@subhsaha subhsaha commented May 13, 2019

Amazing

@subhsaha

This comment has been minimized.

Copy link

@subhsaha subhsaha commented May 13, 2019

Thanks

@ddmitov

This comment has been minimized.

Copy link

@ddmitov ddmitov commented May 16, 2019

Thanks for the clean and understandable code!

@J-Lmb

This comment has been minimized.

Copy link

@J-Lmb J-Lmb commented Oct 15, 2020

Connection unexpectedly closed: [Errno 104] Connection reset by peer

Can anyone helpt with this error?

@mlanca

This comment has been minimized.

Copy link

@mlanca mlanca commented May 22, 2021

Thank you!

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