diff --git a/mistral/actions/std_actions.py b/mistral/actions/std_actions.py index 55af6666..ba60655d 100644 --- a/mistral/actions/std_actions.py +++ b/mistral/actions/std_actions.py @@ -15,18 +15,19 @@ # See the License for the specific language governing permissions and # limitations under the License. +from email.header import Header from email.mime import text + import json import requests import smtplib -from oslo_log import log as logging - from mistral.actions import base from mistral import exceptions as exc from mistral.utils import javascript from mistral.utils import ssh_utils from mistral.workflow import utils as wf_utils +from oslo_log import log as logging LOG = logging.getLogger(__name__) @@ -286,9 +287,8 @@ class SendEmailAction(base.Action): (self.sender, self.to, self.subject, self.smtp_server, self.body[:128])) - # TODO(dzimine): handle utf-8, http://stackoverflow.com/a/14506784 - message = text.MIMEText(self.body) - message['Subject'] = self.subject + message = text.MIMEText(self.body, _charset='utf-8') + message['Subject'] = Header(self.subject, 'utf-8') message['From'] = self.sender message['To'] = self.to diff --git a/mistral/tests/unit/actions/test_std_email_action.py b/mistral/tests/unit/actions/test_std_email_action.py index 9f1554a6..ca1a1854 100644 --- a/mistral/tests/unit/actions/test_std_email_action.py +++ b/mistral/tests/unit/actions/test_std_email_action.py @@ -14,8 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import base64 +from email.header import decode_header from email import parser import mock +import six import testtools from mistral.actions import std_actions as std @@ -103,8 +106,26 @@ class SendEmailActionTest(base.BaseTest): self.assertEqual(self.from_addr, message['from']) self.assertEqual(self.to_addrs_str, message['to']) - self.assertEqual(self.subject, message['subject']) - self.assertEqual(self.body, message.get_payload()) + if six.PY3: + self.assertEqual( + self.subject, + decode_header(message['subject'])[0][0].decode('utf-8') + ) + else: + self.assertEqual( + self.subject.decode('utf-8'), + decode_header(message['subject'])[0][0].decode('utf-8') + ) + if six.PY3: + self.assertEqual( + self.body, + base64.b64decode(message.get_payload()).decode('utf-8') + ) + else: + self.assertEqual( + self.body.decode('utf-8'), + base64.b64decode(message.get_payload()).decode('utf-8') + ) @mock.patch('smtplib.SMTP') def test_with_password(self, smtp):