1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace GLFramework;
10:
11: use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
12:
13: class Mail
14: {
15:
16: private static $mailer;
17: private function getCss($cssFiles = array())
18: {
19: $css = "";
20: foreach($cssFiles as $file)
21: {
22: if(file_exists($file)) $css .= file_get_contents($file) . "\n";
23: }
24: }
25:
26: 27: 28: 29: 30: 31: 32: 33:
34: public function render($controller, $template, $data)
35: {
36: $view = new View($controller);
37: $css = array();
38: $html = $view->mail($template, $data, $css);
39: $cssToInlineStyles = new CssToInlineStyles();
40: $cssToInlineStyles->setCSS($this->getCss($css));
41: $cssToInlineStyles->setHTML($html);
42: return $cssToInlineStyles->convert();
43: }
44:
45: 46: 47:
48: private function getMailSystem()
49: {
50: $config = Bootstrap::getSingleton()->getConfig();
51: if(isset($config['mail']))
52: {
53: if(isset($config['mail']['mailsystem']))
54: {
55: $system = $config['mail']['mailsystem'];
56: $class = "GLPlanning/Mail/$system";
57: if(class_exists($class)) return new $class($config);
58: }
59: }
60: return new Mail\Mail($config);
61: }
62:
63: 64: 65: 66:
67: public function getTransport()
68: {
69: if(!self::$mailer)
70: {
71: self::$mailer = $this->getMailSystem()->getTransport();
72: Events::fire('onMailTransport', array( 'transport' => self::$mailer ));
73: }
74: return self::$mailer;
75: }
76:
77: 78: 79: 80: 81: 82: 83: 84:
85: public function send($to, $subject, $message, $attachments = array())
86: {
87: $config = Bootstrap::getSingleton()->getConfig();
88: $mailsystem = $this->getMailSystem();
89: $transport = $mailsystem->getTransport();
90: Events::fire('onEmail', array( 'emails' => $to, 'subject' => $subject, 'message' => $message, 'transport' => $transport));
91:
92: $mail = new \Swift_Message($subject, $message, "text/html", "UTF-8");
93: $mail->setFrom($config['mail']['from']['email'], $config['mail']['from']['title']);
94: $mail->setTo($to);
95: foreach ($attachments as $key => $attachment)
96: {
97: $atta = \Swift_Attachment::fromPath($attachment);
98: if(!is_int($key)) $atta->setFilename($key);
99: $mail->attach($atta);
100: }
101:
102: return $transport->send($mail);
103:
104: }
105: }