AttributeError:“列表”对象没有属性“ lstrip”

时间:2019-08-23 06:25:31

标签: python django email smtp

在过去的3个小时中,我试图将包含CSV文件附件的电子邮件发送到从数据库中获取的电子邮件列表中,但是却收到一个错误消息,我不确定这是为什么发生的。 如果没有传递任何unicode,我会仔细检查我的代码是否有错误,以免将获取的数据转换为str但没有用。

如果我独立运行脚本,它将执行并发送带有附件的电子邮件,而不会出现问题,但是在Django中,它给出了错误。

电子邮件列表用于检查错误是否与数据库有关,但与数据库无关。它也会显示在“预定义电子邮件”列表中。

我收到了摘录 Here

有一个Reference to such an error,但我认为它不能为我带来任何好处。

错误

执行系统检查...

  

由Traceback(最近一次调用最近)启动的线程中未处理的异常:File   “ 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”,   _run_checks中的第346行       返回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 =检查(app_configs = app_configs)文件“ C:\ Users \ BITSWI〜1 \ Desktop \ LCRPRO〜1 \ VE \ lib \ site-packages \ django \ core \ checks \ urls.py”,   第16行,在check_url_config中       返回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行,获取       res =实例。字典 [self.name] = self.func(instance)文件“ 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行,获取       res =实例。字典 [self.name] = self.func(instance)文件“ 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中       导入(名称)文件“ 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行,在flatten中       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,编码       值= self._encode_chunks(newchunks,maxlinelen)文件“ c:\ python27 \ Lib \ email \ header.py”,第370行,_encode_chunks       _max_append(块,s,maxlinelen,其他)文件“ c:\ python27 \ Lib \ email \ quoprimime.py”,第97行,在_max_append中       L.append(s.lstrip())AttributeError:“列表”对象没有属性“ 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()

1 个答案:

答案 0 :(得分:1)

尽管Python使通过smtplib模块发送电子邮件相对容易,但Django在其上提供了两个轻包装器。提供这些包装程序是为了使电子邮件发送速度更快,使开发过程中的电子邮件发送测试变得容易,并为无法使用SMTP的平台提供支持。

https://docs.djangoproject.com/en/1.11/topics/email/