reduced slack functionality, higher ease of use. Database migration wip
This commit is contained in:
		
							
								
								
									
										42
									
								
								news_fetch/utils_mail/runner.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								news_fetch/utils_mail/runner.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
			
		||||
import smtplib
 | 
			
		||||
from email.mime.multipart import MIMEMultipart
 | 
			
		||||
from email.mime.text import MIMEText
 | 
			
		||||
from email.mime.application import MIMEApplication
 | 
			
		||||
import os
 | 
			
		||||
import logging
 | 
			
		||||
import configuration
 | 
			
		||||
 | 
			
		||||
logger = logging.getLogger(__name__)
 | 
			
		||||
config = configuration.main_config["MAIL"]
 | 
			
		||||
 | 
			
		||||
def send(article_model):
 | 
			
		||||
    mail = MIMEMultipart()
 | 
			
		||||
    mail['Subject'] = "{} -- {}".format(article_model.source_name, article_model.title)
 | 
			
		||||
    mail['From'] = config["sender"]
 | 
			
		||||
    mail['To'] = config["recipient"]
 | 
			
		||||
 | 
			
		||||
    msgs = article_model.mail_info # this is html
 | 
			
		||||
    msg = [m["reply_text"] for m in msgs]
 | 
			
		||||
    msg = "\n".join(msg)
 | 
			
		||||
 | 
			
		||||
    content = MIMEText(msg, "html")
 | 
			
		||||
    mail.attach(content)
 | 
			
		||||
 | 
			
		||||
    files = [m["file_path"] for m in msgs if m["file_path"]]
 | 
			
		||||
    for path in files:
 | 
			
		||||
        with open(path, 'rb') as file:
 | 
			
		||||
            part = MIMEApplication(file.read(), "pdf")
 | 
			
		||||
        # encoders.encode_base64(part)
 | 
			
		||||
        part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(path))
 | 
			
		||||
        mail.attach(part)
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        smtp = smtplib.SMTP(config["smtp_server"], config["port"])
 | 
			
		||||
        smtp.starttls()
 | 
			
		||||
        smtp.login(config["uname"], config["password"])
 | 
			
		||||
        smtp.sendmail(config["sender"], config["recipient"], mail.as_string())
 | 
			
		||||
        smtp.quit()
 | 
			
		||||
        logger.info("Mail successfully sent.")
 | 
			
		||||
    except smtplib.SMTPException as e:
 | 
			
		||||
        logger.error("Could not send mail for article {}".format(article_model))
 | 
			
		||||
        logger.info(e)
 | 
			
		||||
		Reference in New Issue
	
	Block a user