-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmailer.php
More file actions
executable file
·179 lines (166 loc) · 5.46 KB
/
mailer.php
File metadata and controls
executable file
·179 lines (166 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* Email handler
*
* PHP version 8
*
* Copyright (C) Ere Maijala 2018-2025
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category MLInvoice
* @package MLInvoice\Base
* @author Ere Maijala <ere@labs.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://labs.fi/mlinvoice.eng.php
*/
use MLInvoice\Mailer\MailTransport;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Header\Headers;
require_once 'translator.php';
require_once 'settings.php';
/**
* Email handling
*
* @category MLInvoice
* @package MLInvoice\Base
* @author Ere Maijala <ere@labs.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://labs.fi/mlinvoice.eng.php
*/
class Mailer
{
/**
* Any error that occurred while sending email
*
* @var string
*/
protected $error = '';
/**
* Return any error that occurred while sending email
*
* @return string
*/
public function getErrorMessage()
{
return $this->error;
}
/**
* Send email
*
* @param string $from "From" address
* @param array $to "To" addresses
* @param array $cc "CC" addresses
* @param array $bcc "BCC" addresses
* @param string $subject Subject
* @param string $body Message body
* @param array $attachments Attachments
*
* @return bool Success
*/
public function sendEmail($from, $to, $cc, $bcc, $subject, $body, $attachments)
{
$this->error = '';
mb_internal_encoding('UTF-8');
$headers = (new Headers())
->addHeader('X-Mailer', 'MLInvoice');
$message = (new Email())
->setHeaders($headers)
->subject($subject)
->from($from)
->text($body);
foreach (['to' => $to, 'cc' => $cc, 'bcc' => $bcc] as $func => $addresses) {
if ($addresses) {
call_user_func_array(
[$message, $func],
$this->extractAddresses($addresses)
);
}
}
foreach ($attachments as $current) {
$message->attach(
$current['data'],
$current['filename'],
$current['mimetype']
);
}
$settings = $GLOBALS['mlinvoice_mail_settings'] ?? [];
try {
switch ($settings['send_method'] ?? 'mail') {
case 'mail':
$transport = new MailTransport();
break;
case 'sendmail':
$dsn = 'sendmail://default';
if ($command = $settings['sendmail']['command'] ?? '') {
$dsn .= '?command=' . urlencode($command);
}
$transport = Transport::fromDsn($dsn);
break;
case 'smtp':
$smtp = empty($settings['smtp']) ? [] : $settings['smtp'];
$dsn = ($smtp['security'] ?? '') === 'ssl' ? 'smtps://' : 'smtp://';
if ($username = $smtp['username'] ?? '') {
$dsn .= $username;
}
if ($password = $smtp['password'] ?? '') {
$dsn .= ":$password";
}
$dsn .= '@' . $smtp['host'] ?? '';
if ($port = $smtp['port'] ?? '') {
$dsn .= ":$port";
}
$transport = Transport::fromDsn($dsn);
if (!empty($smtp['stream_context_options'])
&& is_callable([$transport, 'getStream'])
) {
$transport->getStream()->setStreamOptions(
$smtp['stream_context_options']
);
}
break;
default:
if (empty($settings['dsn'])) {
$this->error = 'dsn missing from mail settings; check config.php';
return false;
}
$transport = Transport::fromDsn($settings['dsn']);
}
$mailer = new \Symfony\Component\Mailer\Mailer($transport);
$mailer->send($message);
} catch (Exception $e) {
$this->error = Translator::translate('EmailFailed') . ': '
. $e->getMessage();
return false;
}
return true;
}
/**
* Extract addresses to an array
*
* @param string|array $addresses Email addresses
*
* @return array
*/
protected function extractAddresses($addresses): array
{
if ($addresses && is_string($addresses)) {
$result = array_map('trim', str_getcsv($addresses));
} else {
$result = $addresses;
}
return $result;
}
}