Example #1
0
def send_email(recipient_email,
               email_template,
               email_params,
               email_subject,
               non_html_message,
               sender_email=None):
    if not sender_email:
        if Settings.get_by_name("APP_EMAIL"):
            sender_email = Settings.get_by_name(
                "APP_EMAIL").value  # reads from settings
        else:
            sender_email = "*****@*****.**"

    # send web app URL data by default to email template
    email_params["app_root_url"] = request.url_root

    # render the email HTML body
    email_body = render_template(email_template, **email_params)

    # params sent to the background task
    payload = {
        "recipient_email": recipient_email,
        "email_subject": email_subject,
        "sender_email": sender_email,
        "email_body": email_body,
        "non_html_message": non_html_message
    }

    run_background_task(
        relative_path=url_for("tasks.send_email_task.send_email_via_sendgrid"),
        payload=payload,
        queue="email",
        project=os.environ.get("GOOGLE_CLOUD_PROJECT"),
        location="europe-west1")
Example #2
0
def is_local():
    setting = Settings.get_by_name("PROD_ENV")

    if not setting:
        return True

    if setting.value:
        return False
    else:
        return True
Example #3
0
def send_email_via_sendgrid():
    """A background task that sends an email via SendGrid."""
    data = json.loads(request.get_data(as_text=True))

    recipient_email = data.get("recipient_email")
    sender_email = data.get("sender_email")
    email_subject = data.get("email_subject")
    email_body = data.get("email_body")
    non_html_message = data.get("non_html_message")

    if is_local():
        # localhost (not really sending the email)

        logging.warning("***********************")
        logging.warning(
            "You are on localhost, so no e-mail will be sent. This is message:"
        )
        logging.warning("Recipient: " + recipient_email)
        logging.warning("Sender: " + sender_email)
        logging.warning("Subject: " + email_subject)
        logging.warning("Body: " + non_html_message)
        logging.warning("+++++++++++++++++++++++")

        return "{sender_email} {email_subject}".format(
            sender_email=sender_email, email_subject=email_subject)
    else:
        # production (sending the email via SendGrid)
        if request.headers.get("X-AppEngine-QueueName"):
            # If the request has this header (X-AppEngine-QueueName), then it really came from Google Cloud Tasks.
            # Third-party requests that contain headers started with X are stripped of these headers once they hit GAE
            # servers. That's why no one can fake these headers.

            # SendGrid setup
            sg_api_key = Settings.get_by_name("SendGrid-Mail")
            sg = SendGridAPIClient(api_key=sg_api_key.value)

            # Set up email message
            email_message = Mail(from_email=sender_email,
                                 to_emails=recipient_email,
                                 subject=email_subject,
                                 html_content=email_body)

            try:
                response = sg.send(email_message)
                logging.info(response.status_code)
                logging.info(response.body)
                logging.info(response.headers)
            except Exception as e:
                logging.error(str(e))

        return "true"