Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # - *- coding: utf- 8 - *-
- """
- Created on Mon Feb 20 15:36:55 2017
- Name: MD. Khairul Basar
- Phone: 01760446942
- Email: khairul.basar93@gmail.com
- """
- import time;
- import email;
- import imaplib;
- import smtplib;
- from email.mime.text import MIMEText;
- from validate_email import validate_email;
- from email.mime.multipart import MIMEMultipart;
- def send_email(user_name,password):
- host = "smtp.gmail.com";
- port = 587;
- subject = "";
- message = "";
- recipients = [];
- print("To: ");
- recipients = input().split();
- print("Subject: ");
- subject = input();
- print("Message: ");
- while(True):
- line = input();
- if line:
- message+=line+"\n";
- else:
- break;
- if(len(recipients) == 0):
- print("No Recipient!\n");
- return;
- if(len(message) == 0):
- print("Message body is empty!\n");
- return;
- msg = MIMEMultipart();
- msg["Subject"] = subject;
- msg["From"] = user_name;
- msg["To"] = ",".join(recipients);
- body = MIMEText(message);
- msg.attach(body);
- message=msg.as_string();
- mail = smtplib.SMTP(host,port);
- mail.starttls();
- try:
- mail.login(user_name,password);
- except:
- print("Login Failed!\n");
- return;
- try:
- mail.sendmail(user_name,recipients,message);
- mail.quit();
- print("Your message has been sent!");
- except:
- print("Message sending failed!");
- return;
- def check_email(user_name,password):
- encoding = "UTF-8";
- host = "imap.gmail.com";
- mail = imaplib.IMAP4_SSL(host);
- try:
- mail.login(user_name,password);
- except:
- print("Login Failed!\n");
- return;
- result,tree = mail.list();
- if(result != "OK"):
- print("Error! Try again!\n");
- return;
- mail.select("inbox");
- result,data=mail.uid("search",None,"Unseen");
- if(result != "OK"):
- print("Error! Try again!\n");
- return;
- data=str(data[0])[2:-1];
- id_list = data.split();
- tail = "email";
- if(len(id_list) > 1):
- tail+="s";
- print("You have %d new %s\n"%(len(id_list),tail));
- for ids in id_list:
- result,data = mail.uid("fetch",ids,"(RFC822)");
- raw_email=data[0][1];
- raw_email_string = raw_email.decode(encoding);
- email_msg = email.message_from_string(raw_email_string);
- print("From: ",email_msg["From"]);
- print("Subject: ",email_msg["Subject"]);
- for part in email_msg.walk():
- if(part.get_content_type() == "text/plain"):
- body = part.get_payload(decode=True);
- try:
- body = body.decode(encoding);
- print("Message:\n",body);
- time.sleep(5);
- except:
- print("Email can not be decoded!");
- time.sleep(3);
- print("\n");
- mail.close();
- mail.logout();
- return;
- def take_logins():
- user_name = "";
- password = "";
- while(True):
- user_name = input("Username: ");
- password = input("Password: ");
- if(validate_email(user_name)):
- break;
- else:
- print("Invalid Email!\n");
- return user_name,password;
- def menu():
- print();
- print("1. Send Email");
- print("2. Check Email");
- print("3. Change Account");
- print("4. Quit");
- option = int(input());
- return option;
- exit_code = 0;
- user_name,password = take_logins();
- while(exit_code == 0):
- option = menu();
- if(option == 1):
- send_email(user_name,password);
- if(option == 2):
- check_email(user_name,password);
- if(option == 3):
- user_name,password = take_logins();
- if(option == 4):
- exit_code = 1;
- print("Good Bye!");
- time.sleep(3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement