1: <?php
2:
3: namespace Alpha\Util\Email;
4:
5: use Alpha\Exception\MailNotSentException;
6: use Alpha\Exception\PHPException;
7: use Alpha\Util\Logging\Logger;
8: use Alpha\Util\Config\ConfigProvider;
9:
10: /**
11: * Sends an email using the mb_send_mail() function from PHP.
12: *
13: * @since 2.0
14: *
15: * @author John Collins <dev@alphaframework.org>
16: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
17: * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
18: * All rights reserved.
19: *
20: * <pre>
21: * Redistribution and use in source and binary forms, with or
22: * without modification, are permitted provided that the
23: * following conditions are met:
24: *
25: * * Redistributions of source code must retain the above
26: * copyright notice, this list of conditions and the
27: * following disclaimer.
28: * * Redistributions in binary form must reproduce the above
29: * copyright notice, this list of conditions and the
30: * following disclaimer in the documentation and/or other
31: * materials provided with the distribution.
32: * * Neither the name of the Alpha Framework nor the names
33: * of its contributors may be used to endorse or promote
34: * products derived from this software without specific
35: * prior written permission.
36: *
37: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
42: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
48: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
49: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50: * </pre>
51: */
52: class EmailProviderPHP implements EmailProviderInterface
53: {
54: /**
55: * Trace logger.
56: *
57: * @var Alpha\Util\Logging\Logger
58: *
59: * @since 2.0
60: */
61: private static $logger = null;
62:
63: /**
64: * Constructor.
65: *
66: * @since 2.0
67: */
68: public function __construct()
69: {
70: self::$logger = new Logger('EmailProviderPHP');
71: }
72:
73: /**
74: * {@inheritdoc}
75: */
76: public function send($to, $from, $subject, $body, $isHTML = false)
77: {
78: self::$logger->debug('>>send(to=['.$to.'], from=['.$from.'], subject=['.$subject.'], body=['.$body.'], isHTML=['.$isHTML.'])');
79:
80: $config = ConfigProvider::getInstance();
81:
82: $headers = 'MIME-Version: 1.0'."\n";
83: if ($isHTML) {
84: $headers .= 'Content-type: text/html; charset=iso-8859-1'."\n";
85: }
86: $headers .= 'From: '.$from."\n";
87:
88: if ($config->getEnvironment() != 'dev') {
89: try {
90: mb_send_mail($to, $subject, $body, $headers);
91: } catch (PHPException $e) {
92: throw new MailNotSentException('Error sending a mail to ['.$to.']');
93: }
94: } else {
95: self::$logger->info("Sending email:\n".$headers."\n".$body);
96: }
97:
98: self::$logger->debug('<<send');
99: }
100: }
101: