# Buggy code
def send_to_kindle(book_id, book_format, convert):
    if not config.get_mail_server_configured():
        flash(_("Please configure the SMTP mail settings first..."), category="error")
    elif current_user.kindle_mail:
        result = send_mail(
            book_id,
            book_format,
            convert,
            current_user.kindle_mail,
            config.config_calibre_dir,
            current_user.nickname,
        )
        if result is None:
            flash(
                _(
                    "Book successfully queued for sending to %(kindlemail)s",
                    kindlemail=current_user.kindle_mail,
                ),
                category="success",
            )
            ub.update_download(book_id, int(current_user.id))
        else:
            flash(
                _("There was an error sending this book: %(res)s", res=result),
                category="error",
            )
    else:
        flash(
            _("Please configure your kindle e-mail address first..."), category="error"
        )
    return redirect(request.environ["HTTP_REFERER"])


# Traceback (KeyError)
Internal Server Error
500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python3.6/dist-packages/flask_login/utils.py", line 261, in decorated_view
return func(*args, **kwargs)
File "/app/calibre-web/cps/web.py", line 217, in inner
return f(*args, **kwargs)
File "/app/calibre-web/cps/web.py", line 1071, in send_to_kindle
return redirect(request.environ["HTTP_REFERER"])
KeyError: 'HTTP_REFERER'

# Fixed code
def send_to_kindle(book_id, book_format, convert):
    if not config.get_mail_server_configured():
        flash(_("Please configure the SMTP mail settings first..."), category="error")
    elif current_user.kindle_mail:
        result = send_mail(
            book_id,
            book_format,
            convert,
            current_user.kindle_mail,
            config.config_calibre_dir,
            current_user.nickname,
        )
        if result is None:
            flash(
                _(
                    "Book successfully queued for sending to %(kindlemail)s",
                    kindlemail=current_user.kindle_mail,
                ),
                category="success",
            )
            ub.update_download(book_id, int(current_user.id))
        else:
            flash(
                _("There was an error sending this book: %(res)s", res=result),
                category="error",
            )
    else:
        flash(
            _("Please configure your kindle e-mail address first..."), category="error"
        )
    if "HTTP_REFERER" in request.environ:
        return redirect(request.environ["HTTP_REFERER"])
    else:
        return redirect(url_for("web.index"))