This repository has been archived on 2024-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
NG_2023_Video/mail.py

58 lines
1.9 KiB
Python
Raw Normal View History

2023-06-15 18:04:28 +00:00
import configparser
import os
import smtplib
import ssl
2023-06-06 15:18:12 +00:00
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
config = configparser.ConfigParser()
config.read('config.ini')
2023-06-15 18:04:28 +00:00
2023-06-06 15:18:12 +00:00
def send_email(destination, attachment):
subject = 'Your modified video'
body = 'You have recently used our service to modify a video. The result is attached.'
sender_email = config.get('Mail', 'Username')
receiver_email = destination
password = config.get('Mail', 'Password')
2023-06-15 18:04:28 +00:00
2023-06-06 15:18:12 +00:00
# Create a multipart message and set headers
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = receiver_email
message['Subject'] = subject
2023-06-15 18:04:28 +00:00
2023-06-06 15:18:12 +00:00
# Add body to email
message.attach(MIMEText(body, 'plain'))
with open(attachment, 'rb') as file:
part = MIMEBase('application', 'octet-stream')
part.set_payload(file.read())
2023-06-15 18:04:28 +00:00
2023-06-06 15:18:12 +00:00
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
2023-06-15 18:04:28 +00:00
2023-06-06 15:18:12 +00:00
# Add header as key/value pair to attachment part
part.add_header(
'Content-Disposition',
2023-06-17 14:09:50 +00:00
f'attachment; filename= {os.path.basename(attachment)}',
2023-06-06 15:18:12 +00:00
)
2023-06-15 18:04:28 +00:00
2023-06-06 15:18:12 +00:00
# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()
2023-06-15 18:04:28 +00:00
2023-06-06 15:18:12 +00:00
# Log in to server using secure context and send email
if config.getboolean('Mail', 'SSL'):
context = ssl.create_default_context()
with smtplib.SMTP_SSL(config.get('Mail', 'Server'), config.get('Mail', 'Port'), context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, text)
else:
with smtplib.SMTP(config.get('Mail', 'Server'), config.get('Mail', 'Port')) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, text)