Skip to content
Stephen Brennan edited this page Mar 3, 2017 · 1 revision

How to preview emails

In development, you can't send emails. The sendgrid backend requires an API token and will fail without it. The appengine backend checks whether you're running a development server and won't send. So how can you preview emails? One way is to create a web view that will return the email html, like this:

@app.route('/viewemail/love', methods=['GET'])
@user_required
def view_love():
    # holy hack, batman!
    old_backend = config.EMAIL_BACKEND
    config.EMAIL_BACKEND = 'dummy'
    import logic.email
    import logic.love
    im_a_list = []
    logic.email.EMAIL_BACKENDS['dummy'] = lambda _, __, ___, h, ____: im_a_list.append(h)
    current = Employee.get_current_employee()
    l = logic.love.get_love(sender_username=current.username, limit=1).get_result()[0]
    logic.love.send_love_email(l)
    config.EMAIL_BACKEND = old_backend
    return im_a_list[0]

Yeah, that's a major hack.

Clone this wiki locally