Beispiel #1
0
def send_an_email(subject: str, message: str, recipients: list) -> bool:
    """
    Using the imported Gmail module, this function sends an email to the provided
    recipients with the provided subject and message.
    :param subject: Subject of the email.
    :param message: HTML string containing the message of the email.
    :param recipients: List of email addresses.
    :return: Boolean where True means the email was sent successfully, and False
        means the email failed to send.
    """
    # Attempt to configure the gmail object.
    try:
        gmail = Gmail(GMAIL_USERNAME, GMAIL_PASSWORD)
        gmail.set_recipients(recipients)
        gmail.set_subject(subject)
        gmail.add_html(message)
    except Exception as email_config_exception:
        LOGGER.error(
            f"Error configuring the gmail object: {email_config_exception}")
        return False
    # Attempt to send the email.
    try:
        gmail.send_email()
        LOGGER.info("Email successfully sent.")
    except Exception as email_exception:
        LOGGER.error(f"Error sending the email: {email_exception}")
        return False
    return True