<?php namespace App\Controllers;

use CodeIgniter\Controller;
use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
use \App\Andy\CheckSpam;
use \App\Andy\Utility;
use CodeIgniter\I18n\Time;



class Sendmail extends BaseController
{
       
   protected $totalMessage;    
  
  
  public function __construct() 
						{
							parent::__construct();
							$this->myTime = parent::getTime();
							$this->myDate= date("d/m/Y",$this->myTime); 	
						
					   }
  
  
  
  
   public function processform()
	
	{
		
	//	$lucky= $this->request->getVar(csrf_token());
	//above checks csrf is working 	
		
     		
	
	$cleanName= htmlentities( $this->request->getVar('name')); 
	$cleanEmail=  htmlentities($this->request->getVar('email'));
	$message= $this->request->getVar('message');
	$spamHandle = new CheckSpam();
	$utilityHandle= new Utility();
	//CheckSpam is in app/Andy and has namespace App\Andy 
	$logic = $spamHandle->filterSpam($message);
	//before bothering to remove char check if spam 	
					if ($logic =="true")
		{
		 $data = [
						 'title'=>'spam',
						 'date'=>$this->myDate
						  
						 
						 ];
		
		
		
		echo view('spam',$data);
		
				
		
		}
	
	       else
	       {
			   
			   //start of else bracket
	
	
	$cleanMessage= $utilityHandle->removeScript($message);
	$cleanMessage= $utilityHandle->convertQuotes($cleanMessage);
	$cleanMessage = $utilityHandle->newRegex($cleanMessage);
	
	
	$IP= $_SERVER['REMOTE_ADDR'];
	
	$theirIP= strval($IP);
	$ip= $this->request->getIPAddress();
	$this->totalMessage = "new enquiry  from a lovely web   \r\n ".$cleanName ." \r\n their ip from native php is    :".$theirIP."\r\n from CI4 get request its  ".$ip."\r\n their email is:  ".$cleanEmail." \r\n their message:  ".$cleanMessage;
	
	               
	                  $mail = new PHPMailer(); 
	                        $mail->isSMTP();
							$mail->Timeout = 20;
							
							$mail->SMTPDebug = 0;
						
							$mail->Host = 'smtp.gmail.com';
							$mail->Port = 587;
							$mail->SMTPSecure = 'tls';        
							$mail->SMTPAuth = true;
					
							$mail->Username = "youremail@gmail.com";
							//Password to use for SMTP authentication
							$mail->Password = "password";
							//Set who the message is to be sent from
							$mail->setFrom('admin@webnameYouWant.com', 'admin');
							$mail->addAddress('AddressWhereYouWantMessageSent@gmail.com', ' ');
						
							
							$mail->Subject = 'new enquiry from your web name    ';
							$mail->Body = $this->totalMessage;
							
							
							
						$logic2 = $result=	$mail->send();
	                    
	                                     if (($logic2==1) OR ($logic2=="true"))
					     {
					      $data = ['title' => 'darth',
					      'info'    => 'your message was sent ',
					      'date'=>$this->myDate
					      
					      
					      
					      
					      
					      ];
					     
					    
					     echo view ('info',$data);
					    
					     }
					     
		}//end of else bracket 
	
	
	}//end of process form method 
	
	
	
	
	
   
	
	
	
	
	
	
	
	
	
}
