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

Namespaces

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

Classes

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