Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mail panel #99

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/panels.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,10 @@ Profiler
Reports profiling data for the current request. Due to the performance overhead, profiling is disabled by default. Click the checkmark to toggle profiling on or off. After enabling the profiler, refresh the page to re-run it with profiling.

.. image:: _static/screenshot-profiler-panel.png

Mail
--------

flask_debugtoolbar.panels.mail.MailDebugPanel

Shows mails send with flask_mail.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's some slightly improved docs...

Shows mails that have been sent with `Flask-Mail`_. Use MAIL_SUPPRESS_SEND=True so the mail is not sent.

.. _Flask-Mail: https://pythonhosted.org/Flask-Mail/

1 change: 1 addition & 0 deletions flask_debugtoolbar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def _default_config(self, app):
'flask_debugtoolbar.panels.logger.LoggingPanel',
'flask_debugtoolbar.panels.route_list.RouteListDebugPanel',
'flask_debugtoolbar.panels.profiler.ProfilerDebugPanel',
'flask_debugtoolbar.panels.mail.MailDebugPanel',
),
}

Expand Down
78 changes: 78 additions & 0 deletions flask_debugtoolbar/panels/mail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from flask import g
from flask_debugtoolbar.panels import DebugPanel
from flask_debugtoolbar import module
import jinja2

mails = {}
last_index = 0

def _add_mail(message, app):
global mails, last_index
mails.update({last_index: message})
last_index += 1

try:
import flask_mail
flask_mail.email_dispatched.connect(_add_mail)
except:
pass

@module.route('/mail/remove/<int:key>', methods=['GET', 'POST'])
def remove_mail(key=None):
global mails
del mails[key]
return "ok"

@module.route('/mail/remove/all', methods=['GET', 'POST'])
def remove_all_mails():
global mails
mails = {}
return "ok"


class MailDebugPanel(DebugPanel):
"""
Panel that displays the mails send from flask_mail.
"""
name = 'Mail'

try: # if resource module not available, don't show content panel
flask_mail
except NameError:
has_content = False
has_resource = False
else:
has_content = True
has_resource = True

def process_request(self, request):
pass

def process_response(self, request, response):
pass

def nav_title(self):
return 'Mails'

def nav_subtitle(self):
if len(mails) == 1:
return '1 Mail'
else:
return '%d Mails' % (len(mails))

def title(self):
return 'Mails'

def url(self):
return ''

def content(self):
global mails
context = self.context.copy()
context.update({
'mails': mails,
})

return self.render('panels/mail.html', context)


118 changes: 118 additions & 0 deletions flask_debugtoolbar/templates/panels/mail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<a href="#" onclick="return removeAll()">remove all</a>

{% for key, mail in mails.items() %}
<div class="mail" id="mail-{{ key }}">

<a href="#" onclick="return removeMail( {{ key }} )">remove</a>

<table>
<thead>
<tr>
<th>Variable</th>
<th>Value</th>
</tr>
</thead>
<tbody>

<tr class="flDebugOdd">
<td>subject</td>
<td>{{ mail.subject }}</td>
</tr>

<tr class="flDebugEven">
<td>recipients</td>
<td>{{ mail.recipients }}</td>
</tr>

<tr class="flDebugOdd">
<td>body</td>
<td>{{ mail.body }}</td>
</tr>

<tr class="flDebugEven">
<td>html</td>
<td>{{ mail.html | safe }}</td>
</tr>

<tr class="flDebugOdd">
<td>sender</td>
<td>{{ mail.sender }}</td>
</tr>

<tr class="flDebugEven">
<td>cc</td>
<td>{{ mail.cc }}</td>
</tr>

<tr class="flDebugOdd">
<td>bcc</td>
<td>{{ mail.bcc }}</td>
</tr>

<tr class="flDebugEven">
<td>attachments</td>
<td>{{ mail.attachments }}</td>
</tr>

<tr class="flDebugOdd">
<td>reply_to</td>
<td>{{ mail.reply_to }}</td>
</tr>

<tr class="flDebugEven">
<td>date</td>
<td>{{ mail.date }}</td>
</tr>

<tr class="flDebugEven">
<td>charset</td>
<td>{{ mail.charset }}</td>
</tr>

<tr class="flDebugEven">
<td>extra_headers</td>
<td>{{ mail.extra_headers }}</td>
</tr>

<tr class="flDebugEven">
<td>mail_options</td>
<td>{{ mail.mail_options }}</td>
</tr>

<tr class="flDebugEven">
<td>rcpt_options</td>
<td>{{ mail.rcpt_options }}</td>
</tr>

</tbody>
</table>
</div>
{% endfor %}
<script>
function removeMail(key) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var d = document.getElementById("mail-"+key);
d.parentElement.removeChild(d);
}
};
xhttp.open("GET", "/_debug_toolbar/views/mail/remove/"+key, true);
xhttp.send();
return false;
}
function removeAll() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var d = document.getElementsByClassName("mail");
while (d.length > 0) {
d[0].parentElement.removeChild(d[0]);
}
}
};
xhttp.open("GET", "/_debug_toolbar/views/mail/remove/all", true);
xhttp.send();
return false;
}
</script>