Repository: ckan/ckan

Issue:
ValueError when you configure exception emails
When I configure CKAN to send me exception emails, I get this exception:
```
$ paster db upgrade -c /etc/ckan/default/development.ini
2019-05-31 21:27:32,979 DEBUG [ckan.lib.fanstatic_resources] Base path /usr/lib/ckan/default/src/ckan/ckan/public/base
2019-05-31 21:27:33,449 DEBUG [ckanext.harvest.model] Harvest tables defined in memory
2019-05-31 21:27:33,462 DEBUG [ckanext.harvest.model] Harvest tables already exist
2019-05-31 21:27:34,126 DEBUG [ckanext.harvest.model] Harvest tables already exist
Traceback (most recent call last):
  File "/usr/lib/ckan/default/bin/paster", line 11, in <module>
    load_entry_point('PasteScript', 'console_scripts', 'paster')()
  File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/paste/script/command.py", line 102, in run
    invoke(command, command_name, options, args[1:])
  File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/paste/script/command.py", line 141, in invoke
    exit_code = runner.run(args)
  File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/paste/script/command.py", line 236, in run
    result = self.command()
  File "/usr/lib/ckan/default/src/ckan/ckan/lib/cli.py", line 354, in command
    self._load_config(cmd!='upgrade')
  File "/usr/lib/ckan/default/src/ckan/ckan/lib/cli.py", line 331, in _load_config
    self.site_user = load_config(self.options.config, load_site_user)
  File "/usr/lib/ckan/default/src/ckan/ckan/lib/cli.py", line 244, in load_config
    app = make_app(conf.global_conf, **conf.local_conf)
  File "/usr/lib/ckan/default/src/ckan/ckan/config/middleware/__init__.py", line 60, in make_app
    flask_app = make_flask_stack(conf, **app_conf)
  File "/usr/lib/ckan/default/src/ckan/ckan/config/middleware/flask_app.py", line 188, in make_flask_stack
    _register_error_handler(app)
  File "/usr/lib/ckan/default/src/ckan/ckan/config/middleware/flask_app.py", line 463, in _register_error_handler
    _setup_error_mail_handler(app)
  File "/usr/lib/ckan/default/src/ckan/ckan/config/middleware/flask_app.py", line 481, in _setup_error_mail_handler
    subject='Application Error'
  File "/usr/lib/python2.7/logging/handlers.py", line 892, in __init__
    self.mailhost, self.mailport = mailhost
ValueError: need more than 1 value to unpack
```

Reproduce this using config:
```
#debug = false
email_to = errors@example.com
```

The problem is SMTPHandler is expecting a hostname AND port, but the default value doesn't have a port:
```
smtp.server = localhost
```
This was introduced on master: https://github.com/ckan/ckan/pull/4623

### CKAN Version if known (or site URL)

Doesn't affect any CKAN releases. Affects master branch since January 2019.


Discussion:

Resolving patch:
diff --git a/ckan/config/middleware/flask_app.py b/ckan/config/middleware/flask_app.py
--- a/ckan/config/middleware/flask_app.py
+++ b/ckan/config/middleware/flask_app.py
@@ -477,7 +477,9 @@ def filter(self, log_record):
             log_record.headers = request.headers
             return True
 
-    mailhost = tuple(config.get('smtp.server', 'localhost').split(":"))
+    smtp_server = config.get('smtp.server', 'localhost')
+    mailhost = tuple(smtp_server.split(':')) \
+        if ':' in smtp_server else smtp_server
     credentials = None
     if config.get('smtp.user'):
         credentials = (config.get('smtp.user'), config.get('smtp.password'))


Test patch: