Advertisement
Guest User

Untitled

a guest
Jun 16th, 2017
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.47 KB | None | 0 0
  1. <?php
  2.  
  3. require_once 'lib/utils/Swift/swift_required.php';
  4.  
  5. class Mailer {
  6.     protected $from;
  7.     protected $to;
  8.     protected $subject;
  9.     protected $attachments = array();
  10.     protected $views = array();
  11.     protected $data = array();
  12.     protected $layout = false;
  13.    
  14.     public function __construct($data = array()) {
  15.         foreach($data as $key => $value):
  16.             $this->{$key} = $value;
  17.         endforeach;
  18.     }
  19.     public function transport() {
  20.         switch(Config::read('Mailer.transport')):
  21.             case 'mail':
  22.                 $transport = Swift_MailTransport::newInstance();
  23.                 break;
  24.             case 'smtp':
  25.                 $host = Config::read('Mailer.smtp.host');
  26.                 $port = Config::read('Mailer.smtp.port');
  27.                 $encryption = Config::read('Mailer.smtp.encryption');
  28.                 $transport = Swift_SmtpTransport::newInstance($host, $port, $encryption);
  29.                
  30.                 if(Config::read('Mailer.smtp.username')):
  31.                     $username = Config::read('Mailer.smtp.username');
  32.                     $password = Config::read('Mailer.smtp.password');
  33.                     $transport->setUsername($username)->setPassword($password);
  34.                 endif;
  35.         endswitch;
  36.        
  37.         return $transport;
  38.     }
  39.     public function message() {
  40.         $message = Swift_Message::newInstance($this->subject);
  41.         $message->setFrom($this->from);
  42.         $message->setTo($this->to);
  43.        
  44.         $this->render($message);
  45.        
  46.         if(!empty($this->attachments)):
  47.             $this->attachFiles($message);
  48.         endif;
  49.        
  50.         return $message;
  51.     }
  52.     public function attachFiles($message) {
  53.         foreach($this->attachments as $name => $file):
  54.             $attachment = Swift_Attachment::fromPath($file);
  55.            
  56.             if(!is_numeric($name)):
  57.                 $attachment->setFilename($name);
  58.             endif;
  59.            
  60.             $message->attach($attachment);
  61.         endforeach;
  62.     }
  63.     public function render($message) {
  64.         $view = new View();
  65.                 $view->data = $this->data;
  66.         foreach($this->views as $type => $path):
  67.             $content = $view->render($path, $this->layout);
  68.             $message->addPart($content, $type);
  69.         endforeach;
  70.     }
  71.     public function send() {
  72.         $mailer = Swift_Mailer::newInstance($this->transport());
  73.         return $mailer->send($this->message());
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement