Skip to content

Commit

Permalink
add more receivers in mail
Browse files Browse the repository at this point in the history
  • Loading branch information
mrzaizai2k committed Oct 10, 2024
1 parent 6971af3 commit 587e73c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
4 changes: 3 additions & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ excel:
mail:
smtp_server: smtp.gmail.com
port: 587
receiver: [email protected] # For demo only
receivers: # For demo only
- [email protected]
- [email protected]

modify_invoice_remind:
subject: "[No Reply - Done extracting Invoice Information]"
Expand Down
4 changes: 2 additions & 2 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def process_change_stream(ocr_reader, invoice_extractor, config):
mongo_db.update_document_by_id(str(document_id), new_data)

email_sender.send_email(email_type='modify_invoice_remind',
receiver=None,
receivers=None,
)

except Exception as e:
Expand Down Expand Up @@ -121,7 +121,7 @@ def process_change_stream(ocr_reader, invoice_extractor, config):
employee_expense_report_path, output_2_excel = export_json_to_excel(invoice_pairs=invoice_pairs, logger=logger)
if employee_expense_report_path or output_2_excel:
email_sender.send_email(email_type='send_excel',
receiver=None,
receivers=None,
attachment_paths=[employee_expense_report_path, output_2_excel])
logger.debug(f"attachment_paths: {[employee_expense_report_path, output_2_excel]}")
except Exception as e:
Expand Down
29 changes: 18 additions & 11 deletions src/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,30 @@ def __init__(self, config: dict, logger=None):
self.logger = logger

def send_email(self, email_type: Literal['modify_invoice_remind', 'send_excel'],
receiver: str = None,
receivers: list[str] = None,
attachment_paths: Optional[List[str]] = None):
"""
Function to send email
email_type:
- 'modify_invoice_remind': To remind the employees that the invoices have been extracted, go to website and modify the info
- 'send_excel': Send email to admin with excel files
receivers: List of email string
attachment_paths: List of path to attachment files
"""
try:
# Connect to the SMTP server for each email
server = smtplib.SMTP(self.smtp_server, self.port)
server.starttls()
server.login(self.login, self.password)

if not receiver:
receiver = self.config["receiver"]
if not receivers:
receivers = self.config["receivers"]

message = self._prepare_email(email_type=email_type, receiver=receiver, attachment_paths=attachment_paths)
message = self._prepare_email(email_type=email_type, receivers=receivers, attachment_paths=attachment_paths)
server.send_message(message)
print(f"Email sent successfully to {receiver}")
print(f"Email sent successfully to {receivers}")
if self.logger:
self.logger.debug(f"Email sent to {receiver}")
self.logger.debug(f"Email sent to {receivers}")

except Exception as e:
msg = f"Error sending email: {str(e)}"
Expand All @@ -51,7 +59,7 @@ def send_email(self, email_type: Literal['modify_invoice_remind', 'send_excel'],
server.quit()

def _prepare_email(self, email_type: Literal['modify_invoice_remind', 'send_excel'],
receiver: str = None,
receivers: str = None,
attachment_paths: Optional[List[str]] = None):

config = self.config[email_type]
Expand All @@ -61,7 +69,7 @@ def _prepare_email(self, email_type: Literal['modify_invoice_remind', 'send_exce
message = MIMEMultipart()
message["Subject"] = subject
message["From"] = self.login
message["To"] = receiver
message["To"] = ', '.join(receivers)
message.attach(MIMEText(body, "plain"))

# Add attachments
Expand Down Expand Up @@ -112,13 +120,12 @@ def _attach_files(self, message, attachment_paths):

email_sender.send_email(
email_type="modify_invoice_remind",
receiver="[email protected]",
receivers=None,
# attachment_paths=["output/Stdi_08_24.xlsx", "output/1.4437_10578_A3DS GmbH_04_2024 .xlsm"], # List of file paths
)


email_sender.send_email(
email_type="send_excel",
receiver="[email protected]",
receivers=["[email protected]"],
attachment_paths=["output/Stdi_08_24.xlsx", "output/1.4437_10578_A3DS GmbH_04_2024 .xlsm", "notfoundfile.txt"], # List of file paths
)

0 comments on commit 587e73c

Please sign in to comment.