<?php
if (!defined('SECRET_KEY')){ header("HTTP/1.0 404 Not Found"); exit; }

###
###  Basic Layout Class
###

class recipeEmail {

	var $_email;
	var $_body;
	var $_header;
	var $_subject;
    var $_from;

	function recipeEmail($id, $email, $subject, $msg, $layout){
		global $rSdb;

	    $useroptions = $rSdb->get_option($_SESSION['username']."_options");

	    if (  unsanitize($useroptions['rS_email'],true)<>''  )
	        $this->_from = unsanitize($useroptions['rS_email'],true);
	    else{
	        $this->_from = getGenericFROM();
		}

		$eol 			 = "\n";

		$r				 = preg_match('/<img.+\/photos\/([^"]+)".+[^>]+>/',$layout,$imageFile);
		$imageFile		 = dirname(__FILE__) . '/../photos/' . urldecode($imageFile[1]);
        $imageExt		 = substr( $imageFile,-3 );

        $i				 = ( $imageFile<>'' )?'<img src="cid:recipe@recipeshelf"/>':'';

        $layout 		 = preg_replace('/<img[^>]+>/',$i,$layout,1);
        $layout 		 = preg_replace('/<\/style>/',$eol.'</style><style>div.rsl-layout{ width:auto; margin:15px 20px; }</style>'.$eol,$layout);

		$this->_email	 = $email;
		$this->_subject	 = $subject;

		$this->_header 	 = 'From: '. $this->_from . $eol;
		$this->_header 	.= 'MIME-Version: 1.0' . $eol;
		$this->_header 	.= 'Content-Type: multipart/related; type="text/html"; boundary="----MIME_BOUNDRY_main_message"';

		$this->_body	.= $eol . '------MIME_BOUNDRY_main_message'  . $eol;

		$this->_body	.= 'Content-Type: multipart/alternative; boundary="----MIME_BOUNDRY_sub_message"' . $eol . $eol;
		$this->_body	.= '------MIME_BOUNDRY_sub_message'  . $eol;
		$this->_body	.= 'Content-Type: text/plain; charset="utf-8"' . $eol;
		$this->_body	.= 'Content-Transfer-Encoding: 8bit'  . $eol . $eol . $eol;

		$this->_body	.= __('Please view with an HTML capable email client.','recipeshelf') . $eol . $eol;

		$this->_body	.= '------MIME_BOUNDRY_sub_message'  . $eol;
		$this->_body	.= 'Content-Type: text/html; charset="utf-8"'. $eol;
		$this->_body	.= 'Content-Transfer-Encoding: 8bit'  . $eol . $eol . $eol;

		$this->_body 	.= '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'. $eol;
		$this->_body 	.= '<html><head><meta content="text/html;charset=utf-8" http-equiv="Content-Type"><title>[recipeShelf]</title></head>'. $eol;
		$this->_body 	.= '<body alink=#ee0000 bgcolor=#FFFFFF link=#333333 text=#000000 vlink=#551a8b>'. $eol;
		$this->_body 	.= $msg . '<br /><br />' . $eol . $layout . $eol;
		$this->_body 	.= '</html>'. $eol;
		$this->_body	.= $eol . $eol;

		$this->_body	.= '------MIME_BOUNDRY_sub_message--'  . $eol;

        if( file_exists($imageFile) ){
	        $fp = fopen($imageFile, "rb");
	            $fdata = fread($fp, filesize($imageFile));
	            $fbase64 = chunk_split(base64_encode($fdata));
	        fclose($fp);
	        $this->_body    .= '------MIME_BOUNDRY_main_message' . $eol;
	        $this->_body    .= 'Content-Type: '.image_type_to_mime_type(exif_imagetype($imageFile)).'; name="recipe.jpg"' .$eol;
	        $this->_body    .= 'Content-Transfer-Encoding: base64' . $eol;
	        $this->_body    .= 'Content-ID: <recipe@recipeshelf>' . $eol;
	        $this->_body    .= 'Content-Disposition: inline; filename="recipe.jpg"' . $eol;
	        $this->_body    .= $eol . $fbase64;
		}
	}

	function send(){
		$r = @mail( $this->_email, $this->_subject, $this->_body, $this->_header );
        return $r;
	}

}

?>