Skip to content
Snippets Groups Projects
Select Git revision
  • 2650ddbddfe7f767b840d7fc09de225d9639d445
  • master default
  • rename_category_group
  • php8.1_deprecations
  • v1.12.1
  • v1.13
  • v1.12
  • v1.11
  • v1.10
  • v1.9
  • v1.8
  • v1.7
  • v1.6
  • v1.5
  • v1.4
  • v1.3
  • v1.2
  • v1.1
  • v1.0
19 results

MailOnEventListener.php

Blame
  • user avatar
    Carsten Karbach authored
    2650ddbd
    History
    MailOnEventListener.php 1.40 KiB
    <?php
    
    namespace jards\eventsapi;
    
    use PHPMailer\PHPMailer\PHPMailer;
    
    /**
     * Handler, which sends a mail for every event
     *
     */
    class MailOnEventListener implements EventListener{
    	
    	/**
    	 * Send a mail using PHPMailer
    	 * @param string $to mail address to send to 
    	 * @param string $subject the subject of the mail
    	 * @param string $message the body of the mail
    	 */
    	public function sendmail($to, $subject, $message){
    		$config = parse_ini_file(__DIR__.'/../configs/mail.cnf');
    	
    		$mail=new PHPMailer();
    		$mail->CharSet = 'UTF-8';
    	
    		$mail->IsSMTP();
    		$mail->Host       = $config['Host'];
    	
    		$mail->SMTPSecure = $config['SMTPSecure'];
    		$mail->Port       = intval($config['Port']);
    		$mail->SMTPDebug  = 0;
    		$mail->SMTPAuth   = true;
    	
    		if($config['verify_peer']==false){
    			$mail->SMTPOptions = array(
    					'ssl' => array(
    							'verify_peer' => false
    					)
    			);
    		}
    	
    		$mail->Username   = $config['Username'];
    		$mail->Password   = $config['Password'];
    	
    		$mail->SetFrom($config['Username'], 'jardsserver');
    		$mail->Subject    = $subject;
    		$mail->MsgHTML($message);
    	
    		$mail->AddAddress($to, $to);
    	
    		$mail->send();
    	}
    	
    	
    	/**
    	 * 
    	 * @param unknown $event
    	 */
    	public function handleEvent($event){
    		$config = parse_ini_file(__DIR__.'/../configs/mail.cnf');
    		$this->sendmail($config['TARGET'], 'New event received', json_encode($event));
    	}
    }
    
    ?>