Location: PHPKode > projects > ICTFAX > ictfax/agi-bin/fax2mail.php
#!/usr/bin/php -q
<?php
    $conf         = array();
    $configSource = '/usr/ictfax/ictfax.conf';

    global $argv;
    $stdinRequest = array();
    $attachment   = '';

    //Getting input data (Parameter Passed to Script)
    $fax_did   = $argv[1];
    $caller_id = $argv[2];
    $tiff_file = $argv[3];
    $fax_pages = $argv[4];
    $fax_date  = date('Y-m-d H:i:s');

    // Loading Configuration
    if (is_file($configSource)) {
        $conf = parse_ini_file($configSource, TRUE);
        if (!$conf) {
            die("Bad configuration file");
        }
    } else {
        die("No configuration file found");
    }

    // Connecting with database
    mysql_connect($conf['dbhost'], $conf['dbuser'], $conf['dbpass']);
    @mysql_select_db($conf['dbname']) or die("Unable to select database");

	
    // Load DID relevent entries from database
 	  $SQL    = "SELECT forwardto, redirectevent, redirectalways
	             FROM astacdid 
	             WHERE active = 1 AND redirectevent = 'F' AND did = '$fax_did' LIMIT 1";
	  $result = mysql_query($SQL);
	  $forw   = mysql_fetch_array($result) or die('No extension found');

    $accounts   = explode('&', $forw['forwardto']);
    $accounts   = implode(',', $accounts);
    $user_email = $forw['redirectalways'];

    $SQL    = "SELECT astaccount.tech, astaccount.accountcode, astaccount.ccbatchno, aststatus.servertech, astaccount.serverid
               FROM astaccount LEFT JOIN aststatus ON astaccount.serverid = aststatus.serverid
               WHERE ((astaccount.accountcode) IN ($accounts)) LIMTI 1";
    $result = mysql_query($SQL);
    $account= mysql_fetch_array($result);



    $company_name  = $conf['company_name'];
    $company_email = $conf['emailadd'];


    // GET tiff to pdf data
    exec("tiff2pdf '$tiff_file' > '$tiff_file.pdf'");
    $attachment = chunk_split(base64_encode(file_get_contents("$tiff_file.pdf")));


    // prepare to send an email
    $random_hash = md5(uniqid(time()));
    $subject = "You have received a FAX";

    ob_start();
    include(dirname(__FILE__).'/fax_email.txt.php');
    $txt_message = ob_get_clean();

    ob_start();
    include(dirname(__FILE__).'/fax_email.tpl.php');
    $html_message = ob_get_clean();

    $header  = "From: $company_name <$company_email>\r\n";
    $header .= "Reply-To: $company_email\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"mixed$random_hash\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--mixed$random_hash\r\n";

    $header .= "Content-Type: multipart/alternative; boundary=\"alt$random_hash\"\r\n\r\n";
    $header .= "--alt$random_hash\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $txt_message."\r\n\r\n";
    $header .= "--alt$random_hash\r\n";
    $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $html_message."\r\n\r\n";
    $header .= "--alt$random_hash--\r\n\r\n";

    $header .= "--mixed$random_hash\r\n";
    $header .= "Content-Type: application/pdf; name=\"fax.pdf\"\r\n";
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"fax.pdf\"\r\n\r\n";
    $header .= $attachment."\r\n\r\n";
    $header .= "--mixed$random_hash--";

    return @mail($user_email, $subject, '', $header); 

?>
Return current item: ICTFAX