AloFramework documentation
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo

Namespaces

  • Alo
    • Cache
    • CLI
    • Controller
    • Db
    • Exception
    • FileSystem
    • Session
    • Traversables
    • Validators
    • Windows
  • Controller
  • None
  • PHP

Classes

  • Cookie
  • Cron
  • Curl
  • Email
  • Format
  • Handler
  • Locale
  • Profiler
  • Security
  • SFTP
  1 <?php
  2 
  3     namespace Alo;
  4 
  5     use PHPMailer;
  6 
  7     if (!defined('GEN_START')) {
  8         http_response_code(404);
  9     } else {
 10 
 11         require_once DIR_SYS . 'external' . DIRECTORY_SEPARATOR . 'email' . DIRECTORY_SEPARATOR . 'class.phpmailer.php';
 12         require_once DIR_SYS . 'external' . DIRECTORY_SEPARATOR . 'email' . DIRECTORY_SEPARATOR .
 13                      'PHPMailerAutoload.php';
 14 
 15         \Alo::loadConfig('email');
 16 
 17         /**
 18          * Mail wrapper for the external PHPMailer library
 19          *
 20          * @author Art <a.molcanovas@gmail.com>
 21          * @link   https://github.com/PHPMailer/PHPMailer
 22          */
 23         class Email extends PHPMailer {
 24 
 25             /**
 26              * Static reference to the last instance of the class
 27              *
 28              * @var Email
 29              */
 30             static $this;
 31 
 32             /**
 33              * Array of debug outputs, each send operation representing a key/value pair
 34              *
 35              * @var array
 36              */
 37             protected $debugOutput;
 38 
 39             /**
 40              * Array of content attachments to clean afterwards
 41              *
 42              * @var array
 43              */
 44             protected $attachedContent;
 45 
 46             /**
 47              * Instantiates the class
 48              *
 49              * @author Art <a.molcanovas@gmail.com>
 50              *
 51              * @param boolean $exceptions Should we throw external exceptions?
 52              */
 53             function __construct($exceptions = false) {
 54                 parent::__construct($exceptions);
 55 
 56                 if (ALO_EMAIL_ERR_LANG != 'en') {
 57                     $this->setLanguage(ALO_EMAIL_ERR_LANG);
 58                 }
 59 
 60                 $this->isSMTP(ALO_EMAIL_USE_SMTP);
 61                 $this->Host       = ALO_EMAIL_HOSTS;
 62                 $this->SMTPAuth   = ALO_EMAIL_AUTH;
 63                 $this->Username   = ALO_EMAIL_USERNAME;
 64                 $this->Password   = ALO_EMAIL_PASSWORD;
 65                 $this->SMTPSecure = ALO_EMAIL_SECURE;
 66                 $this->Port       = ALO_EMAIL_PORT;
 67                 $this->From       = ALO_EMAIL_FROM_DEFAULT_ADDR;
 68                 $this->FromName   = ALO_EMAIL_FROM_DEFAULT_NAME;
 69                 $this->Subject    = ALO_EMAIL_SUBJECT_DEFAULT;
 70                 $this->isHTML(ALO_EMAIL_HTML_ENABLED);
 71 
 72                 self::$this = &$this;
 73             }
 74 
 75             /**
 76              * Instantiates the class
 77              *
 78              * @author Art <a.molcanovas@gmail.com>
 79              *
 80              * @param boolean $exceptions Should we throw external exceptions?
 81              *
 82              * @return Email
 83              */
 84             static function email($exceptions = false) {
 85                 return new Email($exceptions);
 86             }
 87 
 88             /**
 89              * Checks if the supplied string is an email
 90              *
 91              * @author Art <a.molcanovas@gmail.com>
 92              *
 93              * @param string $str The input
 94              *
 95              * @return boolean
 96              */
 97             static function isEmailAddress($str) {
 98                 if (!is_string(($str))) {
 99                     return false;
100                 } else {
101                     return preg_match('/^[a-z\.\-_0-9]+@[a-z\.\-_0-9]+\.[a-z]{2,3}$/is', $str) == 1;
102                 }
103             }
104 
105             /**
106              * Destructor. Performs cleanup operations
107              *
108              * @author Art <a.molcanovas@gmail.com>
109              */
110             function __destruct() {
111                 $this->cleanup();
112                 parent::__destruct();
113             }
114 
115             /**
116              * Cleans up attached content
117              *
118              * @author Art <a.molcanovas@gmail.com>
119              * @return Email
120              */
121             function cleanup() {
122                 if (!empty($this->attachedContent)) {
123                     foreach ($this->attachedContent as $file) {
124                         if (file_exists($file)) {
125                             unlink($file);
126                         }
127                     }
128 
129                     $this->attachedContent = [];
130                 }
131 
132                 return $this;
133             }
134 
135             /**
136              * Adds a recipient address
137              *
138              * @author Art <a.molcanovas@gmail.com>
139              *
140              * @param string $address The address to add
141              * @param string $name    Optionally, the recipient's name
142              *
143              * @return Email
144              * @throws \phpmailerException
145              */
146             function addAddress($address, $name = '') {
147                 parent::addAddress($address, $name);
148 
149                 return $this;
150             }
151 
152             /**
153              * Adds a BCC address
154              *
155              * @author Art <a.molcanovas@gmail.com>
156              *
157              * @param string $address The address
158              * @param string $name    Their name
159              *
160              * @throws \phpmailerException
161              * @return Email
162              */
163             function addBCC($address, $name = '') {
164                 parent::addBCC($address, $name);
165 
166                 return $this;
167             }
168 
169             /**
170              * Adds a CC address
171              *
172              * @author Art <a.molcanovas@gmail.com>
173              *
174              * @param string $address The address
175              * @param string $name    Their name
176              *
177              * @return Email
178              * @throws \phpmailerException
179              */
180             function addCC($address, $name = '') {
181                 parent::addCC($address, $name);
182 
183                 return $this;
184             }
185 
186             /**
187              * Adds reply-to data
188              *
189              * @author Art <a.molcanovas@gmail.com>
190              *
191              * @param string $to   Reply-to address
192              * @param string $name Reply-to name
193              *
194              * @return Email
195              * @throws \phpmailerException
196              */
197             function addReplyTo($to, $name = '') {
198                 parent::addReplyTo($to, $name);
199 
200                 return $this;
201             }
202 
203             /**
204              * Create a message and send it.
205              *
206              * @author Art <a.molcanovas@gmail.com>
207              * @throws \phpmailerException
208              * @return boolean false on error - See the ErrorInfo property for details of the error.
209              */
210             function send() {
211                 ob_start();
212                 $send                = parent::send();
213                 $this->debugOutput[] = ob_get_clean();
214 
215                 return $send;
216             }
217 
218             /**
219              * Attempts to attach not a file from the disk, but generated contents
220              *
221              * @author Art <a.molcanovas@gmail.com>
222              *
223              * @param string $name    The attachment filename
224              * @param string $content The contents
225              *
226              * @return bool
227              * @throws \Exception
228              * @throws \phpmailerException
229              */
230             function attachContent($name, $content) {
231                 $destFilename = Security::getUniqid('md5', 'email_attachment');
232                 $dest         = DIR_TMP . $destFilename;
233 
234                 if (file_exists($dest)) {
235                     //try again
236                     return $this->attachContent($name, $content);
237                 } elseif (file_put_contents($dest, $content) !== false) {
238                     $this->attachedContent[] = $dest;
239 
240                     return $this->addAttachment($dest, $name);
241                 } else {
242                     return false;
243                 }
244             }
245 
246             /**
247              * Returns the debug output from calls to $this->send()
248              *
249              * @author Art <a.molcanovas@gmail.com>
250              * @return array
251              */
252             function getDebugOutput() {
253                 return $this->debugOutput;
254             }
255         }
256     }
257 
AloFramework documentation API documentation generated byApiGen 2.8.0