Skip to content

Instantly share code, notes, and snippets.

@srv89
Last active January 29, 2024 00:24
Show Gist options
  • Save srv89/1d3dac6672895f5ca65f to your computer and use it in GitHub Desktop.
Save srv89/1d3dac6672895f5ca65f to your computer and use it in GitHub Desktop.
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
@devMlGUE
Copy link

devMlGUE commented Jun 3, 2017

how can I set UTF-8 for the msg?

@rm17tink
Copy link

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
Copy link

Thanks this worked out perfectly for me!!!

@ldmontibeller
Copy link

So handy, thanks!

@lanreogun
Copy link

Perfect!

@lanreogun
Copy link

This is as awesome

@wghot
Copy link

wghot commented Jan 11, 2018

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

@willrte
Copy link

willrte commented Jun 7, 2018

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

@zimdero
Copy link

zimdero commented Jun 11, 2018

Thank you

@Birendra-Rai
Copy link

Perfect and great

@Ravenagape
Copy link

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
Copy link

Amazing

@subhsaha
Copy link

Thanks

@ddmitov
Copy link

ddmitov commented May 16, 2019

Thanks for the clean and understandable code!

@J-Lmb
Copy link

J-Lmb commented Oct 15, 2020

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

Can anyone helpt with this error?

@mlanca
Copy link

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