work
This commit is contained in:
		
							parent
							
								
									59eba8d7a8
								
							
						
					
					
						commit
						7819d8f79f
					
				
							
								
								
									
										18
									
								
								get.py
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								get.py
									
									
									
									
									
								
							| @ -19,16 +19,15 @@ imap_conn.login(email_address, password) | |||||||
| # Select the 'alerts' folder | # Select the 'alerts' folder | ||||||
| imap_conn.select('Alerts') | imap_conn.select('Alerts') | ||||||
| 
 | 
 | ||||||
| # Search for unseen emails in the 'alerts' folder | # Search for unseen emails in the 'alerts' folder, starting with the newest | ||||||
| status, email_ids = imap_conn.search(None, 'UNSEEN') | status, response = imap_conn.uid('search', None, 'UNSEEN') | ||||||
|  | email_ids = response[0].split() | ||||||
| 
 | 
 | ||||||
| # Loop through the email IDs | # Loop through the email UIDs | ||||||
| for email_id in email_ids[0].split(): | for email_id in email_ids: | ||||||
|     # Fetch the email by ID |     # Fetch the email by UID | ||||||
|     status, email_data = imap_conn.fetch(email_id, '(RFC822)') |     status, response = imap_conn.uid('fetch', email_id, '(RFC822)') | ||||||
| 
 |     raw_email = response[0][1] | ||||||
|     # Parse the email data |  | ||||||
|     raw_email = email_data[0][1] |  | ||||||
|     email_message = email.message_from_bytes(raw_email) |     email_message = email.message_from_bytes(raw_email) | ||||||
| 
 | 
 | ||||||
|     # Extract the body of the email |     # Extract the body of the email | ||||||
| @ -47,4 +46,3 @@ for email_id in email_ids[0].split(): | |||||||
| # Logout and close the connection | # Logout and close the connection | ||||||
| imap_conn.logout() | imap_conn.logout() | ||||||
| imap_conn.close() | imap_conn.close() | ||||||
| 
 |  | ||||||
|  | |||||||
							
								
								
									
										65
									
								
								get_csv.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								get_csv.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,65 @@ | |||||||
|  | import imaplib | ||||||
|  | import email | ||||||
|  | import csv | ||||||
|  | from getpass import getpass | ||||||
|  | 
 | ||||||
|  | # IMAP server settings for Hotmail | ||||||
|  | imap_server = 'imap-mail.outlook.com' | ||||||
|  | imap_port = 993 | ||||||
|  | 
 | ||||||
|  | # Email credentials | ||||||
|  | email_address = input('Email address: ') | ||||||
|  | password = getpass('Password: ') | ||||||
|  | 
 | ||||||
|  | # Connect to the IMAP server | ||||||
|  | imap_conn = imaplib.IMAP4_SSL(imap_server, imap_port) | ||||||
|  | 
 | ||||||
|  | # Login to the email account | ||||||
|  | imap_conn.login(email_address, password) | ||||||
|  | 
 | ||||||
|  | # Select the 'alerts' folder | ||||||
|  | imap_conn.select('alerts') | ||||||
|  | 
 | ||||||
|  | # Search for unseen emails in the 'alerts' folder, starting with the newest | ||||||
|  | status, response = imap_conn.uid('search', None, 'UNSEEN') | ||||||
|  | email_ids = response[0].split() | ||||||
|  | 
 | ||||||
|  | # Create a list to store the email data | ||||||
|  | emails = [] | ||||||
|  | 
 | ||||||
|  | # Loop through the email UIDs | ||||||
|  | for email_id in email_ids: | ||||||
|  |     # Fetch the email by UID | ||||||
|  |     status, response = imap_conn.uid('fetch', email_id, '(RFC822)') | ||||||
|  |     raw_email = response[0][1] | ||||||
|  |     email_message = email.message_from_bytes(raw_email) | ||||||
|  | 
 | ||||||
|  |     # Extract the desired information from the email | ||||||
|  |     uid = email_id.decode() | ||||||
|  |     date = email_message['Date'] | ||||||
|  |     sender = email.utils.parseaddr(email_message['From'])[1] | ||||||
|  | 
 | ||||||
|  |     # Extract the body of the email | ||||||
|  |     if email_message.is_multipart(): | ||||||
|  |         for part in email_message.get_payload(): | ||||||
|  |             if part.get_content_type() == 'text/plain': | ||||||
|  |                 email_body = part.get_payload(decode=True).decode() | ||||||
|  |                 break | ||||||
|  |     else: | ||||||
|  |         email_body = email_message.get_payload(decode=True).decode() | ||||||
|  | 
 | ||||||
|  |     # Add the email data to the list | ||||||
|  |     emails.append([uid, date, sender, email_body]) | ||||||
|  | 
 | ||||||
|  | # Logout and close the connection | ||||||
|  | imap_conn.logout() | ||||||
|  | imap_conn.close() | ||||||
|  | 
 | ||||||
|  | # Write the email data to a CSV file | ||||||
|  | output_file = 'email_data.csv' | ||||||
|  | with open(output_file, 'w', newline='') as file: | ||||||
|  |     writer = csv.writer(file) | ||||||
|  |     writer.writerow(['UID', 'Date', 'Sender', 'Body']) | ||||||
|  |     writer.writerows(emails) | ||||||
|  | 
 | ||||||
|  | print(f"Email data written to {output_file}.") | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user