-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_email.py
24 lines (20 loc) · 863 Bytes
/
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
import smtplib
import ssl
from configparser import ConfigParser
# Reading the configuration file
config = ConfigParser()
config.read('config.ini')
def send_email(message):
port = config.getint('mailserver', 'port')
smtp_server = config.get('mailserver', 'SMTPserver')
sender_email = config.get('mailserver', 'senderEmail')
receiver_email = config.get('mailserver', 'recepientEmail')
password = config.get('mailserver', 'senderPassword')
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
try:
server.login(sender_email, password)
res = server.sendmail(sender_email, receiver_email, message.as_string())
print(f"An email has been sent to {receiver_email}!")
except:
print("Error connecting to the mail server.")