Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import smtplib
- import threading
- from email import encoders
- from email.mime.multipart import MIMEMultipart
- from email.mime.base import MIMEBase
- from email.mime.text import MIMEText
- SMTP_HOST = "smtp.mail.yahoo.com"
- SMTP_PORT = 587
- SMTP_ACCO = "xxxxxxxxxxx@yahoo.com"
- SMTP_PASS = "xxxxxxxxxxx"
- FROM_ADDR = SMTP_ACCO
- class myThread(threading.Thread):
- def __init__(self, toaddr):
- threading.Thread.__init__(self)
- self.toaddr = toaddr
- def run(self):
- threadLock.acquire()
- sendTo(self.toaddr)
- threadLock.release()
- def sendTo(toaddr):
- outer = MIMEMultipart()
- outer['From'] = FROM_ADDR
- outer['To'] = toaddr
- outer['Subject'] = "Python email"
- body = "Hi, \nPython test mail."
- outer.attach(MIMEText(body, 'plain'))
- # attach file
- fp = open('file.pdf', 'rb')
- msg = MIMEBase('application', 'pdf')
- msg.set_payload(fp.read())
- fp.close()
- # Encode the payload
- encoders.encode_base64(msg)
- # Set the filename parameter
- msg.add_header('Content-Disposition', 'attachment', filename='file.pdf')
- outer.attach(msg)
- compose = outer.as_string()
- # Send the mail
- server = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
- server.set_debuglevel(False) # show communicationwith the server
- #Next, log in to the server
- server.ehlo()
- server.starttls()
- server.ehlo()
- server.login(SMTP_ACCO, SMTP_PASS)
- server.sendmail(FROM_ADDR, toaddr, compose)
- # close server
- server.quit()
- def main():
- # start time
- start = time.time()
- # send email to list recipients file
- f = open('recipients','r')
- for addr in f.readlines():
- t = myThread(addr)
- t.start()
- threads.append(t)
- f.close()
- # Wait for all threads to complete
- for t in threads:
- t.join()
- print('Time: ', time.time() - start)
- # init threadlock
- threadLock = threading.Lock()
- threads = []
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment