【发布时间】:2019-12-28 11:00:30
【问题描述】:
在过去的 3 个小时里,我试图向从数据库中获取的电子邮件列表发送一封带有 CSV 文件附件的电子邮件,但我收到了一个错误,我不确定它为什么会发生。 如果有任何 unicode 被通过等,我会仔细检查我的代码是否有错误,以避免我将获取的数据转换为 str 但没有用。
如果我独立运行脚本,它会毫无问题地执行并发送带有附件的电子邮件,但在 Django 中它会出错。
电子邮件列表用于检查错误是否与数据库相关,但不是。它也会显示在预定义的电子邮件列表中。
我从 Here
有一个Reference to such an error,但我认为它没有给我带来任何好处。
错误
正在执行系统检查...
Traceback 启动的线程中未处理的异常(最近一次调用最后一次):文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\utils\autoreload.py", 第 228 行,在包装器中 fn(*args, **kwargs) 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\management\commands\runserver.py", 第 124 行,inner_run self.check(display_num_errors=True) 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\management\base.py", 第 359 行,检查中 include_deployment_checks=include_deployment_checks,文件“C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\management\base.py”, 第 346 行,在 _run_checks 返回 checks.run_checks(**kwargs) 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\checks\registry.py", 第 81 行,在 run_checks new_errors = check(app_configs=app_configs) 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\checks\urls.py", 第 16 行,在 check_url_config 中 return check_resolver(resolver) 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\checks\urls.py", 第 26 行,在 check_resolver 中 返回 check_method() 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\urls\resolvers.py", 第 256 行,检查中 对于 self.url_patterns 中的模式:文件“C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\utils\functional.py”, 第 35 行,在 get 中 res = instance.dict[self.name] = self.func(instance) File "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django \urls\resolvers.py", 第 407 行,在 url_patterns 中 模式= getattr(self.urlconf_module,“urlpatterns”,self.urlconf_module)文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\utils\functional.py", 第 35 行,在 get 中 res = instance.dict[self.name] = self.func(instance) File "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django \urls\resolvers.py", 第 400 行,在 urlconf_module 中 返回 import_module(self.urlconf_name) 文件“c:\python27\Lib\importlib__init__.py”,第 37 行,在 import_module 导入(名称)文件“C:\Users\Bitswits 3\Desktop\LCRProject\LeastCostRouting\LeastCostRouting\urls.py”,行 12、在 url('', include('LCR.urls')), 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\conf\urls__init__.py", 第 50 行,包括 urlconf_module = import_module(urlconf_module) 文件“c:\python27\Lib\importlib__init__.py”,第 37 行,在 import_module import(名称)文件“C:\Users\Bitswits 3\Desktop\LCRProject\LeastCostRouting\LCR\urls.py”,第 25 行,在 url(r'^(?i)someemail/$', views.lcr_send_email(), name='EE-Page'), 文件 "C:\Users\Bitswits 3\Desktop\LCRProject\LeastCostRouting\LCR\views.py",第 422 行,在 lcr_send_email server.sendmail(from_address, email_list, msg.as_string()) 文件“c:\python27\Lib\email\message.py”,第 137 行,在 as_string g.flatten(self, unixfrom=unixfrom) 文件“c:\python27\Lib\email\generator.py”,第 83 行,扁平化 self._write(msg) 文件“c:\python27\Lib\email\generator.py”,第 115 行,在 _write self._write_headers(msg) 文件“c:\python27\Lib\email\generator.py”,第 164 行,在 _write_headers v, maxlinelen=self._maxheaderlen, header_name=h).encode() 文件“c:\python27\Lib\email\header.py”,第 410 行,在编码中 value = self._encode_chunks(newchunks, maxlinelen) 文件“c:\python27\Lib\email\header.py”,第 370 行,在 _encode_chunks _max_append(chunks, s, maxlinelen, extra) 文件“c:\python27\Lib\email\quoprimime.py”,第 97 行,在 _max_append L.append(s.lstrip()) AttributeError: 'list' object has no attribute 'lstrip'
代码
def lcr_send_email():
template = "LCR\LCR_Email.html"
email_list = []
vendor_list = []
email_data = RecipientEmails.objects.values_list(
'recipient_email', flat=True)
for i in email_data:
email_list.append(str(i))
for z in email_list:
print type(z)
vendor_name = Vendor.objects.values_list('ven_name', flat=True)
for i in vendor_name:
vendor_list.append(str(i))
emails =[
'foo@example.com',
'bar@example.com',
...
]
subject = vendor_list
password = settings.EMAIL_HOST_PASSWORD
email = settings.EMAIL_HOST_USER # email from
smtp_server = "smtp.gmail.com"
smtp_port = 587
from_address = settings.EMAIL_HOST_USER
from_password = settings.EMAIL_HOST_PASSWORD
mail_body = "This is an automated respose"
msg = MIMEMultipart()
msg['Subject'] = subject
msg['To'] = email_list
msg.attach(MIMEText(mail_body))
file = "MyCsv.csv"
part = MIMEBase('application', "octet-stream")
part.set_payload(open(file, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment; filename="{0}"'.format(os.path.basename(file)))
msg.attach(part)
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(from_address, from_password)
server.sendmail(from_address, email_list, msg.as_string())
server.quit()
【问题讨论】:
-
哪个版本的 django?
-
Django = 1.11.17 Python = 2.7
-
这里发生了很多奇怪的事情。为什么不使用 Django 提供的邮件功能,甚至不使用 smtplib
send_email消息,而不是真正低级的sendmail?但要解决您的问题,我们可能需要查看 EMAI_HOST_USER 值。 -
@DanielRoseman 是 DjangoEmail@gmail.com