Ejemplo n.º 1
0
def send_email(to, action, subject, html):
    """
    Sends email and records it in DB
    """
    if not string_empty(to):
        key = get_settings()['sendgrid_key']
        if not key and not current_app.config['TESTING']:
            print 'Sendgrid key not defined'
            return

        if not current_app.config['TESTING']:
            headers = {
                "Authorization": ("Bearer " + key)
            }
            payload = {
                'to': to,
                'from': '*****@*****.**',
                'subject': subject,
                'html': html
            }
            from tasks import send_email_task
            send_email_task.delay(payload, headers)
        # record_mail(to, action, subject, html)
        mail = Mail(
            recipient=to, action=action, subject=subject,
            message=html, time=datetime.now()
        )
        from data import save_to_db
        save_to_db(mail, 'Mail Recorded')
    return
Ejemplo n.º 2
0
def approve():
    id = request.args.get("id", type=int)
    food_resource = FoodResource.query.filter_by(id=id).first()
    contact = food_resource.food_resource_contact

    if contact.email:
        send_email_task.delay(
            recipient=contact.email,
            subject=food_resource.name + ' has been approved',
            html_message='Dear ' + contact.name + ', <p>Good news! Your food resource <b>' +
                         food_resource.name + '</b> was added to the map. Thank you!</p><br> ' +
                         'Sincerely,<br>' + app.config['USER_APP_NAME'],
            text_message='Good news! Your food resource ' + food_resource.name +
                         ' was added to the map. Thank you!'
        )

    if len(contact.food_resource) <= 1:
        db.session.delete(contact)
    else:
        contact.food_resource.remove(food_resource)

    food_resource.is_approved = True
    db.session.commit()

    return jsonify(message="success")
Ejemplo n.º 3
0
def send_email(to, action, subject, html):
    """
    Sends email and records it in DB
    """
    if not string_empty(to):
        email_service = get_settings()['email_service']
        email_from = get_settings()['email_from']
        payload = {
            'to': to,
            'from': email_from,
            'subject': subject,
            'html': html
        }

        if not current_app.config['TESTING']:
            if email_service == 'smtp':
                smtp_encryption = get_settings()['smtp_host']

                if smtp_encryption == 'tls':
                    smtp_encryption = 'required'
                elif smtp_encryption == 'ssl':
                    smtp_encryption = 'ssl'
                elif smtp_encryption == 'tls_optional':
                    smtp_encryption = 'optional'
                else:
                    smtp_encryption = 'none'

                config = {
                    'host': get_settings()['smtp_host'],
                    'username': get_settings()['smtp_username'],
                    'password': get_settings()['smtp_password'],
                    'tls': smtp_encryption,
                    'port': get_settings()['smtp_port'],
                }

                from tasks import send_mail_via_smtp_task
                send_mail_via_smtp_task.delay(config, payload)

            else:
                key = get_settings()['sendgrid_key']
                if not key and not current_app.config['TESTING']:
                    print 'Sendgrid key not defined'
                    return
                headers = {
                    "Authorization": ("Bearer " + key)
                }
                from tasks import send_email_task
                send_email_task.delay(payload, headers)

        # record_mail(to, action, subject, html)
        mail = Mail(
            recipient=to, action=action, subject=subject,
            message=html, time=datetime.now()
        )

        from data import save_to_db, record_activity
        save_to_db(mail, 'Mail Recorded')
        record_activity('mail_event', email=to, action=action, subject=subject)
    return
Ejemplo n.º 4
0
def remove():
    id = request.args.get("id", type=int)
    food_resource = FoodResource.query.filter_by(id=id).first()
    if not food_resource:
        return jsonify(message="failed")

    # Determine whether the food resource being removed is approved or pending.
    # Needed for front-end update after food resource is removed.
    is_approved = False
    if food_resource.is_approved:
        is_approved = True

    contact = food_resource.food_resource_contact

    if contact and contact.email:
        send_email_task.delay(
            recipient=contact.email,
            subject=food_resource.name + ' has been rejected',
            html_message='Dear ' + contact.name + ', <p>Your food resource <b>' +
                         food_resource.name + '</b> was rejected. Please contact us at ' +
                         '[email protected] to find out why.\
                         </p><br> Sincerely,<br>' + app.config['USER_APP_NAME'],
            text_message='Your food resource ' + food_resource.name + ' was rejected. Please ' +
                         'contact contact us at [email protected] to find out why.'
        )

    # If the food resource has a contact and its contact has submitted no other
    # food resources to the database, remove him/her from the database.
    if contact and len(contact.food_resource) <= 1:
        db.session.delete(contact)

    # Remove the food resoure and its timeslots, address, and phone numbers
    # from the database.
    for timeslot in food_resource.timeslots:
            db.session.delete(timeslot)
    for phone_number in food_resource.phone_numbers:
        db.session.delete(phone_number)
    db.session.delete(food_resource.address)
    db.session.delete(food_resource)
    db.session.delete(food_resource)
    db.session.commit()

    return jsonify(is_approved=is_approved)