Location: PHPKode > projects > phpCAMALEO > common/fun2inc/emailer.inc
<?php
/***************************************************************************
 *	emailer.inc
 *
 *	begin		: March 2005
 *	version		: 10 October 2006
 *	copyright	: (C) 2005,2006 grandolini.net
 *
 ***************************************************************************/
if(!function_exists('emailer'))
{
	#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	# Send email
	#
	function emailer($to, 
					 $object,
					 $body,
					 $reply			= '',
					 $cc			= '',
					 $bcc			= '',
					 $from			= '',
					 $text_type		= 'plain', 
					 $x_prio		= '3',
					 $attach_path	= '',
					 $attach_file	= ''
					 )
	#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	{
		global $USER_PRIV;
		if($_SESSION[misc][MAILSRV]=='none') { die('(emailer.inc) MailServer is set to NONE!'); }
		define(_CR_,"\r\n");
		define(_TAB_,"\t");
		#
		#	Initializations
		#
		if($reply=='') { $reply=$_SESSION[misc][E_NOREPLY]; }
		if($from=='')  { $from =$_SERVER[HTTP_HOST].'#'.$_SESSION[misc][E_SENDER]; }
		list($user_id,$from_id)=explode('#', $from);
		$domain=$_SERVER[SERVER_NAME];
		$mime_boundary='------------{'.md5(uniqid(time())).'}';
		#
		#	Set the common header
		#
		$headers = 'MIME-Version: 1.0'._CR_;
		#
		#	$headers .= 'Return-Receipt-To: '.$_SESSION[misc][E_ADMIN].'<'.$_SESSION[misc][E_ADMIN].'>'._CR_;
		#
		if($reply) { $headers.='Reply-To:'.$reply._CR_; }
		#
		if(is_array($cc))
		{
			#	Copy
			#
			$t=count($cc);
			$headers.='Cc: ';
			for($i=0;$i<$t;$i++) { $headers.=$cc[$i].', '; }
			$headers=substr($headers,0,-2)._CR_;
		}
		else { if($cc) { $headers.='Cc: '.$cc._CR_; } }
		#
		if(is_array($bcc))
		{
			#	Blind copy
			#
			$t=count($bcc);
			$headers.='Bcc: ';
			for($i=0;$i<$t;$i++) { $headers.=$bcc[$i].', '; }
			$headers=substr($headers,0,-2)._CR_;
		}
		else { if($bcc) { $headers.='Bcc: '.$bcc._CR_; } }
		#
		$headers.='User-Agent: '.$_SERVER[HTTP_USER_AGENT]._CR_;
		$headers.='From: '.$user_id.' <'.$from_id.'>'._CR_;
		$headers.='Message-ID: <'.md5(uniqid(time())).'@{'.$domain.'}>'._CR_;
		#
		switch($x_prio)
		{
			case '1':	$x_prio.=' (Highest)';	break;
			case '2':	$x_prio.=' (High)';		break;
			case '3':	$x_prio.=' (Normal)';	break;
			case '4':	$x_prio.=' (Low)';		break;
			case '5':	$x_prio.=' (Lowest)';	break;
		}
		if($x_prio)	{ $headers.='X-Priority: '.$x_prio._CR_; }
		#
		#	Message Priority for Exchange Servers
		#
		#	$headers .=	'X-MSmail-Priority: '.$x_prio_des._CR_;
		#
		#	!!! WARNING !!!---# Hotmail and others do NOT like PHP mailer...
		#	$headers .=	'X-Mailer: PHP/'.phpversion()._CR_;---#
		#
		#	$headers .= 'X-Mailer: Microsoft Office Outlook, Build 11.0.6353'._CR_;
		#	$headers .= 'X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527'._CR_;
		#
		$headers.='X-Sender: '.$_SESSION[misc][E_ROBOT]._CR_;
		$headers.='X-AntiAbuse: This is a solicited email for - '.$to.' - '._CR_;
		$headers.='X-AntiAbuse: Servername - {'.$domain.'}'._CR_;
		$headers.='X-AntiAbuse: User - '.$_SESSION[misc][E_ROBOT]._CR_;
		#
		#	Set the right start of header
		#
		if($attach_file)
		{
			include($_SESSION[common].'fun2inc/mimetype.inc');
			$file_name_type=mimetype($attach_path,$attach_file);
			$file_name_name=$attach_file;
			#
			#	Read the file to be attached
			$file=@fopen($attach_path.$attach_file,'rb') or die('(emailer.inc) Cannot open: '.$attach_path.$attach_file);
			while(!feof($file)) { $data.=@fread($file,4096); }
			@fclose($file);
			#
			#	Base64 encode the file data
			$data=chunk_split(base64_encode($data));
			#
			#	Complete headers
			$headers.='Content-Type: multipart/mixed;'._CR_;
			$headers.=' boundary="'.$mime_boundary.'"'."\n\n";
			#
			#	Add a multipart boundary above the text message
			$mail_body_attach= 'This is a multi-part message in MIME format.'._CR_;
			$mail_body_attach.='--'.$mime_boundary."\n";
			$mail_body_attach.='Content-Type: text/'.$text_type.'; charset=us-ascii;'."\n";	# us-ascii,	iso-8859-1
			$mail_body_attach.='Content-Transfer-Encoding: 7bit'."\n\n";					# 7bit, 8bit
			$mail_body_attach.=$body."\n";
			#
			#	Add file attachment
			$mail_body_attach.='--'.$mime_boundary."\n";
			$mail_body_attach.='Content-Type: '.$file_name_type.";\n";
			$mail_body_attach.=' name="'.$file_name_name.'"'."\n";
			$mail_body_attach.='Content-Disposition: attachment;'."\n";
			$mail_body_attach.=' filename="'.$file_name_name.'"'."\n";
			$mail_body_attach.='Content-Transfer-Encoding: base64'."\n\n";
			$mail_body_attach.=$data."\n";
			#
			$body=$mail_body_attach;
		} 
		else
		{
			if($text_type=='plain')
			{
				$headers.='Content-Type: text/'.$text_type.'; charset=us-ascii;'._CR_;	# us-ascii, iso-8859-1
				$headers.='Content-Transfer-Encoding: 7bit'._CR_;						# 7bit, 8bit
			}
			if ($text_type=='html')
			{
				$headers.='Content-Type: multipart/alternative;'._CR_;
				$headers.=' boundary="'.$mime_boundary.'"'."\n\n";
				#
				#	Add ascii
				$mail_body_multipart= 'This is a multi-part message in MIME format.'._CR_;
				$mail_body_multipart.='--'.$mime_boundary."\n";
				$mail_body_multipart.='Content-Type: text/plain; charset=us-ascii; format=flowed'."\n";
				$mail_body_multipart.='Content-Transfer-Encoding: 7bit'."\n\n";
				$mail_body_multipart.=$body."\n\n";
				#
				#	Add html
				$mail_body_multipart.='--'.$mime_boundary."\n";
				$mail_body_multipart.='Content-Type: text/html; charset=us-ascii'."\n";
				$mail_body_multipart.='Content-Transfer-Encoding: 7bit'."\n\n";
				$mail_body_multipart.=ascii2html($body)."\n";
				#
				$body=$mail_body_multipart."\n".'--'.$mime_boundary."--\n";
			}
		}
		#
		if($_SESSION[misc][MAILSRV]!='ini')
		{
			ini_set(SMTP,$_SESSION[misc][MAILSRV]);
			ini_set(smtp_port,$_SESSION[misc][MAILSRVPORT]);
		}
		ini_set(sendmail_from,$_SESSION[misc][E_SENDER]);
		#
		#	echo '(emailer.inc)<hr>To<hr>'.$to,'<hr>Object<hr>'.$object.'<hr>Headers<hr>'.$headers.'<hr>Body<hr>'.$body.'<hr>'; die; return('*OK*'); # debug
		#	echo '(emailer.inc)'.'Headers<hr>'.$headers.'<hr>Body<hr>'.$body.'<hr>'; die; return('*OK*'); # debug
		#
		#	$extra_header = '-fwebmaster@{'.$domain.'}'; # this is the User of the machine or hosting account
		#
		$tmp=@mail($to,$object,$body,$headers); #, $extra_header);
		ini_restore(sendmail_from);
		#
		if($tmp==true) { return('*OK*'); } else
		{
			echo '<hr>There has been a mail error sending to:'.$to.'<hr>';
			if($USER_PRIV>=80)
			{
				echo 'Subject:'.$object
					.'<br>Reply:'.$reply
					.'<br>cc:'.$cc 
					.'<br>Body:<br>'.ascii2html($body)
					.'<br>From_id:'.$from_id
					.'<br>Mail Server:'.$_SESSION[misc][MAILSRV].':'.$_SESSION[misc][MAILSRVPORT];
			}
			die;
		}
	}
	#~~~~~~~~~~~~~~~~~~~~~~~~~~
	function ascii2html($text)
	#~~~~~~~~~~~~~~~~~~~~~~~~~~
	{
		$css=file_get_contents($_SESSION[skinf].'emailer-css.htm');
		return($css."<body>\n<i>".date('r T').'</i><br /><br />'.$text."\n</body></html>");
	}
}
?>
Return current item: phpCAMALEO