Browse Source

Support testing email based on values in UI instead of stored config.

pull/148/head
Jostein Kjønigsen 11 years ago
parent
commit
db4ecfc59c
  1. 4
      .gitignore
  2. 16
      interfaces/Config/templates/_inc_header_uc.tmpl
  3. 6
      interfaces/Config/templates/config_notify.tmpl
  4. 3
      sabnzbd/api.py
  5. 47
      sabnzbd/emailer.py

4
.gitignore

@ -26,3 +26,7 @@ SABnzbd*.dmg
*.keep
*.bak
*.log
# Some people use Emacs as an editor
\#*
.\#*

16
interfaces/Config/templates/_inc_header_uc.tmpl

@ -99,6 +99,22 @@
});
});
/*
* takes the inputs-elements found in the current selector
* and extracts the values into the provided data-object.
*/
\$.fn.extractFormDataTo = function(target)
{
var inputs = \$("input[type != 'submit'][type != 'button']", this);
// could use .serializeArray() but that omits unchecked items
inputs.each(function (i,elem) {
target[elem.name] = elem.value;
});
return this;
}
</script>

6
interfaces/Config/templates/config_notify.tmpl

@ -40,7 +40,7 @@
</fieldset>
</div><!-- /col1 -->
</div><!-- /section -->
<div class="section">
<div class="section" id="email">
<div class="col2">
<h3>$T('emailAccount')</h3>
</div><!-- /col2 -->
@ -139,10 +139,12 @@
\$('#email_dir').fileBrowser({ title: 'Select $T('opt-email_dir')' });
\$('#test_email').click(function () {
if (confirm(\$('#test_email').attr('rel'))) {
var data = { mode: 'test_email', apikey: '$session', output: 'json' };
\$("#email").extractFormDataTo(data);
\$.ajax({
type: "GET",
url: "../../tapi",
data: {mode: 'test_email', apikey: '$session', output: 'json' },
data: data,
beforeSend: function () {
\$('#test_email').attr("disabled", "disabled");
\$('#testmail-result').removeClass("success failure").addClass("loading").html('$T('post-Verifying')');

3
sabnzbd/api.py

@ -682,7 +682,8 @@ def _api_test_email(name, output, kwargs):
pack['unpack'] = ['action 1', 'action 2']
res = sabnzbd.emailer.endjob('I had a d\xe8ja vu', 123, 'unknown', True,
os.path.normpath(os.path.join(cfg.complete_dir.get_path(), '/unknown/I had a d\xe8ja vu')),
123*MEBI, None, pack, 'my_script', 'Line 1\nLine 2\nLine 3\nd\xe8ja vu\n', 0)
123*MEBI, None, pack, 'my_script', 'Line 1\nLine 2\nLine 3\nd\xe8ja vu\n', 0,
test=kwargs)
if res == 'Email succeeded':
res = None
return report(output, error=res)

47
sabnzbd/emailer.py

@ -37,22 +37,40 @@ def errormsg(msg):
logging.error(latin1(msg))
return msg
################################################################################
# EMAIL_SEND
#
#
################################################################################
def send(message, recipient):
def send(message, email_to, test=None):
""" Send message if message non-empty and email-parms are set """
# we should not use CFG if we are testing. we should use values
# from UI instead.
if test:
email_server = test.get('email_server')
email_from = test.get('email_from')
email_account = test.get('email_account')
email_pwd = test.get('email_pwd')
else:
email_server = cfg.email_server()
email_from = cfg.email_from()
email_account = cfg.email_account()
email_pwd = cfg.email_pwd()
# email_to is replaced at send_with_template, since it can be an array
if not message.strip('\n\r\t '):
return "Skipped empty message"
if cfg.email_server() and recipient and cfg.email_from():
if email_server and email_to and email_from:
message = _prepare_message(message)
server, port = split_host(cfg.email_server())
server, port = split_host(email_server)
if not port:
port = 25
@ -92,14 +110,14 @@ def send(message, recipient):
return errormsg(T('Failed to initiate TLS connection'))
# Authentication
if (cfg.email_account() != "") and (cfg.email_pwd() != ""):
if (email_account != "") and (email_pwd != ""):
try:
mailconn.login(cfg.email_account(), cfg.email_pwd())
mailconn.login(email_account, email_pwd)
except:
return errormsg(T('Failed to authenticate to mail server'))
try:
mailconn.sendmail(cfg.email_from(), recipient, message)
mailconn.sendmail(email_from, email_to, message)
msg = None
except smtplib.SMTPHeloError:
msg = errormsg('The server didn\'t reply properly to the helo greeting.')
@ -139,7 +157,7 @@ def get_email_date():
################################################################################
from Cheetah.Template import Template
def send_with_template(prefix, parm):
def send_with_template(prefix, parm, test=None):
""" Send an email using template """
parm['from'] = cfg.email_from()
@ -166,15 +184,20 @@ def send_with_template(prefix, parm):
source = _decode_file(temp)
if source:
sent = True
if len(cfg.email_to()):
for recipient in cfg.email_to():
if test:
recipients = [ test.get('email_to') ]
else:
recipients = cfg.email_to()
if len(recipients):
for recipient in recipients:
parm['to'] = recipient
message = Template(source=source,
searchList=[parm],
filter=EmailFilter,
compilerSettings={'directiveStartToken': '<!--#',
'directiveEndToken': '#-->'})
ret = send(message.respond(), recipient)
ret = send(message.respond(), recipient, test)
del message
else:
ret = T('No recipients given, no email sent')
@ -187,7 +210,7 @@ def send_with_template(prefix, parm):
return ret
def endjob(filename, msgid, cat, status, path, bytes, fail_msg, stages, script, script_output, script_ret):
def endjob(filename, msgid, cat, status, path, bytes, fail_msg, stages, script, script_output, script_ret, test=None):
""" Send end-of-job email """
# Translate the stage names
@ -219,7 +242,7 @@ def endjob(filename, msgid, cat, status, path, bytes, fail_msg, stages, script,
parm['size'] = "%sB" % to_units(bytes)
parm['end_time'] = time.strftime(time_format('%Y-%m-%d %H:%M:%S'), time.localtime(time.time()))
return send_with_template('email', parm)
return send_with_template('email', parm, test)
def rss_mail(feed, jobs):

Loading…
Cancel
Save