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

时间:2015-08-11 06:17:57

标签: python stringio

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

但是,该函数也在空文件的情况下发送邮件。 请帮助我在哪里犯错误。

<?php
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_YoutubeService.php';
session_start();

$OAUTH2_CLIENT_ID = 'XXXXXXXXXXXXXXXX';
$OAUTH2_CLIENT_SECRET = 'XXXXXXXXXXXXXXXX';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
  FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

$youtube = new Google_YoutubeService($client);

if (isset($_GET['code'])) 
{
    if (strval($_SESSION['state']) !== strval($_GET['state'])) 
    {
        die('The session state did not match.');
    }

    $client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
    header('Location: ' . $redirect);
}

if (isset($_SESSION['token'])) 
{
  $client->setAccessToken($_SESSION['token']);
}

$json_output = json_decode($client->getAccessToken());
$token = $json_output->access_token;   

if ($client->getAccessToken()) 
{        
    $userid = getYoutubeData($token);
    $htmlBody = "SubscriberCount: ".youtubefollowers($userid);
    $_SESSION['token'] = $client->getAccessToken();   
}
else 
{
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  $htmlBody = <<<END
  <h3>Connect to youtube</h3>
  <p>You need to <a href="$authUrl">Connect</a> before proceeding.<p>
  END;
}

function getYoutubeData($token)
{   
    $json_url ='https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true&access_token='.$token;
    $json = file_get_contents($json_url);
    $json_output = json_decode($json);

    if($json_output->items[0]->id){
            $id = $json_output->items[0]->id;
    }else{
            $id = "";
    }
    return $id;
}

function youtubefollowers($channelID)
{   
    $json_url ='https://www.googleapis.com/youtube/v3/channels?part=statistics&id='.$channelID.'&key=AIzaSyAlgOkFu2KlYJAQwt5j3jO1rUARpPzAIww';
    $json = file_get_contents($json_url);
    $json_output = json_decode($json);

    if($json_output->items[0]->statistics->subscriberCount){
            $subscriberCount = $json_output->items[0]->statistics->subscriberCount;
    }else{
            $subscriberCount = 0;
    }
    return $subscriberCount;
}
?>

<!doctype html>
<html>
  <head>
    <title>YouTube</title>
  </head>
  <body>
   <?=$htmlBody?>
  </body>
</html>

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

1 个答案:

答案 0 :(得分:0)

我解决了使条件错误的问题:   我改变的代码片段只是:

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