<?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()
	
	{
	//$myTime = new Time('now');
	//$date=	$myTime->toLocalizedString('MMM d, yyyy');   	
	$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 queck 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 AndrinDesign    \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;
							//Enable SMTP debugging, 0 = off (for production use),  1 = client messages,  2 = client and server messages
							$mail->SMTPDebug = 0;
							//Set the hostname of the mail server
							$mail->Host = 'smtp.gmail.com';
							$mail->Port = 587;
							$mail->SMTPSecure = 'tls';        
							$mail->SMTPAuth = true;
							//Username to use for SMTP authentication - use full email address for gmail
							$mail->Username = "*******@gmail.com";
							//Password to use for SMTP authentication
							$mail->Password = "***********";
							//Set who the message is to be sent from
							$mail->setFrom('admin@yourdomain', 'john Doe');
							$mail->addAddress('emailAddressWhereMeesageShouldBeSent', 'John Doe');
							
							
							
							//Set the subject line
							$mail->Subject = 'new enquiry from web app starter girlsbeyourself    ';
							
							$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 
	
	
	}
	
	
	
	
	
   
	
	
	
	
	
	
	
	
	
}
