Page 1 of 1

Parse Download For EPUB And Mail As Attachment With Gmail

Posted: July 30th, 2012, 7:13 pm
by keyosk
You'll need to tweak the path in the batch script to point it at where you place the PHP script.

You'll also need to configure the gmail email and password toward the top of the php script.

This has been tested by sending and receiving from the same gmail account, I have not tested sending from gmail to another provider, but it should be fine.

This will look for and send any file with EPUB extension located in the completed download folder for a given job.

Batch Script 'general.cmd' :

Code: Select all

@echo off
php -f C:\sabnzbd_post\general.php %1 %2 %3 %4 %5 %6 %7
Processing Script 'general.php' :

Code: Select all

<?php
/**
    START CONFIGURE
    $email should be a full gmail account
    $password should be the password for this account
**/
$email = 'example@gmail.com';
$password = 'example_password';
/**
    END CONFIGURE
**/

$path = $argv[1];
$filename = $argv[2];
$name = $argv[3];
$index_report_number = $argv[4];
$category = $argv[5];
$group = $argv[6];
$status = $argv[7];

if ($status === '0') {
    $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
    while($it->valid()) {
        if (!$it->isDot()) {
            $info = pathinfo($it->key());
            if (in_array($info['extension'], array('epub'))) {
                echo PHP_EOL . $it->key() . PHP_EOL;
                $basename = basename($it->key());
                $attachment = chunk_split(base64_encode(file_get_contents($it->key())));
                $random_hash = md5(date('r', time()));
                $data = 'MIME-Version: 1.0
To: <'.$email.'>
Subject: eBook : '.$name.'
Content-Type: multipart/mixed; boundary='.$random_hash.'

--'.$random_hash.'

--'.$random_hash.'
Content-Type: application/octet-stream; 
    name="'.$basename.'"
Content-Disposition: attachment; 
    filename="'.$basename.'"
Content-Transfer-Encoding: base64

'.$attachment.'
--'.$random_hash.'--';
                file_put_contents($it->key().'tmp', $data);
                $cmd = 'curl --globoff --ssl-reqd --mail-from "<'.$email.'>" --mail-rcpt "<'.$email.'>" --url smtps://smtp.gmail.com:465 -T "'.$it->key().'tmp'.'" -u "'.$email.':'.$password.'" ';
                exec($cmd);
                unlink($it->key().'tmp');
            }
        }
        $it->next();
    }
}
echo PHP_EOL.'Done processing :)';
exit;
?>