【问题标题】:How to stop mails to be sent if the attachment data is empty in python?如果python中的附件数据为空,如何停止发送邮件?
【发布时间】:2015-08-11 06:17:57
【问题描述】:

我正在使用 csvwriter 和 StringIo 在 Python 中编写一个邮件函数,我的动机是仅当附件包含一些数据时才发送带有附件的邮件。

但是,该功能还可以在文件为空的情况下发送邮件。 请帮助我在哪里犯错。

def cbazaar_quantity_sync(self,products_data_quantity_sync,threep_numbers):
    channel = Channel.objects.get(name='CBazaar')
    channel_configurations = channel.channelconfiguration_set.all()
    listed_threep_numbers = self.filter_products_listed_on_channel(channel,threep_numbers)
    products_to_sync = self.filter_products_data(products_data_quantity_sync,listed_threep_numbers)
    # pdb.set_trace()
    data = products_to_sync
    current_time = datetime.now()
    task_start_time = "%s-%s-%s_%s:%s:%s"%(current_time.day, current_time.month, current_time.year, current_time.hour, current_time.minute, current_time.second)
    csvresult = StringIO.StringIO()
    csvresultwriter = csv.writer(csvresult)
    csvresultwriter.writerow(["Pri.Vendor Name","Product Code","Product Size","Design No","Stock Qty","Shipping Lead Time","Replicable (Yes / No)","Is stock Qty exclusively allocated to CBazaar ( Yes / No)"])
    if len(products_to_sync) != 0:
        for product in data:
            # pdb.set_trace()
            Store_Name = str('voylla retail pvt ltd')
            sku = str(product[2])
            size = str(product[3])
            threep_number =  str(product[1])
            qty = int(product[7])
            leadtime = int(product[8])
            mod = str(product[11])
            none = str('None')
            if mod == none:
                leadtime = 2
            replicable = str('Yes')
            product_obj = Product.objects.get(threep_number=threep_number)
            channel_sku = self.threep_sku_mapping_method(channel,product_obj)
            # pdb.set_trace()
            if channel_sku != 0:
                sku=str(channel_sku)
                if qty == 0:
                    continue
                else:
                    csvresultwriter.writerow(['%s'%Store_Name,'%s'%sku,'%s'%size,'%s'%threep_number,'%d'%qty,'%d'%leadtime,'%s'%replicable])
    else:
        self.stdout.write('No products to sync with CBazaar.')
    # #Build message for Production
    # email = EmailMessage(subject='CBazaar Quantity Sync Details File', body='PFA CSV File attached with this mail.', from_email='help@voylla.com',
    #         to=['tamilmaran@cbazaar.com'], cc=['3pcatalogging@voylla.in'],
    #         headers = {'Reply-To': '3pcatalogging@voylla.in'})
    # # Build message for Local
    email = EmailMessage(subject='CBazaar Quantity Sync Details File', body='PFA CSV File attached with this mail.', from_email='help@voylla.com',
            to=['abhishek.g@qa.voylla.com'],
            headers = {'Reply-To': '3pcatalogging@voylla.in'})
    # Attach csv file
    email.attach("cbazaar_quantity_sync_"+task_start_time+".csv", csvresult.getvalue(), 'text/csv')
    # Send message with built-in send() method
    email.send()

    #=================================================================#
    ##For sending Mail for "Zero Quantity Product" to Threep-Team
    #=================================================================#
    csvresult = StringIO.StringIO()
    csvresultwriter = csv.writer(csvresult)
    csvresultwriter.writerow(["Pri.Vendor Name","Product Code","Product Size","Design No","Stock Qty","Shipping Lead Time","Replicable (Yes / No)","Is stock Qty exclusively allocated to CBazaar ( Yes / No)"])
    if len(products_to_sync) != 0:
        for product in data:
            # pdb.set_trace()
            Store_Name = str('voylla retail pvt ltd')
            sku = str(product[2])
            size = str(product[3])
            threep_number =  str(product[1])
            qty = int(product[7])
            leadtime = int(product[8])
            mod = str(product[11])
            none = str('None')
            if mod == none:
                leadtime = 2
            replicable = str('Yes')
            product_obj = Product.objects.get(threep_number=threep_number)
            channel_sku = self.threep_sku_mapping_method(channel,product_obj)
            if qty == 0:
                        if channel_sku != 0:
                            sku=str(channel_sku)
                            csvresultwriter.writerow(['%s'%Store_Name,'%s'%sku,'%s'%size,'%s'%threep_number,'%d'%qty,'%d'%leadtime,'%s'%replicable])
                        else:
                            self.stdout.write('No Zero Quantity products to sync with CBazaar.')                        
    # Build message for Threep-Team
    email = EmailMessage(subject='CBazaar_"Zero_Quantity_product"_File', body='PFA CSV File attached with this mail.', from_email='help@voylla.com',
    to=['abhishek.g@qa.voylla.com'])
    # # Attach csv file
    email.attach("cbazaar_zero_quantity_"+task_start_time+".csv", csvresult.getvalue(), 'text/csv')
    # # Send message with built-in send() method
    email.send()
    return

我得到的输出还包含我正在传递的带有标题的空文件。

【问题讨论】:

  • 当你说空文件时,你的意思是 - csvresult.getvalue() - 是空字符串?你不能检查一下吗?
  • 没有看到空文件是如何被过滤掉的——你在打开csvresultwriter之后在product in data循环之外放了一个标题行,这意味着输出必须有标题。您希望如何处理您的“空”文件?
  • @AnandSKumar 我检查了 csvresult.getvalue() 是的,它正在显示 csvresultwriter.writerow() 数据。不是 products_to_sync 数据。告诉我如果文件为空,我该怎么做才能停止发送邮件
  • @charlee 我也试过你的意见,但它也在发送带有损坏邮件附件的文件
  • 如果 csv 不包含任何数据,你应该停止email.attach()

标签: python stringio


【解决方案1】:

我解决了使条件为 false 的问题: 我更改的代码 sn-p 只是:

if len(products_to_sync) != 0:
        for product in data:
            # pdb.set_trace()
            Store_Name = str('voylla retail pvt ltd')
            sku = str(product[2])
            size = str(product[3])
            threep_number =  str(product[1])
            qty = int(product[7])
            leadtime = int(product[8])
            mod = str(product[11])
            none = str('None')
            if mod == none:
                leadtime = 2
            replicable = str('Yes')
            product_obj = Product.objects.get(threep_number=threep_number)
            channel_sku = self.threep_sku_mapping_method(channel,product_obj)
            # pdb.set_trace()
            if channel_sku != 0:
                sku=str(channel_sku)
                if qty == 0:
                    continue
                else:
                    csvresultwriter.writerow(['%s'%Store_Name,'%s'%sku,'%s'%size,'%s'%threep_number,'%d'%qty,'%d'%leadtime,'%s'%replicable])
        email = EmailMessage(subject='CBazaar Quantity Sync Details File', body='PFA CSV File attached with this mail.', from_email='help@voylla.com',
            to=['abhishek.g@qa.voylla.com'],
            headers = {'Reply-To': '3pcatalogging@voylla.in'})
        # Attach csv file
        email.attach("cbazaar_quantity_sync_"+task_start_time+".csv", csvresult.getvalue(), 'text/csv')
        # Send message with built-in send() method
        email.send()
else:
     self.stdout.write('No products to sync with CBazaar.')

【讨论】:

    猜你喜欢
    • 2015-10-24
    • 2011-10-20
    • 1970-01-01
    • 2019-09-09
    • 2018-05-31
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 2020-01-17
    相关资源
    最近更新 更多