Last active
January 29, 2024 00:24
-
-
Save srv89/1d3dac6672895f5ca65f to your computer and use it in GitHub Desktop.
Python code for sending HTML email (Attachment + Multiple Recipients )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__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 |
yes, the "from_addrs" and "to_addrs" are missing and i have a problem, "timeout error"
Thank you
Perfect and great
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
Amazing
Thanks
Thanks for the clean and understandable code!
Connection unexpectedly closed: [Errno 104] Connection reset by peer
Can anyone helpt with this error?
Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is "from_addr"? I can't find where we do define it.