| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports. |
| 5 | * Copyright (C) 2024 Aleksey Andreev (liuch) |
| 6 | * |
| 7 | * Available at: |
| 8 | * https://github.com/liuch/dmarc-srg |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the Free |
| 12 | * Software Foundation, either version 3 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 17 | * more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along with |
| 20 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | * ========================= |
| 23 | * |
| 24 | * This file contains an class to implement sending emails using the standard PHP mail() function. |
| 25 | * |
| 26 | * @category API |
| 27 | * @package DmarcSrg |
| 28 | * @author Aleksey Andreev (liuch) |
| 29 | * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3 |
| 30 | */ |
| 31 | |
| 32 | namespace Liuch\DmarcSrg\Mail; |
| 33 | |
| 34 | use Liuch\DmarcSrg\Exception\LogicException; |
| 35 | use Liuch\DmarcSrg\Exception\RuntimeException; |
| 36 | |
| 37 | /** |
| 38 | * This class is for sending emails using the standard PHP mail() function. |
| 39 | */ |
| 40 | class MailerInternal extends Mailer |
| 41 | { |
| 42 | /** |
| 43 | * Creates a message and sends it |
| 44 | * |
| 45 | * @throws RuntimeException |
| 46 | * |
| 47 | * @return void |
| 48 | */ |
| 49 | public function send(): void |
| 50 | { |
| 51 | if (isset($this->params['method']) && $this->params['method'] !== 'mail') { |
| 52 | throw new LogicException('Unsupported sending method'); |
| 53 | } |
| 54 | $this->checkData(); |
| 55 | $headers = [ |
| 56 | 'From' => $this->from, |
| 57 | 'MIME-Version' => '1.0', |
| 58 | 'Content-Type' => $this->body->contentType() |
| 59 | ]; |
| 60 | $ex = null; |
| 61 | $res = null; |
| 62 | try { |
| 63 | $res = mail( |
| 64 | implode(',', $this->addr), |
| 65 | mb_encode_mimeheader($this->subj, 'UTF-8'), |
| 66 | implode("\r\n", $this->body->content()), |
| 67 | $headers |
| 68 | ); |
| 69 | } catch (\Exception $e) { |
| 70 | $ex = $e; |
| 71 | } |
| 72 | if (!$res) { |
| 73 | throw new RuntimeException('Failed to send an email', -1, $ex); |
| 74 | } |
| 75 | } |
| 76 | } |