Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Mailer | |
0.00% |
0 / 22 |
|
0.00% |
0 / 3 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | ||||
| createMailer | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @section LICENSE |
| 4 | * This file is part of Wikimedia Slim application library |
| 5 | * |
| 6 | * Wikimedia Slim application library is free software: you can |
| 7 | * redistribute it and/or modify it under the terms of the GNU General Public |
| 8 | * License as published by the Free Software Foundation, either version 3 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * |
| 11 | * Wikimedia Slim application library is distributed in the hope that it |
| 12 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 13 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with Wikimedia Grants Review application. If not, see |
| 18 | * <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | * @file |
| 21 | * @copyright © 2015 Bryan Davis, Wikimedia Foundation and contributors. |
| 22 | */ |
| 23 | |
| 24 | namespace Wikimedia\Slimapp; |
| 25 | |
| 26 | use PHPMailer\PHPMailer\Exception; |
| 27 | use PHPMailer\PHPMailer\PHPMailer; |
| 28 | use Psr\Log\LoggerInterface; |
| 29 | use Psr\Log\NullLogger; |
| 30 | |
| 31 | /** |
| 32 | * Wrapper around PHPMailer |
| 33 | * |
| 34 | * @author Bryan Davis <bd808@wikimedia.org> |
| 35 | * @copyright © 2015 Bryan Davis, Wikimedia Foundation and contributors. |
| 36 | */ |
| 37 | class Mailer { |
| 38 | |
| 39 | /** |
| 40 | * @var LoggerInterface |
| 41 | */ |
| 42 | protected $logger; |
| 43 | |
| 44 | /** |
| 45 | * @var array |
| 46 | */ |
| 47 | protected $settings = [ |
| 48 | 'AllowEmpty' => false, |
| 49 | 'CharSet' => 'utf-8', |
| 50 | 'ContentType' => 'text/plain', |
| 51 | 'From' => 'grants@wikimedia.org', |
| 52 | 'FromName' => 'Wikimedia Grants', |
| 53 | 'Mailer' => 'smtp', |
| 54 | 'WordWrap' => 72, |
| 55 | 'XMailer' => 'Wikimedia Grants review system', |
| 56 | ]; |
| 57 | |
| 58 | /** |
| 59 | * @param array $settings Configuration settings for PHPMailer |
| 60 | * @param LoggerInterface|null $logger Log channel |
| 61 | */ |
| 62 | public function __construct( $settings = [], $logger = null ) { |
| 63 | $this->logger = $logger ?: new NullLogger(); |
| 64 | $settings = is_array( $settings ) ? $settings : []; |
| 65 | $this->settings = array_merge( $this->settings, $settings ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param string $to Recipient(s) |
| 70 | * @param string $subject Subject |
| 71 | * @param string $message Message |
| 72 | * @param array $settings Additional settings |
| 73 | * @return bool Send status |
| 74 | */ |
| 75 | public function mail( $to, $subject, $message, $settings = [] ) { |
| 76 | try { |
| 77 | $mailer = $this->createMailer( $settings ); |
| 78 | $mailer->addAddress( $to ); |
| 79 | $mailer->Subject = $subject; |
| 80 | $mailer->Body = $message; |
| 81 | return $mailer->send(); |
| 82 | |
| 83 | } catch ( Exception $e ) { |
| 84 | $this->logger->error( 'Failed to send message: {message}', [ |
| 85 | 'method' => __METHOD__, |
| 86 | 'exception' => $e, |
| 87 | 'message' => $e->getMessage(), |
| 88 | ] ); |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Create and configure a PHPMailer instance. |
| 95 | * |
| 96 | * @param array|null $settings Configuration settings |
| 97 | * @return PHPMailer New mailer configured with default, instance and local |
| 98 | * settings |
| 99 | */ |
| 100 | protected function createMailer( $settings = null ) { |
| 101 | $settings = is_array( $settings ) ? $settings : []; |
| 102 | $mailer = new PHPMailer( true ); |
| 103 | foreach ( array_merge( $this->settings, $settings ) as $key => $value ) { |
| 104 | $mailer->set( $key, $value ); |
| 105 | } |
| 106 | return $mailer; |
| 107 | } |
| 108 | |
| 109 | } |