mayankjoin3

python email

Nov 27th, 2022
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. import smtplib
  2. from email.mime.multipart import MIMEMultipart
  3. from email.mime.text import MIMEText
  4. from email.mime.base import MIMEBase
  5. from email import encoders
  6.  
  7. import csv
  8. from random import randint
  9. from time import sleep
  10.  
  11.  
  12.  
  13. def send_mail(fromaddr, frompasswd, toaddr, msg_subject, msg_body, file_path):
  14.     try:
  15.         msg = MIMEMultipart()
  16.         print("[+] Message Object Created")
  17.     except:
  18.         print("[-] Error in Creating Message Object")
  19.         return
  20.  
  21.     msg['From'] = fromaddr
  22.  
  23.     msg['To'] = toaddr
  24.  
  25.     msg['Subject'] = msg_subject
  26.  
  27.     body = msg_body
  28.  
  29.     msg.attach(MIMEText(body, 'plain'))
  30.  
  31.     filename = file_path
  32.     attachment = open(filename, "rb")
  33.  
  34.     p = MIMEBase('application', 'octet-stream')
  35.  
  36.     p.set_payload((attachment).read())
  37.  
  38.     encoders.encode_base64(p)
  39.  
  40.     p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
  41.  
  42.     try:
  43.         msg.attach(p)
  44.         print("[+] File Attached")
  45.     except:
  46.         print("[-] Error in Attaching file")
  47.         return
  48.  
  49.     try:
  50.         #s = smtplib.SMTP('smtp.gmail.com', 587)
  51.         s = smtplib.SMTP('mail.iitp.ac.in', 587)
  52.         print("[+] SMTP Session Created")
  53.     except:
  54.         print("[-] Error in creating SMTP session")
  55.         return
  56.  
  57.     s.starttls()
  58.  
  59.     try:
  60.         s.login(fromaddr, frompasswd)
  61.         print("[+] Login Successful")
  62.     except:
  63.         print("[-] Login Failed")
  64.  
  65.     text = msg.as_string()
  66.  
  67.     try:
  68.         s.sendmail(fromaddr, toaddr, text)
  69.         print("[+] Mail Sent successfully")
  70.     except:
  71.         print('[-] Mail not sent')
  72.  
  73.     s.quit()
  74.  
  75.  
  76. def isEmail(x):
  77.     if ('@' in x) and ('.' in x):
  78.         return True
  79.     else:
  80.         return False
  81.  
  82.  
  83. f = open('found_details.csv', 'r')
  84. reader = csv.reader(f)
  85.  
  86. FROM_ADDR = "email@iitp.ac.in"
  87. FROM_PASSWD = "Change"
  88.  
  89. Subject = "Mandatory QR Code for IITP Gate Entry/Exit "
  90. Body ='''
  91. Dear Student,
  92.  
  93. '''
  94. from datetime import datetime
  95.  
  96. start_time = datetime.now()
  97.  
  98. #what a ever is the limit of your sending mails, like gmail has 500.
  99. max_count = 9999999
  100. count=0
  101. last_email = open("last_email.txt","a")
  102. data_list = [row for row in reader]
  103. f.close()
  104. for row in data_list:
  105.     file_path = 'qrcodes\\'+row[1] + '.png'
  106.        
  107.     if isEmail(row[8]) and count <=max_count:
  108.         count+=1
  109.         print(row[8])
  110.         last_email.write(row[8].strip()+"\n")
  111.         send_mail(FROM_ADDR, FROM_PASSWD, row[8], Subject, Body, file_path)
  112.     if isEmail(row[9]) and count <=max_count:
  113.         count+=1
  114.         print(row[9])
  115.         last_email.write(row[9].strip()+"\n")
  116.         send_mail(FROM_ADDR, FROM_PASSWD, row[9], Subject, Body, file_path)
  117.     print("Count Value: ", count)
  118.     print("Sleeping . .. . ")
  119.     sleep(randint(1,3))
  120.     if count == max_count:
  121.         break
  122.  
  123. print("Count Max is reached: " ,count)
  124. last_email.close()
  125.  
  126. end_time = datetime.now()
  127. print('Duration: {}'.format(end_time - start_time))
  128.  
Add Comment
Please, Sign In to add comment