rofi93

Gmail Manager

Mar 5th, 2017
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.00 KB | None | 0 0
  1. # - *- coding: utf- 8 - *-
  2. """
  3. Created on Mon Feb 20 15:36:55 2017
  4.  
  5. Name: MD. Khairul Basar
  6. Phone: 01760446942
  7. """
  8.  
  9. import time;
  10. import email;
  11. import imaplib;
  12. import smtplib;
  13. from email.mime.text import MIMEText;
  14. from validate_email import validate_email;
  15. from email.mime.multipart import MIMEMultipart;
  16.  
  17. def send_email(user_name,password):
  18.    
  19.     host = "smtp.gmail.com";
  20.     port = 587;
  21.    
  22.     subject = "";
  23.     message = "";
  24.     recipients = [];
  25.  
  26.     print("To: ");
  27.     recipients = input().split();
  28.     print("Subject: ");
  29.     subject = input();
  30.     print("Message: ");
  31.     while(True):
  32.         line = input();
  33.         if line:
  34.             message+=line+"\n";
  35.         else:
  36.             break;
  37.            
  38.     if(len(recipients) == 0):
  39.         print("No Recipient!\n");
  40.         return;
  41.     if(len(message) == 0):
  42.         print("Message body is empty!\n");
  43.         return;
  44.    
  45.     msg = MIMEMultipart();
  46.  
  47.     msg["Subject"] = subject;
  48.     msg["From"] = user_name;
  49.     msg["To"] = ",".join(recipients);
  50.     body = MIMEText(message);
  51.     msg.attach(body);
  52.  
  53.     message=msg.as_string();
  54.    
  55.     mail = smtplib.SMTP(host,port);
  56.     mail.starttls();
  57.     try:
  58.         mail.login(user_name,password);
  59.     except:
  60.         print("Login Failed!\n");
  61.         return;
  62.  
  63.     try:
  64.         mail.sendmail(user_name,recipients,message);
  65.         mail.quit();
  66.         print("Your message has been sent!");
  67.     except:
  68.         print("Message sending failed!");
  69.     return;
  70.    
  71. def check_email(user_name,password):
  72.    
  73.     encoding = "UTF-8";
  74.     host = "imap.gmail.com";
  75.     mail = imaplib.IMAP4_SSL(host);
  76.     try:
  77.         mail.login(user_name,password);
  78.     except:
  79.         print("Login Failed!\n");
  80.         return;
  81.     result,tree = mail.list();
  82.     if(result != "OK"):
  83.         print("Error! Try again!\n");
  84.         return;
  85.    
  86.     mail.select("inbox");
  87.     result,data=mail.uid("search",None,"Unseen");
  88.     if(result != "OK"):
  89.         print("Error! Try again!\n");
  90.         return;
  91.    
  92.     data=str(data[0])[2:-1];    
  93.     id_list = data.split();
  94.  
  95.     tail = "email";
  96.     if(len(id_list) > 1):
  97.         tail+="s";
  98.     print("You have %d new %s\n"%(len(id_list),tail));
  99.  
  100.     for ids in id_list:
  101.        
  102.         result,data = mail.uid("fetch",ids,"(RFC822)");
  103.         raw_email=data[0][1];
  104.         raw_email_string = raw_email.decode(encoding);
  105.         email_msg = email.message_from_string(raw_email_string);
  106.        
  107.         print("From: ",email_msg["From"]);
  108.         print("Subject: ",email_msg["Subject"]);
  109.  
  110.         for part in email_msg.walk():
  111.            
  112.             if(part.get_content_type() == "text/plain"):
  113.                 body = part.get_payload(decode=True);
  114.                 try:
  115.                     body = body.decode(encoding);
  116.                     print("Message:\n",body);
  117.                     time.sleep(5);
  118.                 except:
  119.                     print("Email can not be decoded!");
  120.                     time.sleep(3);
  121.         print("\n");
  122.  
  123.     mail.close();
  124.     mail.logout();
  125.     return;
  126.    
  127. def take_logins():
  128.    
  129.     user_name = "";
  130.     password = "";
  131.    
  132.     while(True):
  133.         user_name = input("Username: ");
  134.         password = input("Password: ");
  135.         if(validate_email(user_name)):
  136.             break;
  137.         else:
  138.             print("Invalid Email!\n");
  139.     return user_name,password;
  140.    
  141. def menu():
  142.    
  143.     print();
  144.     print("1. Send Email");
  145.     print("2. Check Email");
  146.     print("3. Change Account");
  147.     print("4. Quit");
  148.  
  149.     option = int(input());
  150.     return option;
  151.  
  152. exit_code = 0;
  153. user_name,password = take_logins();
  154. while(exit_code == 0):
  155.    
  156.     option = menu();
  157.     if(option == 1):
  158.         send_email(user_name,password);
  159.     if(option == 2):
  160.         check_email(user_name,password);
  161.     if(option == 3):
  162.         user_name,password = take_logins();
  163.     if(option == 4):
  164.         exit_code = 1;
  165.         print("Good Bye!");
  166.         time.sleep(3);
Advertisement
Add Comment
Please, Sign In to add comment