Advertisement
rofi93

Gmail Manager

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