Files
zShared/shared/attmail.py
2022-09-18 19:46:58 +01:00

53 lines
1.7 KiB
Python
Executable File

import smtplib
import toml
# import the corresponding modules
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
with open('/zTools/zPrivateConfig/secrets/config.toml') as f:
config = toml.load(f)
port = config['attmail']['port']
smtp_server = config['attmail']['smtp_server']
login = config['attmail']['login'] # paste your login generated by Mailtrap
password = config['attmail']['password'] # paste your password generated by Mailtrap
subject = "An example of boarding pass"
sender_email = config['attmail']['sender_email']
receiver_email = config['attmail']['receiver_email']
msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = receiver_email
msg["Subject"] = subject
# Add body to email
body = "This is an example of how you can send a boarding pass in attachment with Python"
msg.attach(MIMEText(body, "plain"))
filename = "/var/tmp/keys.tar"
# Open PDF file in binary mode
# We assume that the file is in the directory where you run your Python script from
with open(filename, "rb") as attachment:
# The content type "application/octet-stream" means that a MIME attachment is a binary file
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode to base64
encoders.encode_base64(part)
# Add header
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
# Add attachment to your message and convert it to string
msg.attach(part)
text = msg.as_string()
# send your email
with smtplib.SMTP_SSL(smtp_server, port) as server:
server.login(login, password)
server.sendmail(
sender_email, receiver_email, text
)
print('Sent')