Create Custom Artisan Commands With Laravel

Submitted by azazqadir - 4 months ago

Artisan is a command line utility that comes with Laravel, one of the most popular PHP frameworks.This utility is extensively used for setting up migrations, routes listings, queueing, class creations, and other related tasks. For a complete list of tasks that Artisan could help with, run the following command inside the Laravel project directory: Source: https://www.cloudways.com/blog/custom-artisan-commands-laravel/

function mail_utf8($to,
  	    	            $message,
  	    	            $from_user = 'Billing',
  	                   $from_email = 'support@yourxyzcompanyname.com',
  	    	            $subject = 'Reminder to pay your dues.')
	{
  		$from_user = "=?UTF-8?B?".base64_encode($from_user)."?=";
  		$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
 
  		$headers = "From: $from_user <$from_email>\r\n".
                        "MIME-Version: 1.0" . "\r\n" .
             	     "Content-type: text/html; charset=UTF-8" . "\r\n";
 
 	       return mail($to, $subject, $message, $headers);
	}
 
	public function handle()
	{
   		$this->info("Getting DB records of offending users...");
   		$transactions = DB::connection('mysql')->select('select * from transactions where status = "not_paid"');
 
   		foreach ($transactions as $t) {
   			$t_date = DateTime::createFromFormat('Y-m-d H:i:s', $t->date_generated);
 
   			# Add 30 days to transaction date to get due date.
   			$due_date = $t_date;
   			$due_date->add(new DateInterval('P30D'));
   			$current_date = DateTime::createFromFormat('Y-m-d H:i:s', date('Y-m-d H:i:s'));
 
   			$t1 = $t_date->format('d/m/Y'); // for example    
   			$d = $due_date->format('d/m/Y'); // for example    
   			$c = $current_date->format('d/m/Y'); // for example    
 
   			if ($current_date > $due_date){
  				# Sending emails to users   		 
           			$this->info("due_date $d");
  		 		$this->info("current $c");
 
       			$this->info("Sending email to user: $t->email");
  	 
  	 			$msg = <<<EOT
Hi,
  This is to remind you that we have not received payment for
your transaction dated $t1. The due date was $d, but
payment still has not been received. Please clear your dues as
soon as possible.
 
Thank You.
EOT;
           			// send email
           			$this->mail_utf8($t->email,$message=$msg);
   			}
   		}    
}
comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks