使用Mailgun发送带附件和内嵌图像的电子邮件时出现编解码器错误

时间:2015-05-24 00:53:47

标签: mailgun

更新:我已经确定此代码适用于python2.7,但不适用于python 3.4。有什么建议吗?

我试图使用Mailgun的API发送多个图像。由于我发送的电子邮件数量因电子邮件而异,因此我必须创建一个动态列表,而不是可以在文件参数中传递。

这是我的代码:

  list_of_files = []
  count = 0
  for attachment in attachments:
    count += 1
    #tup = ("attachment" %(count), open(attachment))
    tup = ("attachment", open(attachment))

    list_of_files.append(tup)

  for inline in inlines:
    count += 1
    #tup = ("attachment" %(count), open(inline))
    tup = ("inline", open(attachment))

    list_of_files.append(tup)
  list_of_files = MultiDict(list_of_files)
  #sys.exit()
  print(list_of_files)

  r =  requests.post(
                      keys.mailgun_login,
                      auth=("api", keys.mailgun_API_key),
                      files = list_of_files,
                      #files = [('attachment', open('/Users/krishnaregmi/workdir/project12/media/BFSTPFQPBPSVUQY/pic_2.jpg'))],
                      data = {
                              "from": from_email,
                              "to": to_email,
                              "subject": subject,
                              "text" : body_plain,
                              "html" : body_html,
                              }
                      )
  return r

然而,我一直在犯错误,我无法找出根本原因。这是堆栈跟踪:

关于如何解决这个问题的任何想法?

MultiDict([('attachment', <_io.TextIOWrapper name='/Users/krishnaregmi/workdir/project12/media/BFSTPFQPBPSVUQY/pic_2.jpg' mode='r' encoding='UTF-8'>), ('attachment', <_io.TextIOWrapper name='/Users/krishnaregmi/workdir/project12/media/BFSTPFQPBPSVUQY/pic_2.jpg' mode='r' encoding='UTF-8'>)])
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/krishnaregmi/workdir/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/krishnaregmi/workdir/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/krishnaregmi/workdir/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 190, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Users/krishnaregmi/workdir/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 40, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/Users/krishnaregmi/workdir/project12/continuous_loop/management/commands/run_emailsending_loop.py", line 22, in <module>
    status = mh.send_email_from_email_object(email)
  File "/Users/krishnaregmi/workdir/project12/executive_email/mailgun_helpers.py", line 34, in send_email_from_email_object
    body_html,
  File "/Users/krishnaregmi/workdir/project12/executive_email/mailgun_helpers.py", line 90, in send_email_files
    "html" : body_html,
  File "/Users/krishnaregmi/workdir/venv/lib/python3.4/site-packages/requests/api.py", line 109, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/Users/krishnaregmi/workdir/venv/lib/python3.4/site-packages/requests/api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/Users/krishnaregmi/workdir/venv/lib/python3.4/site-packages/requests/sessions.py", line 451, in request
    prep = self.prepare_request(req)
  File "/Users/krishnaregmi/workdir/venv/lib/python3.4/site-packages/requests/sessions.py", line 382, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/Users/krishnaregmi/workdir/venv/lib/python3.4/site-packages/requests/models.py", line 307, in prepare
    self.prepare_body(data, files, json)
  File "/Users/krishnaregmi/workdir/venv/lib/python3.4/site-packages/requests/models.py", line 453, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "/Users/krishnaregmi/workdir/venv/lib/python3.4/site-packages/requests/models.py", line 150, in _encode_files
    fdata = fp.read()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/codecs.py", line 319, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

1 个答案:

答案 0 :(得分:1)

更新

我更改了代码以打开文件:

ListView

解决了这个问题。