Commit af7a2ed9 authored by Hamaker, Alec's avatar Hamaker, Alec
Browse files

Merge branch 'feature/email-specify-recips' into 'develop'

Feature/email specify recips

See merge request nset-utilities/common-package!18
parents a4457c2d 96b493e3
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -9,4 +9,5 @@ COPY src/ tmp/src
RUN set -eux && pip install --upgrade pip && \
      cd /tmp/src/ && pip install -e . && \
      pip install pytest pytest-cov 
      
CMD ['sh', 'tail', '-f', '/dev/null']
+1 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ services:
    #command: ["sh", "-c", "pytest --cov=common /test/test.py"]
    #command: ["sh", "-c", "test-db-conn && pytest -v --cov=common /test/test.py"]
    command: ["python3", "/test/test.py"]
    #command: tail -f /dev/null
    depends_on:
      - postgres

+15 −8
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ from common.env import check_environment as ce
from common.logz import create_logger


def send_email(subject, text, files=None):
def send_email(subject, text, files=None, recipients=None):
    """ Send email to the EMAIL_RECIPIENTS env variable with the given subject
        and message body from the EMAIL_SENDER address. Email debug level is
        controlled with the EMAIL_DEBUG_LEVEL environmental variable and
@@ -27,17 +27,25 @@ def send_email(subject, text, files=None):
    relay_address = ce('RELAY_ADDRESS')
    relay_port = ce('RELAY_PORT')
    relay_password = ce('RELAY_PASSWORD')
    relay_user = ce('RELAY_USER')
    smtp_port = ce('SMTP_PORT', 25)
    if from_address is None:
        logger.critical('Unable to send email as no EMAIL_SENDER set')
        return None
    
    try:
        if recipients is None:
            to_address = ce('EMAIL_RECIPIENTS')
        else:
            to_address = recipients
        if ',' in to_address:
            to_address = ', '.join(to_address.split(','))
        logger.info(f"{to_address}")
    except AttributeError:
            logger.critical('No EMAIL_RECIPIENTS set.')
            return None


    msg = MIMEMultipart()
    msg['From'] = from_address
    msg['To'] = to_address
@@ -66,8 +74,7 @@ def send_email(subject, text, files=None):
            logger.error(f'Failed to attach file {files}. {files} is not a '
                         'file.')
    else:
        logger.error(f'Failed to attach files "{files}". Please ensure that '
                     '"files" is passed as type "list"')
        logger.info(f"No files attached: \"{files}\" ")
    logger.info(f'Sending email to {to_address} from {from_address}')
    # try:
    if all(var is None for var in [relay_address, relay_port,
@@ -78,13 +85,13 @@ def send_email(subject, text, files=None):
            server.quit()
        logger.info(f'Email successfully sent to {to_address}.')
    elif all(var is not None for var in [relay_address, relay_port,
                                         relay_address]):
                                         relay_password, relay_user]):
        # allow ssl connection
        context = ssl.create_default_context()
        with smtplib.SMTP(relay_address, relay_port) as conn:
            conn.ehlo()
            conn.starttls(context=context)
            conn.login(from_address, relay_password)
            conn.login(relay_user, relay_password)
            conn.send_message(msg)
            conn.quit()
        logger.info(f'Email successfully sent to {to_address}.')
+1 −1
Original line number Diff line number Diff line
rich==10.11.0
pandas==1.3.3
psycopg2-binary==2.9.1
pymssql==2.2.4
pymssql==2.2.7
+4 −0
Original line number Diff line number Diff line
@@ -292,6 +292,10 @@ class MailTestCase(unittest.TestCase):
                   files=ATTACHMENT_PATHS_W_BAD_FILE)
        send_email("TEST SUBJECT WITH STRING FILE PATH", "TEST MESSAGE",
                   files=ATTACHMENT_PATHS[0])
        send_email("TESTING RECIPIENT FIELD", "TEST RECIPENT FIELD", 
                    recipients='huihuijk@ornl.gov')
        #send_email("TESTING RECIPIENT FIELD MULTI ADDRESS", "TEST RECIPENT FIELD", 
        #            recipients='huihuijk@ornl.gov, jonathanhuihui@yahoo.com, jonathankhuihui@gmail.com')


if __name__ == "__main__":
Loading