Added person lookup by email and email auth url key.

This commit is contained in:
Scott Idem
2021-12-02 17:57:01 -05:00
parent 2c0af0a321
commit 630739aae6
7 changed files with 265 additions and 9 deletions

View File

@@ -1,7 +1,15 @@
from __future__ import annotations
import datetime, jwt, os, pandas, pathlib, pytz, redis, time
import datetime, html2text, jwt, os, pandas, pathlib, pytz, redis, time
from passlib.hash import argon2
# Import smtplib for the actual sending function
import smtplib, ssl
# Import the email package modules needed
from email.message import EmailMessage
from email.headerregistry import Address
from email.utils import make_msgid
from fastapi import APIRouter, Depends, Header, HTTPException, Response, status
from pydantic import BaseModel, EmailStr, Field
from typing import Dict, List, Optional, Set, Union
@@ -163,3 +171,90 @@ def create_export_file(
return tmp_file_path # True
# ### END ### API Lib General ### create_export() ###
# ### BEGIN ### API Lib General ### send_email() ###
# Updated 2021-12-02
@logger_reset
def send_email(
from_email:str,
to_email: str,
subject: str,
body_html: str,
from_name: str = '',
reply_to_email: str = '',
reply_to_name: str = '',
to_name: str = '',
cc_email: str = '',
cc_name: str = '',
bcc_email: str = '',
bcc_name: str = '',
body_text: str = '',
):
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
message = EmailMessage()
if subject:
message['Subject'] = subject
else:
return False
if from_email:
#message['From'] = Address(display_name=from_name, username=from_email.split('@')[0], domain=from_email.split('@')[1])
message['From'] = Address(display_name=from_name, addr_spec=from_email)
else:
return False
if reply_to_email:
message['Reply-To'] = Address(display_name=reply_to_name, addr_spec=reply_to_email)
if to_email:
message['To'] = Address(display_name=to_name, addr_spec=to_email)
else:
return False
if cc_email:
message['Cc'] = Address(display_name=cc_name, addr_spec=cc_email)
if bcc_email:
message['Bcc'] = Address(display_name=bcc_name, addr_spec=bcc_email)
html_version = """\
<html>
<body>
<div>
"""+body_html+"""
</div>
</body>
</html>
"""
if body_text:
text_version = body_text
else:
text_version = html2text.html2text(html_version)
message.set_content(text_version)
message.add_alternative(html_version, subtype='html')
log.info('Sending email...')
log.debug(settings.SMTP)
log.info(f'Subject: {subject}')
log.info(f'From: {from_email} Reply To: {reply_to_email} To: {to_email} CC: {cc_email} BCC: {bcc_email}')
log.debug('Message:')
log.debug(message.as_string())
log.info('Creating SMTP SSL connection...')
context = ssl.create_default_context()
log.info('SMTP configuration, connect, and send')
try:
with smtplib.SMTP_SSL(settings.SMTP['server_name'], settings.SMTP['server_port'], context=context) as server:
server.login(settings.SMTP['username'], settings.SMTP['password'])
server.send_message(message)
log.info('Email sent')
return True
except:
#except SMTPException:
log.error('Error: unable to send email')
return False
# ### END ### API Lib General ### send_email() ###