Add logging and more options
This commit is contained in:
		
							parent
							
								
									bbe48d2c38
								
							
						
					
					
						commit
						4f4fc2b609
					
				
					 2 changed files with 23 additions and 8 deletions
				
			
		
							
								
								
									
										22
									
								
								mail.py
									
										
									
									
									
								
							
							
						
						
									
										22
									
								
								mail.py
									
										
									
									
									
								
							|  | @ -1,10 +1,9 @@ | |||
| import logging | ||||
| import smtplib | ||||
| import time | ||||
| 
 | ||||
| from string import Template | ||||
| 
 | ||||
| from email.mime.multipart import MIMEMultipart | ||||
| from email.mime.text import MIMEText | ||||
| from string import Template | ||||
| 
 | ||||
| import pandas as pd | ||||
| 
 | ||||
|  | @ -17,6 +16,8 @@ def get_contacts(filename): | |||
|     read from a file specified by filename. | ||||
|     """ | ||||
| 
 | ||||
|     logging.debug(f'Loading contacts from {filename}...') | ||||
| 
 | ||||
|     return pd.read_csv(filename) | ||||
| 
 | ||||
| 
 | ||||
|  | @ -32,6 +33,10 @@ def read_template(filename): | |||
| 
 | ||||
| 
 | ||||
| def main(): | ||||
|     logging.basicConfig(level=LOGLEVEL, format='%(asctime)s - %(levelname)s - %(message)s') | ||||
| 
 | ||||
|     logging.info('Starting pymail') | ||||
| 
 | ||||
|     contacts = get_contacts('contacts.csv') | ||||
|     message_template = read_template('message.html') | ||||
| 
 | ||||
|  | @ -39,10 +44,13 @@ def main(): | |||
|     s = smtplib.SMTP(HOST, PORT) | ||||
|     s.starttls() | ||||
|     s.login(MY_ADDRESS, PASSWORD) | ||||
|     logging.debug('Mail server is set up.') | ||||
| 
 | ||||
|     logging.info(f'Starting to send mails to {len(contacts.index)} recipients, this might take a while...') | ||||
| 
 | ||||
|     # For each contact, send the email: | ||||
|     for i in contacts.index: | ||||
|         print(f"Sending email {i+1}\n") | ||||
|         logging.info(f"Sending email {i+1} of {len(contacts.index)}\n") | ||||
|         msg = MIMEMultipart()       # create a message | ||||
| 
 | ||||
|         # add in the actual person name to the message template | ||||
|  | @ -50,7 +58,7 @@ def main(): | |||
|         message = message_template.substitute(contact_dict) | ||||
| 
 | ||||
|         # Prints out the message body for our sake | ||||
|         print(f"{message}\n") | ||||
|         logging.debug(f"{message}\n") | ||||
| 
 | ||||
|         # setup the parameters of the message | ||||
|         msg['From'] = MY_ADDRESS | ||||
|  | @ -60,16 +68,20 @@ def main(): | |||
|         # add in the message body | ||||
|         msg.attach(MIMEText(message, 'html')) | ||||
| 
 | ||||
|         if not DRY_RUN: | ||||
|             # send the message via the server set up earlier. | ||||
|             s.send_message(msg) | ||||
|         del msg | ||||
| 
 | ||||
|         # Wait for DELAY_SEC seconds every WAIT_EVERY-th message | ||||
|         if (i + 1) % WAIT_EVERY == 0: | ||||
|             time.sleep(DELAY_SEC) | ||||
| 
 | ||||
|     # Terminate the SMTP session and close the connection | ||||
|     s.quit() | ||||
| 
 | ||||
|     logging.info('Done!') | ||||
| 
 | ||||
| 
 | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
|  |  | |||
|  | @ -8,3 +8,6 @@ PORT = 587 | |||
| 
 | ||||
| WAIT_EVERY = 5 | ||||
| DELAY_SEC = 0 | ||||
| 
 | ||||
| LOGLEVEL = 'INFO' | ||||
| DRY_RUN = True | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue