Location: PHPKode > projects > MembersGear > membersgear/includes/autoresponder.php
<?php
    require_once CONFIG_DIR."/config.php";
    require_once "users_class.php";
    require_once "product_class.php";
    require_once "cannedemail_class.php";
    require_once "emaillog_class.php";
    require_once "phpmailer/Functions.php";

        
    function SendCannedEmailsAutoresponse($pEmail_tempalte_id,$pProductID,$pEmailFor="",$pProductName="",$pProductURL="",$pUsername,$pPassword,$pEmail,$pFirstname,$pLastname)
    {
        $loCannedEmail = new cannedemail_class();
        $loUserClass   = new users_class();
        $loEmailLog   = new emaillog_class();
        
        $query = 'SELECT * FROM '.TABLE_PREFIX.'email_templates WHERE id=? AND productid=? AND status = 1';
        
        $lsValues = array($pEmail_tempalte_id,$pProductID);
        
        if($pEmailFor != '')
        {
            $query .= ' AND name=?';
            $lsValues[] = $pEmailFor;
        }
        
        
        $laResultData = $loCannedEmail->View($query,$lsValues,null);    
        /*
        $laResultData[0]['admin_email_message'] = ReplaceCannedEmailTemplateContents($laResultData[0]['admin_email_message'],$pUsername,$pPassword,$pEmail,$pFirstname,$pLastname);
        $laResultData[0]['email_message']  = ReplaceCannedEmailTemplateContents($laResultData[0]['email_message'],$pUsername,$pPassword,$pEmail,$pFirstname,$pLastname);
            
        sendCannedEmails($laResultData);
        */ 
        if($laResultData[0]['sendmessageto'] == 'Both') 
        {
            $laResultData[0]['admin_email_message'] = ReplaceCannedEmailTemplateContents($laResultData[0]['admin_email_message'],$laResultData[0]['admin_email_format'],$pUsername,$pPassword,$pEmail,$pFirstname,$pLastname,$pProductName,$pProductURL);
            $laResultData[0]['email_message']  = ReplaceCannedEmailTemplateContents($laResultData[0]['email_message'],$laResultData[0]['email_format'],$pUsername,$pPassword,$pEmail,$pFirstname,$pLastname,$pProductName,$pProductURL);
            
            $laResultData[0]['email_subject']=ReplaceCannedEmailTemplateSubject($laResultData[0]['email_subject'],$pFirstname,$pLastname,$pUsername,$pProductName);
            $laResultData[0]['admin_email_subject']=ReplaceCannedEmailTemplateSubject($laResultData[0]['admin_email_subject'],$pFirstname,$pLastname,$pUsername,$pProductName);
            
            
            /**************************************** Send Email and Add Email LOG ***********************************************/
            if(sendCannedEmailsToAdmin($laResultData))
            {
                $UserID = $loUserClass->getUserId('administrator');
                   
                $emaillogQuery = "INSERT INTO ".TABLE_PREFIX."emails_log 
                                    (`email_template_id`,`productid`,`sendmessageto`,`sender_name`,`sender_email`,`email_format`,`email_subject`,`email_copy_to`,`email_message`,`email_attachment`,`admin_email_format`,`admin_email_subject`,`admin_email_message`,`createdon`,`createdby`,`createdipaddress`,`senttype`,`sentto_userid`) 
                                    VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                $laValues = array();
                $laValues[0] = $pEmail_tempalte_id;
                $laValues[1] = $pProductID;
                $laValues[2] = $laResultData[0]['sendmessageto'];
                $laValues[3] = ($laResultData[0]['sender_name'] != '') ? $laResultData[0]['sender_name'] : ' ';
                $laValues[4] = ($laResultData[0]['sender_email'] != '') ? $laResultData[0]['sender_email'] : ' ';    
                $laValues[5] = ($laResultData[0]['email_format'] != '') ? $laResultData[0]['email_format'] : ' ';
                $laValues[6] = ($laResultData[0]['email_subject'] != '') ? $laResultData[0]['email_subject'] : ' ';
                $laValues[7] = ($laResultData[0]['email_copy_to'] != '') ? $laResultData[0]['email_copy_to'] : ' ';
                $laValues[8] = ($laResultData[0]['email_message'] != '') ? $laResultData[0]['email_message'] : ' ';
                $laValues[9] = $laResultData[0]['email_attachment'];
                $laValues[10] = $laResultData[0]['admin_email_format'];
                $laValues[11] = $laResultData[0]['admin_email_subject'];
                $laValues[12] = $laResultData[0]['admin_email_message'];
                $laValues[13] = mktime();
                $laValues[14] = 0;//$_SESSION[SESSION_PREFIX.'Admin_UserID'];
                $laValues[15] = $_SERVER['REMOTE_ADDR'];
                
                if(($pEmailFor == 'Subscription about to expire' and EMAIL_SUBSCRIPTION_ABOUT_TO_EXPIRED_DAYS > 0) or ($pEmailFor == 'Subscription expired' and EMAIL_SUBSCRIPTION_IS_EXPIRED_DAYS > 0))
                {
                    $laValues[16] = 1;   
                }
                else
                {
                    $laValues[16] = 2;   
                }
                
                $laValues[17] = $UserID+0;
                
                $result = $loEmailLog->Insert($emaillogQuery,$laValues,null);
            }
            
            if(sendCannedEmailsToUsers($pEmail,$laResultData))
            {
                $UserID = $loUserClass->getUserId($pUsername);
                   
                $emaillogQuery = "INSERT INTO ".TABLE_PREFIX."emails_log 
                                    (`email_template_id`,`productid`,`sendmessageto`,`sender_name`,`sender_email`,`email_format`,`email_subject`,`email_copy_to`,`email_message`,`email_attachment`,`admin_email_format`,`admin_email_subject`,`admin_email_message`,`createdon`,`createdby`,`createdipaddress`,`senttype`,`sentto_userid`) 
                                    VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                $laValues = array();
                $laValues[0] = $pEmail_tempalte_id;
                $laValues[1] = $pProductID;
                $laValues[2] = $laResultData[0]['sendmessageto'];
                $laValues[3] = ($laResultData[0]['sender_name'] != '') ? $laResultData[0]['sender_name'] : ' ';  
                $laValues[4] = ($laResultData[0]['sender_email'] != '') ? $laResultData[0]['sender_email'] : ' '; 
                $laValues[5] = ($laResultData[0]['email_format'] != '') ? $laResultData[0]['email_format'] : ' ';
                $laValues[6] = ($laResultData[0]['email_subject'] != '') ? $laResultData[0]['email_subject'] : ' ';
                $laValues[7] = ($laResultData[0]['email_copy_to'] != '') ? $laResultData[0]['email_copy_to'] : ' ';
                $laValues[8] = ($laResultData[0]['email_message'] != '') ? $laResultData[0]['email_message'] : ' ';
                $laValues[9] = $laResultData[0]['email_attachment'];
                $laValues[10] = $laResultData[0]['admin_email_format'];
                $laValues[11] = $laResultData[0]['admin_email_subject'];
                $laValues[12] = $laResultData[0]['admin_email_message'];
                $laValues[13] = mktime();
                $laValues[14] = 0;//$_SESSION[SESSION_PREFIX.'Admin_UserID'];
                $laValues[15] = $_SERVER['REMOTE_ADDR'];
                
                if(($pEmailFor == 'Subscription about to expire' and EMAIL_SUBSCRIPTION_ABOUT_TO_EXPIRED_DAYS > 0) or ($pEmailFor == 'Subscription expired' and EMAIL_SUBSCRIPTION_IS_EXPIRED_DAYS > 0))
                {
                    $laValues[16] = 1;   
                }
                else
                {
                    $laValues[16] = 2;   
                }
                
                $laValues[17] = $UserID+0;
                
                $result = $loEmailLog->Insert($emaillogQuery,$laValues,null);
            }
            /***********************************************************************************************************/
            
            //sendMail(ADMINISTRATOR_EMAIL_ADDRESS,$laResultData[0]['sender_name'],$laResultData[0]['admin_email_subject'],$lsAdminMessage,'',false);
            //sendMail($pEmail,$laResultData[0]['sender_name'],$laResultData[0]['email_subject'],$lsAdminMessage,'',false);
            
        }
        elseif($laResultData[0]['sendmessageto'] == 'Admin')
        {
            $laResultData[0]['admin_email_message'] = ReplaceCannedEmailTemplateContents($laResultData[0]['admin_email_message'],$laResultData[0]['admin_email_format'],$pUsername,$pPassword,$pEmail,$pFirstname,$pLastname,$pProductName,$pProductURL);    
            
           
            $laResultData[0]['admin_email_subject']=ReplaceCannedEmailTemplateSubject($laResultData[0]['admin_email_subject'],$pFirstname,$pLastname,$pUsername,$pProductName); 
            //sendCannedEmailsToAdmin($laResultData);
            //sendMail($pEmail,$laResultData[0]['sender_name'],$laResultData[0]['admin_email_subject'],$lsAdminMessage,'',false);
            
            /*********************************************** Send Email and Add Email LOG ***********************************************************/
            if(sendCannedEmailsToAdmin($laResultData))
            {
                $UserID = $loUserClass->getUserId('administrator');
                   
                $emaillogQuery = "INSERT INTO ".TABLE_PREFIX."emails_log 
                                    (`email_template_id`,`productid`,`sendmessageto`,`sender_name`,`sender_email`,`email_format`,`email_subject`,`email_copy_to`,`email_message`,`email_attachment`,`admin_email_format`,`admin_email_subject`,`admin_email_message`,`createdon`,`createdby`,`createdipaddress`,`senttype`,`sentto_userid`) 
                                    VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                $laValues = array();
                $laValues[0] = $pEmail_tempalte_id;
                $laValues[1] = $pProductID;
                $laValues[2] = $laResultData[0]['sendmessageto'];
                $laValues[3] = ($laResultData[0]['sender_name'] != '') ? $laResultData[0]['sender_name'] : ' '; 
                $laValues[4] = ($laResultData[0]['sender_name'] != '') ? $laResultData[0]['sender_name'] : ' '; 
                $laValues[5] = ($laResultData[0]['email_format'] != '') ? $laResultData[0]['email_format'] : ' ';
                $laValues[6] = ($laResultData[0]['email_subject'] != '') ? $laResultData[0]['email_subject'] : ' ';
                $laValues[7] = ($laResultData[0]['email_copy_to'] != '') ? $laResultData[0]['email_copy_to'] : ' ';
                $laValues[8] = ($laResultData[0]['email_message'] != '') ? $laResultData[0]['email_message'] : ' ';
                $laValues[9] = $laResultData[0]['email_attachment'];
                $laValues[10] = $laResultData[0]['admin_email_format'];
                $laValues[11] = $laResultData[0]['admin_email_subject'];
                $laValues[12] = $laResultData[0]['admin_email_message'];
                $laValues[13] = mktime();
                $laValues[14] = 0;//$_SESSION[SESSION_PREFIX.'Admin_UserID'];
                $laValues[15] = $_SERVER['REMOTE_ADDR'];
                
                if(($pEmailFor == 'Subscription about to expire' and EMAIL_SUBSCRIPTION_ABOUT_TO_EXPIRED_DAYS > 0) or ($pEmailFor == 'Subscription expired' and EMAIL_SUBSCRIPTION_IS_EXPIRED_DAYS > 0))
                {
                    $laValues[16] = 1;   
                }
                else
                {
                    $laValues[16] = 2;   
                }
                
                $laValues[17] = $UserID+0;
                
                $result = $loEmailLog->Insert($emaillogQuery,$laValues,null);
            }
        }
        elseif($laResultData[0]['sendmessageto'] == 'User')
        {
            $laResultData[0]['email_message'] = ReplaceCannedEmailTemplateContents($laResultData[0]['email_message'],$laResultData[0]['email_format'],$pUsername,$pPassword,$pEmail,$pFirstname,$pLastname,$pProductName,$pProductURL);
            $laResultData[0]['email_subject']=ReplaceCannedEmailTemplateSubject($laResultData[0]['email_subject'],$pFirstname,$pLastname,$pUsername,$pProductName);
            
            //sendCannedEmailsToUsers($pEmail,$laResultData);
            //sendMail($pEmail,$laResultData[0]['sender_name'],$laResultData[0]['email_subject'],$lsAdminMessage,'',false);
            
            /*********************************************** Send Email and Add Email LOG ***********************************************************/
            if(sendCannedEmailsToUsers($pEmail,$laResultData))
            {
                $UserID = $loUserClass->getUserId($pUsername);
                   
                $emaillogQuery = "INSERT INTO ".TABLE_PREFIX."emails_log 
                                    (`email_template_id`,`productid`,`sendmessageto`,`sender_name`,`sender_email`,`email_format`,`email_subject`,`email_copy_to`,`email_message`,`email_attachment`,`admin_email_format`,`admin_email_subject`,`admin_email_message`,`createdon`,`createdby`,`createdipaddress`,`senttype`,`sentto_userid`) 
                                    VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                $laValues = array();
                $laValues[0] = $pEmail_tempalte_id;
                $laValues[1] = $pProductID;
                $laValues[2] = $laResultData[0]['sendmessageto'];
                $laValues[3] = ($laResultData[0]['sender_name'] != '') ? $laResultData[0]['sender_name'] : ' '; 
                $laValues[4] = ($laResultData[0]['sender_name'] != '') ? $laResultData[0]['sender_name'] : ' ';
                $laValues[5] = ($laResultData[0]['email_format'] != '') ? $laResultData[0]['email_format'] : ' ';
                $laValues[6] = ($laResultData[0]['email_subject'] != '') ? $laResultData[0]['email_subject'] : ' ';
                $laValues[7] = ($laResultData[0]['email_copy_to'] != '') ? $laResultData[0]['email_copy_to'] : ' ';
                $laValues[8] = ($laResultData[0]['email_message'] != '') ? $laResultData[0]['email_message'] : ' ';
                $laValues[9] = $laResultData[0]['email_attachment'];
                $laValues[10] = $laResultData[0]['admin_email_format'];
                $laValues[11] = $laResultData[0]['admin_email_subject'];
                $laValues[12] = $laResultData[0]['admin_email_message'];
                $laValues[13] = mktime();
                $laValues[14] = 0;//$_SESSION[SESSION_PREFIX.'Admin_UserID'];
                $laValues[15] = $_SERVER['REMOTE_ADDR'];
                
                if(($pEmailFor == 'Subscription about to expire' and EMAIL_SUBSCRIPTION_ABOUT_TO_EXPIRED_DAYS > 0) or ($pEmailFor == 'Subscription expired' and EMAIL_SUBSCRIPTION_IS_EXPIRED_DAYS > 0))
                {
                    $laValues[16] = 1;   
                }
                else
                {
                    $laValues[16] = 2;   
                }
                
                $laValues[17] = $UserID+0;
                
                $result = $loEmailLog->Insert($emaillogQuery,$laValues,null);
            } 
        }
        
    }
    
    function ReplaceCannedEmailTemplateContents($pEmailMessage,$pEmailFormat,$pUsername,$pPassword,$pEmail,$pFirstname,$pLastname,$pProductName,$pProductURL)
    {
            $loUserClass = new users_class();
                              
            $validateuser_key='';
            $UserID = $loUserClass->getUserId($pUsername);
            $userID_enc=encrypt($UserID,$validateuser_key);             
                            
            $unsubscribe_key="";
            $key = $loUserClass->getSecretsalt($pUsername);
            $usr=encrypt($pUsername,$unsubscribe_key);
            
            $pEmailMessage = str_replace('[username]',$pUsername,$pEmailMessage);
            $pEmailMessage = str_replace('[password]',decrypt($pPassword,$key),$pEmailMessage);
            $pEmailMessage = str_replace('[email]',$pEmail,$pEmailMessage);
            $pEmailMessage = str_replace('[firstname]',$pFirstname,$pEmailMessage);
            $pEmailMessage = str_replace('[lastname]',$pLastname,$pEmailMessage);
            $pEmailMessage = str_replace('[productname]',$pProductName,$pEmailMessage);
            $pEmailMessage = str_replace('[product_url]',WEB_URL.$pProductURL,$pEmailMessage);
            $pEmailMessage = str_replace('[unsubscribe_link]','<a href="'.WEB_URL."/member/user_unsubscribe_mailing.php?secSalt=".$usr.'">Click here to unsubscribe</a>',$pEmailMessage);
            $pEmailMessage = str_replace('[today_date]',date("Y-m-d"),$pEmailMessage);
            $pEmailMessage = str_replace('[today_time]',date("h:i:s A"),$pEmailMessage);
            $pEmailMessage = str_replace('[today_datetime]',date("Y-m-d h:i:s A"),$pEmailMessage);
            $pEmailMessage=  str_replace('[validateuser_link]','<a href="'.WEB_URL."/user_validate.php?secSalt=".$userID_enc.'">Click here to Validate Your Account</a>',$pEmailMessage);
            //$pEmailMessage = str_replace('<br/>','',$pEmailMessage);
            if($pEmailFormat == 'Text')
            {    
                $pEmailMessage = str_replace('<br />',"\r\n",$pEmailMessage);
            }
            else
            {
                $pEmailMessage = nl2br($pEmailMessage);
            }
            //$pEmailMessage = str_replace('<br />',"\n",$pEmailMessage);
            
            return $pEmailMessage;
    }
    
    
    function ReplaceCannedEmailTemplateSubject($pEmailSubject,$pFirstname,$pLastname,$pUsername,$pProductname)
    {
            $pEmailSubject = str_replace('[firstname]',$pFirstname,$pEmailSubject);
            $pEmailSubject = str_replace('[lastname]',$pLastname,$pEmailSubject);
            $pEmailSubject = str_replace('[username]',$pUsername,$pEmailSubject);
            $pEmailSubject = str_replace('[productname]',$pProductname,$pEmailSubject);
            
            return $pEmailSubject;
    }
    
    function IsUserValidForEmail($pProductID, $pUserId, $pMailFor)
    {

        $loProductClass = new product_class();
        $loUserClass = new users_class();
        
        $result = true;

        $productQuery = 'select pp.id as `ProductID`,pet.id as `TemplateID`,pp.singupemail,pp.expirationemail,pet.status from '.TABLE_PREFIX.'products pp
                         join '.TABLE_PREFIX.'email_templates pet on pet.productid = pp.id
                         where pp.id =? and pet.name=?';
        $lsProductValues = array($pProductID,$pMailFor);
        $laQueryResult = $loProductClass->View($productQuery,$lsProductValues,null);
        
        if(($laQueryResult[0]['singupemail'] == 'DONOTSEND' or $laQueryResult[0]['status'] != 1) and $pMailFor == 'Product signup completed')
        {
            $result = false;
            return $result;
        }
        elseif(($laQueryResult[0]['expirationemail'] == 'DONOTSEND' or $laQueryResult[0]['status'] != 1) and $pMailFor == 'Subscription expired')
        {
            $result = false;
            return $result;
        }

        $userQuery = 'select isunsubscribedfromemails from '.TABLE_PREFIX.'users where id = ? and isapproved=1 and islockedout=0';
        $lsUserValues = array($pUserId);
        
        $laUserQueryResult = $loUserClass->View($userQuery,$lsUserValues,null);
        
        if($laUserQueryResult[0]['isunsubscribedfromemails'] != 0)
        {
            $result = false;
            return $result;
        }

        return $result ;
    }
           
    /* Added By Asmita to ManuallySend Email Without selecting Template*/        
    function SendEmailsAutoresponse($pTemplateid,$pEmaildata,$pContent,$pFrom='',$pTo,$pSubject='',$pCC='',$pBcc='',$pAttachment=null,$pProductID='',$pEmailFor="",$pProductName="",$pProductURL="",$pUsername,$pPassword,$pEmail,$pFirstname,$pLastname)
    {
        $loCannedEmail = new cannedemail_class();
        $loUserClass   = new users_class();
        $loEmailLog   = new emaillog_class();
        
        
        
      
      $UserID = $loUserClass->getUserId($pUsername);
      
      $pEmaildata[0]['email_message'] = ReplaceCannedEmailTemplateContents($pContent,$pEmaildata[0]['email_format'],$pUsername,$pPassword,$pEmail,$pFirstname,$pLastname,$pProductName,$pProductURL);
      $pEmaildata[0]['email_subject'] =$pSubject;
      $pEmaildata[0]['email_subject']=ReplaceCannedEmailTemplateSubject($pEmaildata[0]['email_subject'],$pFirstname,$pLastname,$pUsername,$pProductName);
            
    //  $pEmaildata[0]['email_message'] =$pSubject; 
      $pEmaildata[0]['email_attachment']=$pAttachment;
      $pEmaildata[0]['sendmessageto']="User";  
      if($pFrom!='')
      {
         $pEmaildata[0]['sender_name']=$pFrom; 
         $pEmaildata[0]['sender_email']=$pFrom ;
      } 
            //sendCannedEmailsToUsers($pEmail,$laResultData);
            //sendMail($pEmail,$laResultData[0]['sender_name'],$laResultData[0]['email_subject'],$lsAdminMessage,'',false);
            
            /*********************************************** Send Email and Add Email LOG ***********************************************************/
            if(sendCannedEmailsToUsers($pEmail,$pEmaildata))
            {
                $UserID = $loUserClass->getUserId($pUsername);
                   
                $emaillogQuery = "INSERT INTO ".TABLE_PREFIX."emails_log 
                                    (`email_template_id`,`productid`,`sendmessageto`,`sender_name`,`sender_email`,`email_format`,`email_subject`,`email_copy_to`,`email_message`,`email_attachment`,`admin_email_format`,`admin_email_subject`,`admin_email_message`,`createdon`,`createdby`,`createdipaddress`,`senttype`,`sentto_userid`) 
                                    VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                $laValues = array();
                $laValues[0] = $pTemplateid;
                $laValues[1] = $pProductID;
                $laValues[2] = $pEmaildata[0]['sendmessageto'];
                $laValues[3] =  ($pEmaildata[0]['sender_name'] != '') ? $pEmaildata[0]['sender_name'] : ' ';  
                $laValues[4] = ($pEmaildata[0]['sender_name'] != '') ? $pEmaildata[0]['sender_name'] : ' '; 
                $laValues[5] = ($pEmaildata[0]['email_format'] != '') ?  $pEmaildata[0]['email_format'] : ' ';
                $laValues[6] = ($pEmaildata[0]['email_subject'] != '') ? $pEmaildata[0]['email_subject'] : ' ';
                $laValues[7] = ($pEmaildata[0]['email_copy_to'] != '') ? $pEmaildata[0]['email_copy_to'] : ' ';
                $laValues[8] = ($pEmaildata[0]['email_message'] != '') ? $pEmaildata[0]['email_message'] : ' ';
                $laValues[9] = $pEmaildata[0]['email_attachment'];
                $laValues[10] =$pEmaildata[0]['admin_email_format'];
                $laValues[11] =$pEmaildata[0]['admin_email_subject'];
                $laValues[12] =$pEmaildata[0]['admin_email_message'];
                $laValues[13] = mktime();
                $laValues[14] = 0;//$_SESSION[SESSION_PREFIX.'Admin_UserID'];
                $laValues[15] = $_SERVER['REMOTE_ADDR'];
                
                if(($pEmailFor == 'Subscription about to expire' and EMAIL_SUBSCRIPTION_ABOUT_TO_EXPIRED_DAYS > 0) or ($pEmailFor == 'Subscription expired' and EMAIL_SUBSCRIPTION_IS_EXPIRED_DAYS > 0))
                {
                    $laValues[16] = 1;   
                }
                else
                {
                    $laValues[16] = 2;   
                }
                
                $laValues[17] = $UserID+0;
                
                $result = $loEmailLog->Insert($emaillogQuery,$laValues,null);
            } 
        
        
    }
    
    //SendCannedEmailsAutoresponse(51,0,'','','','jayesh','123456','hide@address.com','Jayesh','Patel');
    //sendMail('hide@address.com','test','Testing','<h1>Hello World</h1>','',false);
?>
Return current item: MembersGear