示例#1
0
def send():
    """
    sends emails to a configured MailChimp list
    """

    # get form data (WE SHOULD VALIDATE)
    msg_text = request.form.get('message')
    subject = request.form.get('subject')
    from_email = request.form.get('from_email')
    from_name = request.form.get('from_name')
    test_list = request.form.get('test_list', None)

    if (test_list):
        list_id = app.config['PM_TEST_LIST_ID']
        app.logger.debug("sending to test list %s" % (list_id))
    else:
        list_id = app.config['PM_LIST_ID']
        app.logger.debug("sending to LIVE list %s" % (list_id))

    pm = PostMonkey(app.config['PM_API_KEY'])
    md = Mandrill(app.config['MD_API_KEY'])

    members = pm.listMembers(id=list_id, limit=1000)
    emails = []

    for mem in members['data']:
        emails.append(mem['email'])

    eastern = pytz.timezone('US/Eastern')
    now = eastern.localize(datetime.datetime.now())
    msg_text = "%s\n\n--------\n\nE-mail generated at %s" % (msg_text, now)

    for email in emails:
        message = {
            "text": msg_text,
            "subject": subject,
            "from_email": from_email,
            "from_name": from_name,
            "to": [{
                "email": email
            }]
        }

        app.logger.debug("sending to %s at %s" % (email, now))

        resp = md.messages.send(message, async=True)

        app.logger.debug(pformat(resp))

    flash("%s e-mails sent!" % len(emails))

    return redirect(url_for('home'))
def send():
    """
    sends emails to a configured MailChimp list
    """

    # get form data (WE SHOULD VALIDATE)
    msg_text = request.form.get('message')
    subject = request.form.get('subject')
    from_email = request.form.get('from_email')
    from_name = request.form.get('from_name')

    pm = PostMonkey(app.config['PM_API_KEY'])
    md = Mandrill(app.config['MD_API_KEY'])

    members = pm.listMembers(id=app.config['PM_LIST_ID'], limit=1000)
    emails = []

    for mem in members['data']:
        emails.append(mem['email'])

    now_utc = datetime.datetime.now()         # get naive utc time
    local_tz = pytz.timezone('US/Eastern')    # set local timezone
    now_utc = pytz.utc.localize(now_utc)      # add timezone data to utc time
    local_time = now_utc.astimezone(local_tz) # convert to local time
    msg_text = "%s\n\n--------\n\nE-mail generated at %s" % (msg_text, local_time)
    

    for email in emails:
        message = {
            "text": msg_text,
            "subject": subject,
            "from_email": from_email,
            "from_name": from_name,
            "to": [{
                "email": email
            }]
        }

        app.logger.debug("sending to %s" % (email))

        resp = md.messages.send(message, async=True)

        app.logger.debug(pformat(resp))

    flash("%s e-mails sent!" % len(emails))

    return redirect(url_for('home'))