forked from DigiFi/digifi-BankToProductCore
[add]: mail
This commit is contained in:
+5
-1
@@ -1,4 +1,5 @@
|
|||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
from flask_mail import Mail
|
||||||
import os
|
import os
|
||||||
from flask_swagger_ui import get_swaggerui_blueprint
|
from flask_swagger_ui import get_swaggerui_blueprint
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
@@ -7,7 +8,7 @@ from app.api.routes import api
|
|||||||
from app.errors import register_error_handlers
|
from app.errors import register_error_handlers
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_migrate import Migrate
|
from flask_migrate import Migrate
|
||||||
from app.extensions import db, migrate
|
from app.extensions import db, migrate, mail
|
||||||
from flask_jwt_extended import (
|
from flask_jwt_extended import (
|
||||||
JWTManager,
|
JWTManager,
|
||||||
jwt_required,
|
jwt_required,
|
||||||
@@ -47,6 +48,9 @@ def create_app():
|
|||||||
|
|
||||||
from . import models
|
from . import models
|
||||||
|
|
||||||
|
# Initialize Flask-Mail
|
||||||
|
mail.init_app(app)
|
||||||
|
|
||||||
# Database and Migrations
|
# Database and Migrations
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
|
|
||||||
|
|||||||
@@ -85,6 +85,14 @@ class Config:
|
|||||||
"salarypaymenT_6"
|
"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')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_migrate import Migrate
|
from flask_migrate import Migrate
|
||||||
|
from flask_mail import Mail
|
||||||
|
|
||||||
|
mail = Mail()
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
migrate = Migrate()
|
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
|
python-dateutil
|
||||||
|
|
||||||
|
Flask-Mail==0.10.0
|
||||||
|
pandas==2.1.3
|
||||||
|
openpyxl==3.1.5
|
||||||
@@ -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)
|
||||||
Reference in New Issue
Block a user