import re import os import dotenv import mistune import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart dotenv.load_dotenv() def generate_post_html(title, summary, mindmap, citation): html_summary = mistune.html(summary) post = f""" {title}

{title.replace("&", "&")}



SUMMARY

{html_summary.replace("&", "&")}



MINDMAP



CITATION

{mistune.html(citation.replace("&", "&"))}

#end """ return post def sanitize_citation(citation): pattern = r"(https://doi\.org/\S+)" sanitized_citation = re.sub( pattern, lambda match: f"[{match.group(1)}](https://doi.org/{match.group(1).split('/')[-1]})", citation ) return sanitized_citation def create_post(title, category, summary, mindmap, citation): mail_title = f"[{category}] {title}" mail_body = generate_post_html(title, summary, mindmap, sanitize_citation(citation)) return mail_title, mail_body def send_mail(mail_title, mail_body): sender_email = os.getenv('SENDER_EMAIL') sender_password = os.getenv('SENDER_PASSWORD') receiver_email = os.getenv('RECEIVER_EMAIL') msg = MIMEMultipart() msg['From'] = sender_email msg['To'] = receiver_email msg['Subject'] = mail_title msg.attach(MIMEText(mail_body, 'HTML')) smtp_server = os.getenv('SMTP_SERVER') smtp_port = os.getenv('SMTP_PORT') res = None try: server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() server.login(sender_email, sender_password) text = msg.as_string() server.sendmail(sender_email, receiver_email, text) print('Email sent successfully') except Exception as e: print('Email not sent. An error occurred:', str(e)) finally: server.quit() return res def main(title, category, summary, mindmap, citation, access_key): if access_key != os.getenv('ACCESS_KEY'): return False try: mail_title, mail_body = create_post(title, category, summary, mindmap, citation) send_mail(mail_title, mail_body) return True except Exception as e: print('An error occurred:', str(e)) return False