-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_send_an_email.py
More file actions
43 lines (36 loc) · 1.3 KB
/
08_send_an_email.py
File metadata and controls
43 lines (36 loc) · 1.3 KB
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
import smtplib
def send_mail(receiver, subject, body, **kwargs):
"""Sends an e-mail from a given e-mail address.
The receiver, subject and the body are customizable.
Args:
receiver (str): e-mail address to the receiver,
subject (str): title of the e-mail,
body (str): the text that should be included in the e-mail.
kwargs: there should be two information passed:
1. SENDER_MAIL (str): sender's e-mail,
2. SENDER_PASS (str): sender's e-mail password.
"""
email_text = """
From: {sender}
To: {receiver}
Subject: {subject}
{body}
""".format(sender=kwargs['SENDER_MAIL'], receiver=receiver, subject=subject, body=body)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
try:
server.login(kwargs['SENDER_MAIL'], kwargs['SENDER_PASS'])
server.sendmail(kwargs['SENDER_MAIL'], receiver, email_text)
print("Email sent successfully!")
except Exception as err:
print("Something went wrong….", err)
if __name__ == "__main__":
sender = {
'SENDER_MAIL': 'sender@example.com',
'SENDER_PASS': 'senders_secret_password'
}
send_mail(
'receiver@example.com',
'Test e-mail',
'This is the test',
sender
)