· 6 years ago · May 26, 2019, 10:08 PM
1__author__ = 'srv'
2
3import smtplib
4from email.mime.multipart import MIMEMultipart
5from email.mime.text import MIMEText
6from email.mime.application import MIMEApplication
7
8username = '' # Email Address from the email you want to send an email
9password = '' # Password
10server = smtplib.SMTP('')
11
12"""
13SMTP Server Information
141. Gmail.com: smtp.gmail.com:587
152. Outlook.com: smtp-mail.outlook.com:587
163. Office 365: outlook.office365.com
17Please verify your SMTP settings info.
18"""
19
20# Create the body of the message (a HTML version for formatting).
21html = """Add you email body here"""
22
23
24# Function that send email.
25def send_mail(username, password, from_addr, to_addrs, msg):
26 server = smtplib.SMTP('smtp-mail.outlook.com', '587')
27 server.ehlo()
28 server.starttls()
29 server.ehlo()
30 server.login(username, password)
31 server.sendmail(from_addr, to_addrs, msg.as_string())
32 server.quit()
33
34# Read email list txt
35email_list = [line.strip() for line in open('email.txt')]
36
37for to_addrs in email_list:
38 msg = MIMEMultipart()
39
40 msg['Subject'] = "Hello How are you ?"
41 msg['From'] = from_addr
42 msg['To'] = to_addrs
43
44 # Attach HTML to the email
45 body = MIMEText(html, 'html')
46 msg.attach(body)
47
48 # Attach Cover Letter to the email
49 cover_letter = MIMEApplication(open("file1.pdf", "rb").read())
50 cover_letter.add_header('Content-Disposition', 'attachment', filename="file1.pdf")
51 msg.attach(cover_letter)
52
53 # Attach Resume to the email
54 cover_letter = MIMEApplication(open("file2.pdf", "rb").read())
55 cover_letter.add_header('Content-Disposition', 'attachment', filename="file2.pdf")
56 msg.attach(cover_letter)
57
58 try:
59 send_mail(username, password, from_addr, to_addrs, msg)
60 print "Email successfully sent to", to_addrs
61 except SMTPAuthenticationError:
62 print 'SMTPAuthenticationError'
63 print "Email not sent to", to_addrs