Server response message : b'+OK Message follows' Server response message : b'+OK 1 157795' Get_email_by_index(user_email, user_password, 1)īelow is the above script output. Get_user_email_index(user_email, user_password) Get_user_email_status(user_email, user_password) Print('attached file is saved in path ' + attach_file_path) # write attached file content to the file. # get current script execution directory path.Ĭurrent_path = os.path.dirname(os.path.abspath(_file_))Īttach_file_path = current_path + '/' + attach_file_name Pos = attach_file_info_string.find(prefix)Īttach_file_name = attach_file_info_stringĪttach_file_data = msg.get_payload(decode=True) # get message header 'Content-Disposition''s value and parse out attached file name.Īttach_file_info_string = msg.get('Content-Disposition') # if this message part is an attachment part that means it is a attached file.Įlif content_type.startswith('image') or content_type.startswith('application'): # if this message part is still multipart such as 'multipart/mixed','multipart/alternative','multipart/related'Įlif content_type.startswith('multipart'): # parse the charset value from 'Content-Type' header value. # get message 'Content-Type' header value.Ĭontent_type = msg.get('Content-Type', '').lower() If content_type='text/plain' or content_type='text/html': Print('-Part ' + str(n) + ' content type : ' + content_type + '-')Ĭontent_type = msg.get_content_type().lower() # just parse from, to, subject header value. Print('Delete email at index : ' + email_index) # parse the email string to a MIMEMessage object. # join each line of email message content to create the email content and decode the data with utf-8 charset encoding. (resp_message, lines, octets) = pop3_server_conn.retr(email_index) Get user account email by the provided email account and email index number.ĭef get_email_by_index(user_email, user_password, email_index): Print('Server response message : ' + str(resp_message)) (resp_message, mails_list, octets) = pop3_server_conn.list() # get all user email list info from pop3 server. Print('Total message size : ' + str(totalMessageSize) + ' bytes.')ĭef get_user_email_index(user_email, user_password): Print('Email message numbers : ' + str(messageCount)) (messageCount, totalMessageSize) = pop3_server_conn.stat() # get user total email message count and email file size. # connect to pop3 server with the user account.Ĭonnect_pop3_server(user_email, user_password) Get email messages status of the given user.ĭef get_user_email_status(user_email, user_password):
PYTHON EMAIL PARSER EXAMPLE PASSWORD
# send user email and password to pop3 server.Ĭlose the pop3 server connection and release the connection object. Print('Below is pop3 server welcome messages : ') Welcome_message = pop3_server_conn.getwelcome() # get pop3 server welcome message and print on console. Pop3_server_conn = poplib.POP3(pop3_server_domain) # if pop3 server connection object is null then create it. # use global pop3_server_conn variable in this function. This method will connect to the global pop3 serverĪnd login with the provided user email and password.ĭef connect_pop3_server(user_email, user_password): The module is used to parse out one email message ( instance of MIMEMessage class) data such as from/to address, subject, content, and attached files. The python poplib module is used to connect to the pop3 email server and retrieve email messages data from the user email box.