Location: PHPKode > projects > PhpRechnung > phpRechnung/invoice/email_pdf.php
<?php
/*
	email_pdf.php

	phpInvoice - is easy-to-use Web-based multilingual accounting software.
	Copyright (C) 2001 - 2008 Edy Corak < phprechnung at ecorak dot net >

	phpRechnung benutzt die FPDF Bibliothek um PDF Dateien zu generieren.
	Copyright (C) Olivier PLATHEY, http://fpdf.org/ License: Freeware.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

require_once('../include/phprechnung.inc.php');

if ($PHPSendMail == "1" )
{
	require_once("../include/mail.inc.php");
}

CheckUser();
CheckSession();

if(isset($_REQUEST['myID']))
{
	$myID = $_REQUEST['myID'];
}
if(isset($_REQUEST['invoiceID']))
{
	$invoiceID = $_REQUEST['invoiceID'];
}
if(isset($_REQUEST['page']))
{
	$page = $_REQUEST['page'];
}
if(isset($_REQUEST['infoID']))
{
	$infoID = $_REQUEST['infoID'];
}
if(isset($_REQUEST['EmailTo']))
{
	$EmailTo = $_REQUEST['EmailTo'];
}
if(isset($_REQUEST['Type']))
{
	$Type = $_REQUEST['Type'];
}
if(isset($_REQUEST['InvoiceID1']))
{
	$InvoiceID1 = $_REQUEST['InvoiceID1'];
}
if(isset($_REQUEST['CustomerID1']))
{
	$CustomerID1 = $_REQUEST['CustomerID1'];
}
if(isset($_REQUEST['DateFrom1']))
{
	$DateFrom1 = $_REQUEST['DateFrom1'];
}
if(isset($_REQUEST['DateTill1']))
{
	$DateTill1 = $_REQUEST['DateTill1'];
}
if(isset($_REQUEST['Total1']))
{
	$Total1 = $_REQUEST['Total1'];
}
if(isset($_REQUEST['Customer1']))
{
	$Customer1 = $_REQUEST['Customer1'];
}
if(isset($_REQUEST['Order']))
{
	$Order = $_REQUEST['Order'];
}
if(isset($_REQUEST['Sort']))
{
	$Sort = $_REQUEST['Sort'];
}
if(isset($_REQUEST['Canceled']))
{
	$Canceled = $_REQUEST['Canceled'];
}

$Searchstring = "InvoiceID1=$InvoiceID1&CustomerID1=$CustomerID1&DateFrom1=$DateFrom1&DateTill1=$DateTill1&Total1=$Total1&Customer1=$Customer1";

// Database connection
//
DBConnect();

// Get Invoice Information
//
$query = $db->GetAll("SELECT A.PREFIX, A.TITLE, A.FIRSTNAME, A.LASTNAME, A.ADDRESS, A.COMPANY, A.POSTALCODE, A.PRINT_NAME,
	A.CITY, A.COUNTRY, A.SALUTATION, A.MYID, DATE_FORMAT(I.INVOICE_DATE,'%d.%m.%Y') AS INVOICE_DATE, I.INVOICEID, I.TOTAL_AMOUNT, I.MYID, I.CREATEDBY, I.PAID, I.MESSAGE_DESC,
	I.METHOD_OF_PAY, DATE_FORMAT(I.METHOD_OF_PAY_DATE,'%d.%m.%Y') AS METHOD_OF_PAY_DATE, I.TAX1_TOTAL, I.TAX2_TOTAL, I.TAX3_TOTAL, I.TAX4_TOTAL, I.TAX1_DESC, I.TAX2_DESC, I.TAX3_DESC, I.TAX4_DESC, I.SUBTOTAL1, I.SUBTOTAL2, I.SUBTOTAL3, I.SUBTOTAL4
	FROM {$TBLName}addressbook AS A, {$TBLName}invoice AS I WHERE A.MYID=I.MYID AND I.INVOICEID=$invoiceID");

// If an error has occurred, display the error message
//
if (!$query)
	print($db->ErrorMsg());
else
	foreach($query as $f)
	{
		$Print_Company_Name = $f['PRINT_NAME'];
		$Date = $f['INVOICE_DATE'];
		$ID = $f['INVOICEID'];
		$CreatedBy = $f['CREATEDBY'];
		$MYID = $f['MYID'];
		$TITLE = $f['TITLE'];
		$PREFIX = $f['PREFIX'];
		$FIRSTNAME = $f['FIRSTNAME'];
		$LASTNAME = $f['LASTNAME'];
		$COMPANY = $f['COMPANY'];
		$ADDRESS = $f['ADDRESS'];
		$CITY = $f['CITY'];
		$POSTALCODE = $f['POSTALCODE'];
		$COUNTRY = $f['COUNTRY'];
		$SALUTATION = $f['SALUTATION'];
		$PAID = $f['PAID'];
		$METHOD_OF_PAY = $f['METHOD_OF_PAY'];
		$METHOD_OF_PAY_DATE = $f['METHOD_OF_PAY_DATE'];
		$SUBTOTAL1 = $f['SUBTOTAL1'];
		$SUBTOTAL2 = $f['SUBTOTAL2'];
		$SUBTOTAL3 = $f['SUBTOTAL3'];
		$SUBTOTAL4 = $f['SUBTOTAL4'];
		$TAX1 = $f['TAX1_TOTAL'];
		$TAX2 = $f['TAX2_TOTAL'];
		$TAX3 = $f['TAX3_TOTAL'];
		$TAX4 = $f['TAX4_TOTAL'];
		$TAX1_DESC = $f['TAX1_DESC'];
		$TAX2_DESC = $f['TAX2_DESC'];
		$TAX3_DESC = $f['TAX3_DESC'];
		$TAX4_DESC = $f['TAX4_DESC'];
		$TOTAL = $f['TOTAL_AMOUNT'];
		$MESSAGEID = $f['MESSAGE_DESC'];
	}

	$PrintD = Print_Date($Date);

	if($Type == 'Invoice')
	{
		$FileName = "$a[invoice_initials]-$PrintD-$ID";
		$Subject = "$a[invoice] - $a[invoice_number]: $FileName, $a[customer_no]: $MYID, $a[date_text]: $Date";
		$sendfile = "$PDFDirectory.$FileName";
		$_SESSION['Type'] = "$a[invoice_number]";
	}
	else
	{
		$FileName = "$a[delivery_note_initials]-$PrintD-$ID";
		$Subject = "$a[delivery_note] - $a[delivery_note_number]: $FileName, $a[customer_no]: $MYID, $a[date_text]: $Date";
		$sendfile = "$PDFDirectory.$FileName";
		$_SESSION['Type'] = "$a[delivery_note_number]";
	}

	$posquery = $db->GetAll("SELECT P.POSITIONID, P.POS_NAME, V.INVOICEID, V.POSITIONID, V.POS_DESC,
	V.POS_QUANTITY, V.POS_PRICE, V.TAX, V.TAX_DIVIDE, V.TAX_MULTI, V.TAX_DESC FROM {$TBLName}article AS P, {$TBLName}invoicepos AS V WHERE P.POSITIONID=V.POSITIONID AND V.INVOICEID=$invoiceID");

	// Print text if invoice is paid
	//
	$paid = $db->GetAll("SELECT METHOD_OF_PAY, INVOICEID, SUM_PAID, DATE_FORMAT(PAYMENT_DATE,'%d.%m.%Y') AS PAYMENT_DATE
			FROM {$TBLName}payment WHERE CANCELED=2 AND INVOICEID=$invoiceID");

	require_once('../include/pdf.inc.php');

	$PDF = @fopen($sendfile,"rb") or die("unable to open file ($sendfile)");
	$PDF_Size = filesize($sendfile);
	$PDF_File = fread($PDF,$PDF_Size);
	$PDF_Email = chunk_split(base64_encode($PDF_File));
	fclose($PDF);
	unlink($sendfile);
	$boundary = "=-".md5(rand())."";

	if(empty($COMPANY))
	{
		$Salutation = "$SALUTATION $PREFIX $FIRSTNAME $LASTNAME";
	}
	else
	{
		$Salutation = "$SALUTATION";
	}

	if ($PHPSendMail == "1" )
	{
		$header["From"] = "$CompanyName <$CompanyEmail>";
		$header["To"] = "$EmailTo";
		$header["Subject"] = "$Subject";
		$header["Organization"] = "$CompanyName";
		$header["MIME-Version"]	= "1.0";
		$header["Content-Type"]	= "multipart/mixed; boundary=\"$boundary\"";
		$header["Content-Transfer-Encoding"] = "7bit";
		$header["X-Mailer"] = "$a[programname] - PHP $phpversion";
	}
	else
	{
		$header = "From: {$CompanyName} <{$CompanyEmail}>\n";
		$header .= "Organization: {$CompanyName}\n";
		$header	.= "MIME-Version: 1.0\n"
		."Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
		$header	.= "Content-Transfer-Encoding: 7bit\n";
		$header	.="X-Mailer: {$a['programname']} - PHP ".PHP_VERSION."\n";
	}

	$body = "--$boundary\n"
	."Content-Type: text/plain; charset=$_SESSION[Charset]\n"
	."Content-Disposition: inline\n"
	."Content-Transfer-Encoding: 7bit\n"
	."\n"
	."$Salutation,\n"
	."\n"
	."$PDFAttachmentText\n"
	."\n"
	."--$boundary\n"
	."Content-Type: application/pdf; name=\"$FileName.pdf\"\n"
	."Content-Description: \"$Subject\"\n"
	."Content-Transfer-Encoding: base64\n"
	."Content-Disposition: attachment\n"
	."\n"
	."$PDF_Email\n"
	."\n"
	."--$boundary--\n";

	if(!empty($EmailTo))
	{
		if($PHPSendMail == "1")
		{
			$mail_object->send($EmailTo, $header, $body);
			$_SESSION['emailID'] = '1';
		}
		else
		{
			// If you have safe_mode = On, please delete ,"-f$CompanyEmail"
			// because the fifth parameter is not allowed in safe_mode
				//
			mail("$EmailTo",$Subject,$body,$header,"-f$CompanyEmail");
			$_SESSION['emailID'] = '1';
		}

		// Database connection
		//
		DBConnect();

		$query2 = "INSERT INTO {$TBLName}syslog (SYSLOGID, CREATED, DESCRIPTION, CREATEDBY, USERGROUP1, USERGROUP2)";
		$query2 .= "VALUES(NULL, '$CurrentDateTime', '$Subject was send by user $_SESSION[Username] (uid=$_SESSION[UserID]) from $IPAddress to E-Mail: $EmailTo', 'admin', '1', '2')";
		if ($db->Execute($query2) === false)
		{
			die($db->ErrorMsg());
		}
	}
	else
	{
		$_SESSION['emailID'] = '2';
	}

	if($infoID == '9')
	{
		Header("Location: $web/invoice/searchlist.php?myID=$myID&invoiceID=$invoiceID&page=$page&$Searchstring&Order=$Order&Sort=$Sort&Canceled=$Canceled&$sessname=$sessid#$invoiceID");
	}
	else
	{
		Header("Location: $web/invoice/list.php?myID=$myID&invoiceID=$invoiceID&page=$page&Order=$Order&Sort=$Sort&Canceled=$Canceled&$sessname=$sessid#$invoiceID");
	}
?>
Return current item: PhpRechnung