-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_email.py
61 lines (49 loc) · 2.16 KB
/
send_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
51
52
53
54
55
56
57
58
59
60
__author__ = "Jonathan Braun"
__version__ = "1.1"
__maintainer__ = "Jonathan Braun"
__email__ = "jonathan.braun@eduvaud.ch"
__status__ = "Production"
__date__ = "December 2023"
#-----------------------------------------------------
# Importing libraries and modules
#-----------------------------------------------------
import datetime # Library for date and time related stuff
import smtplib # Library for email related stuff
#-----------------------------------------------------
# Declaring functions
#-----------------------------------------------------
def send_email(receiver, subject, message):
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
server.login("ETML.ES.EMSY@gmail.com","cely neve caly akjz")
sender = "ETML.ES.EMSY@gmail.com"
headers = {
'Content-Type': 'text/html; charset=utf-8',
'Content-Disposition': 'inline',
'Content-Transfer-Encoding': '8bit',
'From': sender,
'To':receiver,
'Date': datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S %Z'),
'X-Mailer': 'python',
'Subject': subject
}
# create the message
msg = ''
for key, value in headers.items():
msg += "%s: %s\n" % (key, value)
# add contents
msg += "\n%s\n" % (message)
try:
server.sendmail(headers['From'], headers['To'], msg.encode("utf8"))
server.quit()
print("Email sent successfully!")
except Exception as ex:
print("Something went wrong...", ex)
#-----------------------------------------------------
# Main script
#-----------------------------------------------------
if __name__ == "__main__": # Runs only if called as a script but not if imported
print("Hello This script is a test and will try to send an email")
# Sending an email to test the function
subject = "Ceci est un email de test"
message = "Bonjour! Ceci est un email de test."
send_email(["jeremie.jeanelie@eduvaud.ch"], subject, message)