88 lines
3.0 KiB
Python
88 lines
3.0 KiB
Python
import datetime, pytz, time
|
|
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Path, Query, Response, status
|
|
from pydantic import BaseModel, EmailStr, Field
|
|
from typing import Dict, List, Optional, Set, Union
|
|
|
|
from app.lib_general import log, logging, common_route_params, Common_Route_Params, send_email
|
|
from app.config import settings
|
|
from app.db_sql import sql_insert, sql_update, sql_insert_or_update, sql_select, sql_delete, get_id_random, redis_lookup_id_random
|
|
|
|
from app.routers.api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
|
|
|
|
from app.methods.data_store_methods import create_update_data_store_obj, get_data_store_rec_list, load_data_store_obj, load_data_store_obj_w_code
|
|
|
|
from app.models.util_email_models import Email_Send_Base
|
|
from app.models.response_models import Resp_Body_Base, mk_resp
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# ### BEGIN ### API Utility: Email ### util_email_send_obj() ###
|
|
# Updated 2023-06-27
|
|
@router.post('/util/email/send', response_model=Resp_Body_Base)
|
|
async def util_email_send_obj(
|
|
email_send_obj: Email_Send_Base,
|
|
|
|
test: Optional[bool] = False,
|
|
|
|
return_obj: bool = True,
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
resp_data = {}
|
|
|
|
from_email = email_send_obj.from_email
|
|
resp_data['from_email'] = from_email
|
|
from_name = email_send_obj.from_name
|
|
|
|
to_email = email_send_obj.to_email
|
|
resp_data['to_email'] = to_email
|
|
to_name = email_send_obj.to_name
|
|
|
|
bcc_email = email_send_obj.bcc_email
|
|
bcc_name = email_send_obj.bcc_name
|
|
|
|
subject = email_send_obj.subject
|
|
resp_data['subject'] = subject[:20]
|
|
|
|
body_html = email_send_obj.body_html
|
|
body_text = email_send_obj.body_text
|
|
|
|
log.info('Trying send_email()...')
|
|
|
|
# NOTE: This all works, but I think it could be done better???
|
|
|
|
if send_email(
|
|
from_email = from_email,
|
|
from_name = from_name,
|
|
to_email = to_email,
|
|
to_name = to_name,
|
|
bcc_email = bcc_email,
|
|
bcc_name = bcc_name,
|
|
subject = subject,
|
|
body_text = body_text,
|
|
body_html = body_html,
|
|
test = test
|
|
):
|
|
status_code = 200
|
|
status_message = f'Email was sent to <{to_email}>.'
|
|
else:
|
|
status_code = 400
|
|
status_message = f'Email failed to send to <{to_email}>.'
|
|
log.info(status_message)
|
|
|
|
if return_obj:
|
|
return mk_resp(data=resp_data, status_code=status_code, response=commons.response, status_message=status_message)
|
|
else:
|
|
if status_code == 200:
|
|
resp_data = True
|
|
else:
|
|
resp_data = False
|
|
|
|
return mk_resp(data=resp_data, status_code=status_code, response=commons.response, status_message=status_message)
|
|
# ### END ### API Utility: Email ### util_email_send_obj() ###
|