Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- from pathlib import Path
- import smtplib
- from email.mime.text import MIMEText
- from email.mime.multipart import MIMEMultipart
- from email.mime.base import MIMEBase
- from email import encoders
- import os.path
- email_server_credentials = r'../.ssh/email_server_credentials.csv' # keeping a secret
- def send_email(send_to_email, file_location, subject):
- # Setup headers
- message = subject
- msg = MIMEMultipart()
- msg['From'] = email
- msg['To'] = send_to_email
- msg['Subject'] = subject
- msg.attach(MIMEText(message, 'plain'))
- # Setup the attachment
- filename = os.path.basename(file_location)
- attachment = open(file_location, "rb")
- part = MIMEBase('application', 'octet-stream')
- part.set_payload(attachment.read())
- encoders.encode_base64(part)
- part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
- # Attach the attachment to the MIMEMultipart object
- msg.attach(part)
- server = smtplib.SMTP(smtpserver, 587)
- server.starttls()
- server.login(email, password)
- text = msg.as_string()
- server.sendmail(email, send_to_email, text)
- server.quit()
- # get my email server credentials
- with open(email_server_credentials) as f:
- smtpserver, email, password, email_prefix, email_domain = f.read().split(',')
- number_of_ids = 3
- ids = []
- # generate list of random numbers
- while len(ids) < number_of_ids:
- id = randint(1000, 1999)
- if not id in ids:
- ids.append(id)
- # create files
- for id in ids:
- Path(f'{id}.pdf').touch()
- # send messages
- for id in ids:
- if os.path.isfile(f'{id}.pdf'):
- send_email(f'{email_prefix}{id}{email_domain}', f'{id}.pdf', f'pdf for {id}')
- print(f'Sent email for id {id}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement