Skip to content

Commit

Permalink
Fix image attachment (#188)
Browse files Browse the repository at this point in the history
* Fix image attachment

* Fix code analysis

* Update change log
  • Loading branch information
krissik authored and djay committed Sep 27, 2019
1 parent 6328fa1 commit b3b6f12
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ issues if those validators, or default values, were misconfigured in the first p
- Put in tests to show recaptcha validation prevents submissions
[djay]

- Fix UnicodeDecodeError while attaching an image to a mail #187
[krissik]

2.1.0 (2019-04-25)
------------------

Expand Down
4 changes: 2 additions & 2 deletions src/collective/easyform/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,14 @@ def get_mail_text(self, fields, request, context):
ctype = attachment[1]
# encoding = attachment[2]
content = attachment[3]
if not six.PY2 and isinstance(content, six.binary_type):
content = content.decode("utf-8")
if ctype is None:
ctype = "application/octet-stream"

maintype, subtype = ctype.split("/", 1)

if maintype == "text":
if not six.PY2 and isinstance(content, six.binary_type):
content = content.decode("utf-8")
msg = MIMEText(content, _subtype=subtype)
elif maintype == "image":
msg = MIMEImage(content, _subtype=subtype)
Expand Down
Binary file added src/collective/easyform/tests/PloneLogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions src/collective/easyform/tests/attachment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Submit the form with an image attachment::
>>> browser.getControl('Your E-Mail Address').value = 'test@example.com'
>>> browser.getControl('Subject').value = 'test'
>>> browser.getControl('Comments').value = 'PFG rocks!'
>>> browser.getControl(name='form.widgets.attachment').add_file(BytesIO(b'image content'), 'image/gif', 'test.gif')
>>> browser.getControl(name='form.widgets.attachment').add_file(open(get_image_path(), 'rb'), 'image/png', 'test.png')
>>> browser.getControl('Submit').click()
<sent mail from ...to ['mdummy@address.com']>
>>> 'Thanks for your input.' in browser.contents
Expand All @@ -84,8 +84,8 @@ Submit the form with an image attachment::
Make sure the attachment was included in the email message::


>>> portal.MailHost.msg.get_payload()[1].get_payload(decode=True)
b'image content'
>>> b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xb4\x00\x00\x00/\x08\x06\x00\x00\x00Jl\xe0\xb2\x00\x00\x00\x06bKGD\x00\xff\x00\xff\x00\xff\xa0\xbd\xa7\x93\x00\x00\x10\xa8IDATx\x9c\xed\x9d{xT\xd5\xb5\xc0\x7f\xeb\x9c\t\t \xf8\xa0BQ\xd0\x86IxH}]\xad\xb6^\xad\x8f\xa2\xb4\x96\xaaU\xc1\x07>\x9a\x07\xc6\x8b\x8fj\xd5\xab\xb6\xda\xc6\xf7\xf5Q\xfba\xc5\x162\x93\x88\xd7\xf6r\xa3\xe2\x93\xab\xf7\x93[D[\xabT' in portal.MailHost.msg.get_payload()[1].get_payload(decode=True)
True

Submit the form with an audio attachment::

Expand Down Expand Up @@ -163,7 +163,7 @@ Check saved data::
>>> browser.getLink('Saver').click()
>>> "5 input(s) saved" in browser.contents
True
>>> ".widgets.attachment/@@download/test.gif" in browser.contents
>>> ".widgets.attachment/@@download/test.png" in browser.contents
True
>>> ".widgets.attachment/@@download/test.mp3" in browser.contents
True
Expand Down
9 changes: 8 additions & 1 deletion src/collective/easyform/tests/testDocTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import six
import transaction
import unittest
import os


optionflags = (
Expand Down Expand Up @@ -48,6 +49,11 @@ def get_browser(layer, auth=True):
return browser


def get_image_path():
dir_name = os.path.dirname(os.path.realpath(__file__))
return '{0}/PloneLogo.png'.format(dir_name)


def test_suite():
suite = unittest.TestSuite()
suite.addTests(
Expand All @@ -56,7 +62,8 @@ def test_suite():
doctest.DocFileSuite(
f,
optionflags=optionflags,
globs={"get_browser": get_browser},
globs={"get_browser": get_browser,
"get_image_path": get_image_path},
checker=Py23DocChecker(),
),
layer=FUNCTIONAL_TESTING,
Expand Down

0 comments on commit b3b6f12

Please sign in to comment.