Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d8fe24718 | |||
| deaddd8132 | |||
| 6e08b0680f |
@@ -0,0 +1,15 @@
|
||||
from flask import Flask
|
||||
from app.extensions import mail
|
||||
from app.utils.mail import send_report_email, get_report_data
|
||||
from app.config import settings
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(settings)
|
||||
|
||||
mail.init_app(app)
|
||||
|
||||
with app.app_context():
|
||||
report_data = get_report_data()
|
||||
recipients = ["vdagbue@gmail.com"]
|
||||
result = send_report_email(report_data, recipients)
|
||||
print(result)
|
||||
+5
-1
@@ -1,4 +1,5 @@
|
||||
from flask import Flask
|
||||
from flask_mail import Mail
|
||||
import os
|
||||
from flask_swagger_ui import get_swaggerui_blueprint
|
||||
from flask_cors import CORS
|
||||
@@ -7,7 +8,7 @@ from app.api.routes import api
|
||||
from app.errors import register_error_handlers
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_migrate import Migrate
|
||||
from app.extensions import db, migrate
|
||||
from app.extensions import db, migrate, mail
|
||||
from flask_jwt_extended import (
|
||||
JWTManager,
|
||||
jwt_required,
|
||||
@@ -47,6 +48,9 @@ def create_app():
|
||||
|
||||
from . import models
|
||||
|
||||
# Initialize Flask-Mail
|
||||
mail.init_app(app)
|
||||
|
||||
# Database and Migrations
|
||||
db.init_app(app)
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ from flask import jsonify
|
||||
from marshmallow import ValidationError
|
||||
import logging
|
||||
from app.api.integrations import KafkaIntegration
|
||||
from app.utils.mail import send_report_email, get_report_data
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -173,3 +174,6 @@ class BaseService:
|
||||
# return {"rate": 0, "fee": 0, "due_days": 0}
|
||||
|
||||
|
||||
@classmethod
|
||||
def send_mail(cls, report_data, recipients):
|
||||
send_report_email(report_data, recipients)
|
||||
|
||||
+9
-1
@@ -85,7 +85,15 @@ class Config:
|
||||
"salarypaymenT_6"
|
||||
]
|
||||
|
||||
|
||||
MAIL_SERVER = os.getenv('MAIL_SERVER','smtp.zoho.com')
|
||||
MAIL_PORT = 587
|
||||
MAIL_USERNAME = os.getenv('MAIL_USERNAME', 'firstadvance@dynamikservices.tech')
|
||||
MAIL_PASSWORD = os.getenv('MAIL_PASSWORD')
|
||||
MAIL_USE_TLS = True
|
||||
MAIL_USE_SSL = False
|
||||
MAIL_DEFAULT_SENDER = ('FirstAdvance', 'firstadvance@dynamikservices.tech')
|
||||
MAIL_RECEIVER= os.getenv('MAIL_RECEIVER', 'vdagbue@gmail.com')
|
||||
|
||||
|
||||
|
||||
settings = Config()
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_migrate import Migrate
|
||||
from flask_mail import Mail
|
||||
|
||||
mail = Mail()
|
||||
db = SQLAlchemy()
|
||||
migrate = Migrate()
|
||||
@@ -0,0 +1,40 @@
|
||||
from flask_mail import Message
|
||||
from flask import current_app
|
||||
from app.extensions import mail
|
||||
import pandas as pd
|
||||
from io import BytesIO
|
||||
|
||||
def get_report_data():
|
||||
"""
|
||||
Fetch and return loan summary data.
|
||||
"""
|
||||
return [
|
||||
{"Type": "Disbursement", "Count": 45},
|
||||
{"Type": "Repayment", "Count": 32},
|
||||
]
|
||||
def send_report_email(report_data: list, recipients: list):
|
||||
"""
|
||||
Sends an HTML + Excel report to the given email recipients.
|
||||
"""
|
||||
df = pd.DataFrame(report_data)
|
||||
output = BytesIO()
|
||||
df.to_excel(output, index=False)
|
||||
output.seek(0)
|
||||
|
||||
html_table = df.to_html(index=False, border=1)
|
||||
|
||||
msg = Message(
|
||||
subject="Loan Report Summary",
|
||||
recipients=recipients,
|
||||
html=f"<h3>Loan Report Summary</h3>{html_table}",
|
||||
)
|
||||
msg.attach(
|
||||
"loan_report.xlsx",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
output.read()
|
||||
)
|
||||
|
||||
with current_app.app_context():
|
||||
mail.send(msg)
|
||||
|
||||
return "Report email sent"
|
||||
@@ -40,3 +40,6 @@ confluent-kafka==1.9.2
|
||||
|
||||
python-dateutil
|
||||
|
||||
Flask-Mail==0.10.0
|
||||
pandas==2.1.3
|
||||
openpyxl==3.1.5
|
||||
Reference in New Issue
Block a user