-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday28_bulk_email.py
50 lines (36 loc) · 1.16 KB
/
day28_bulk_email.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# ECX 30 DAYS OF CODE AND DESIGN
# Day 28
"""
**Bulk E-mail**
Using the built-in SMTP module, write a function that takes a list of emails as input,
and sends each of them an(any) email message.
"""
import smtplib
import socket # To handle socket.gaierror
from email.message import EmailMessage
try:
sender_email = input('Enter email: ')
sender_password = input('Enter password: ')
msg = EmailMessage()
msg['Subject'] = 'Test Email From Python'
msg['From'] = sender_email
msg['To'] = ['[email protected]', '[email protected]']
msg.set_content('''\
Good day,
This is a test on the usage of python smtplib module in sending emails
Yours faithfully,
Test Email
''')
with smtplib.SMTP('smtp.mail.yahoo.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(sender_email, sender_password)
smtp.send_message(msg)
print('Message sent.')
except smtplib.SMTPAuthenticationError:
print('Wrong password or email.')
except smtplib.SMTPConnectError:
print('Error connecting to service.')
except socket.gaierror:
print('Socket.gaierror')