Django电子邮件附件:TypeError:'File'对象不支持索引 [英] Django Email Attachment: TypeError: 'File' object does not support indexing

查看:171
本文介绍了Django电子邮件附件:TypeError:'File'对象不支持索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我可以在本地机器上发送附件,但不能在Heroku上发送附件。我将附件代码更改为:

 如果docfile:
email.attach_file(docfile.name)

这个工作在本地,在shell中,当我运行我的应用程序的本地主机服务器时。






所以我一直在试图让django发送电子邮件,附件是作为文件对象存储在故事模型中的附件。我有一个处理这个的任务:

 从芹菜导入任务
从django.core.mail.message import EmailMessage
from django.core.files.storage import default_storage
from apps.account.models import UserProfile

@task(name ='send-email')
def sendPublished_article(sender,subject,body,attachment = None):
recipient = []
UserProfile.objects.all()中的配置文件:
如果profile.user_type =='Client':
recipient.append(profile.user.email)
email = EmailMessage(subject,body,sender,recipients)
try:
docfile = default_storage.open(attachment.name, 'w +')
如果docfile:
email.attach_file(docfile.name)
其他:
通过
除了:
通过
电子邮件.send()

我一直在试图用shell找到错误,一切似乎都很好直到我到最后一个l ine,并运行email.send()。 shell barfs这个错误跟踪:

 追溯(最近的最后一次呼叫):
文件<控制台> ,第1行在< module>
文件/home/vagrant/nns/local/lib/python2.7/site-packages/django/core/mail/message.py,第248行,发送
return self.get_connection( fail_silently).send_messages([self])
文件/home/vagrant/nns/local/lib/python2.7/site-packages/django/core/mail/backends/console.py,第23行,在send_messages
self.stream.write('%s\\\
'%message.message()。as_string())
文件/home/vagrant/nns/local/lib/python2.7 /site/package/django/core/mail/message.py,第215行,消息
msg = self._create_message(msg)
文件/ home / vagrant / nns / local / lib / python2.7 / site-packages / django / core / mail / message.py,第272行,_create_message
return self._create_attachments(msg)
文件/ home / vagrant / nns / local / lib / python2.7 / site-packages / django / core / mail / message.py,第285行,在_create_attachments
msg.attach(self._create_attachment(* attachment))
文件/ home /vagrant/nns/local/lib/python2.7/site-packages/django/core/mail/message.py,第312行,在_create _attachment
attachment = self._create_mime_attachment(content,mimetype)
文件/home/vagrant/nns/local/lib/python2.7/site-packages/django/core/mail/message.py ,line 300,in _create_mime_attachment
Encoders.encode_base64(attachment)
文件/usr/lib/python2.7/email/encoders.py,第45行,在encode_base64
encda​​ta = _bencode (orig)
文件/usr/lib/python2.7/email/encoders.py,第31行,_bencode
hasnewline =(s [-1] =='\\\
')
TypeError:'File'对象不支持索引

任何人都可以帮助我了解我'做错了?

解决方案

问题是文件模式(我需要使用'r')。感谢Tomita和Manjunath的部分答案。谢谢。

从芹菜导入任务
从django.core.mail.message导入EmailMessage
从django .core.files.storage import default_storage
from apps.account.models import UserProfile

@task(name ='send-email')
def send_published_article(sender,subject,身体,附件=无):
recipient = []
在UserProfile.objects.all()中的个人资料:
如果profile.user_type =='客户':
收件人。 append(profile.user.email)
email = EmailMessage(subject,body,sender,recipients)
如果附件:
email.attach(attachment.name,'r')
email.send()


EDIT: I can send attachments on my local machine, but not on Heroku. I changed the attach code to this:

if docfile:
    email.attach_file(docfile.name) 

This works locally, in the shell and when I'm running my app's localhost server.


So I've been trying to make django send emails with attachments that are stored as File objects in a story model. I have a task that handle this:

from celery import task
from django.core.mail.message import EmailMessage
from django.core.files.storage import default_storage
from apps.account.models import UserProfile

@task(name='send-email')
def send_published_article(sender, subject, body, attachment=None):
    recipients = []
    for profile in UserProfile.objects.all():
        if profile.user_type == 'Client':
            recipients.append(profile.user.email)
    email = EmailMessage(subject, body, sender, recipients)
    try:
        docfile = default_storage.open(attachment.name, 'w+')
        if docfile:
            email.attach_file(docfile.name)
        else:
            pass
    except:
        pass
    email.send()

I've been trying to find the bug with the shell, and everything seems great until I get to the last line, and run email.send(). The shell barfs up this error trace:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/vagrant/nns/local/lib/python2.7/site-packages/django/core/mail/message.py", line 248, in send
return self.get_connection(fail_silently).send_messages([self])
  File "/home/vagrant/nns/local/lib/python2.7/site-packages/django/core/mail/backends/console.py", line 23, in send_messages
self.stream.write('%s\n' % message.message().as_string())
  File "/home/vagrant/nns/local/lib/python2.7/site-packages/django/core/mail/message.py", line 215, in message
msg = self._create_message(msg)
  File "/home/vagrant/nns/local/lib/python2.7/site-packages/django/core/mail/message.py", line 272, in _create_message
return self._create_attachments(msg)
  File "/home/vagrant/nns/local/lib/python2.7/site-packages/django/core/mail/message.py", line 285, in _create_attachments
msg.attach(self._create_attachment(*attachment))
  File "/home/vagrant/nns/local/lib/python2.7/site-packages/django/core/mail/message.py", line 312, in _create_attachment
attachment = self._create_mime_attachment(content, mimetype)
  File "/home/vagrant/nns/local/lib/python2.7/site-packages/django/core/mail/message.py", line 300, in _create_mime_attachment
Encoders.encode_base64(attachment)
  File "/usr/lib/python2.7/email/encoders.py", line 45, in encode_base64
encdata = _bencode(orig)
  File "/usr/lib/python2.7/email/encoders.py", line 31, in _bencode
hasnewline = (s[-1] == '\n')
  TypeError: 'File' object does not support indexing

Can anyone help me understand what I'm doing wrong?

解决方案

The problem was the file mode (I needed to use 'r'). Credit to both Tomita and Manjunath for partial answers. Thanks.

from celery import task
from django.core.mail.message import EmailMessage
from django.core.files.storage import default_storage
from apps.account.models import UserProfile

@task(name='send-email')
def send_published_article(sender, subject, body, attachment=None):
    recipients = []
    for profile in UserProfile.objects.all():
        if profile.user_type == 'Client':
            recipients.append(profile.user.email)
    email = EmailMessage(subject, body, sender, recipients)
    if attachment:
        email.attach(attachment.name, 'r')
    email.send()

这篇关于Django电子邮件附件:TypeError:'File'对象不支持索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆