在python中,如果附件数据为空,如何停止发送邮件?

2024-10-02 16:23:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用csvwriter和StringIo在Python中编写一个mail函数,我的目的是只在附件包含一些数据时发送带有附件的邮件。你知道吗

但是,该功能还可以在文件为空的情况下发送邮件。 请帮帮我哪里出错了。你知道吗

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

我得到的输出还包含我正在传递的带有头的空文件。你知道吗


Tags: csvtonoiftimeemailchannelsync
1条回答
网友
1楼 · 发布于 2024-10-02 16:23:16

我解决了使条件为假的问题: 我更改的代码片段只是:

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.')

相关问题 更多 >