ayushkhare12

Email Sushmitha Script

Sep 16th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. import smtplib
  2. from email.mime.text import MIMEText
  3. from email.mime.multipart import MIMEMultipart
  4. from email.mime.application import MIMEApplication
  5. from os.path import basename
  6.  
  7.  
  8. def send_mail(send_from: str, subject: str, text: str,
  9. send_to: list, files= None):
  10.  
  11.     send_to= default_address if not send_to else send_to
  12.  
  13.     msg = MIMEMultipart()
  14.     msg['From'] = send_from
  15.     msg['To'] = ', '.join(send_to)
  16.     msg['Subject'] = subject
  17.  
  18.     msg.attach(MIMEText(text))
  19.  
  20.     for f in files or []:
  21.         with open(f, "rb") as fil:
  22.             ext = f.split('.')[-1:]
  23.             attachedfile = MIMEApplication(fil.read(), _subtype = ext)
  24.             attachedfile.add_header(
  25.                 'content-disposition', 'attachment', filename=basename(f) )
  26.         msg.attach(attachedfile)
  27.  
  28.  
  29.     smtp = smtplib.SMTP(host="smtp.gmail.com", port= 587)
  30.     smtp.starttls()
  31.     smtp.login(username,password)
  32.     smtp.sendmail(send_from, send_to, msg.as_string())
  33.     smtp.close()
  34.  
  35.  
  36. username = '[email protected]'
  37. password = 'new3pass@123'
  38. default_address = ['[email protected]']
Add Comment
Please, Sign In to add comment