This commit is contained in:
2022-09-18 19:36:44 +01:00
parent 5833556aed
commit 9f6a4d3847

View File

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