Now with the an email send endpoint!
This commit is contained in:
@@ -412,6 +412,7 @@ def send_email(
|
||||
to_email: str,
|
||||
subject: str,
|
||||
body_html: str,
|
||||
|
||||
from_name: str = '',
|
||||
reply_to_email: str = '',
|
||||
reply_to_name: str = '',
|
||||
@@ -421,6 +422,8 @@ def send_email(
|
||||
bcc_email: str = '',
|
||||
bcc_name: str = '',
|
||||
body_text: str = '',
|
||||
|
||||
test: bool = False
|
||||
):
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
@@ -430,21 +433,76 @@ def send_email(
|
||||
message['Subject'] = subject
|
||||
else:
|
||||
return False
|
||||
if from_email:
|
||||
|
||||
if from_email and from_name:
|
||||
#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)
|
||||
try:
|
||||
message['From'] = Address(display_name=from_name, addr_spec=from_email)
|
||||
except Exception as e:
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
return False
|
||||
elif from_email:
|
||||
try:
|
||||
message['From'] = Address(display_name=from_email, addr_spec=from_email)
|
||||
except Exception as e:
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
return False
|
||||
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)
|
||||
|
||||
if reply_to_email and reply_to_name:
|
||||
try:
|
||||
message['Reply-To'] = Address(display_name=reply_to_name, addr_spec=reply_to_email)
|
||||
except Exception as e:
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
return False
|
||||
elif reply_to_email:
|
||||
try:
|
||||
message['Reply-To'] = Address(display_name=reply_to_email, addr_spec=reply_to_email)
|
||||
except Exception as e:
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
return False
|
||||
|
||||
if to_email and to_name:
|
||||
try:
|
||||
message['To'] = Address(display_name=to_name, addr_spec=to_email)
|
||||
except Exception as e:
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
return False
|
||||
elif to_email:
|
||||
try:
|
||||
message['To'] = Address(display_name=to_email, addr_spec=to_email)
|
||||
except Exception as e:
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
return False
|
||||
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)
|
||||
|
||||
if cc_email and cc_name:
|
||||
try:
|
||||
message['Cc'] = Address(display_name=cc_name, addr_spec=cc_email)
|
||||
except Exception as e:
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
return False
|
||||
elif cc_email:
|
||||
try:
|
||||
message['Cc'] = Address(display_name=cc_email, addr_spec=cc_email)
|
||||
except Exception as e:
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
return False
|
||||
|
||||
if bcc_email and bcc_name:
|
||||
try:
|
||||
message['Bcc'] = Address(display_name=bcc_name, addr_spec=bcc_email)
|
||||
except Exception as e:
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
return False
|
||||
elif bcc_email:
|
||||
try:
|
||||
message['Bcc'] = Address(display_name=bcc_email, addr_spec=bcc_email)
|
||||
except Exception as e:
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
return False
|
||||
|
||||
html_version = """\
|
||||
<html>
|
||||
@@ -479,6 +537,21 @@ def send_email(
|
||||
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.info('SMTP configuration, connect, and send')
|
||||
|
||||
if test:
|
||||
try:
|
||||
with smtplib.SMTP_SSL(settings.SMTP['server'], settings.SMTP['port'], context=context) as server:
|
||||
log.debug('[TEST] SMTP log in...')
|
||||
server.login(settings.SMTP['username'], settings.SMTP['password'])
|
||||
log.debug('[TEST] SMTP send message... [WILL NOT SEND IN TEST MODE]')
|
||||
# server.send_message(message)
|
||||
log.info('[TEST] Email sent!')
|
||||
return True
|
||||
except:
|
||||
#except SMTPException:
|
||||
log.error('[TEST] Error: unable to send email')
|
||||
return False
|
||||
|
||||
try:
|
||||
with smtplib.SMTP_SSL(settings.SMTP['server'], settings.SMTP['port'], context=context) as server:
|
||||
log.debug('SMTP log in...')
|
||||
|
||||
Reference in New Issue
Block a user