• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / mail / 22836185153

09 Mar 2026 02:44AM UTC coverage: 76.863%. Remained the same
22836185153

push

github

dg
fixed PHPStan errors WIP

31 of 45 new or added lines in 5 files covered. (68.89%)

50 existing lines in 5 files now uncovered.

392 of 510 relevant lines covered (76.86%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.93
/src/Mail/Message.php
1
<?php declare(strict_types=1);
1✔
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Nette\Mail;
9

10
use Nette;
11
use Nette\Utils\Strings;
12
use function addcslashes, array_map, array_reverse, basename, date, explode, finfo_buffer, finfo_open, implode, is_array, is_numeric, is_string, ltrim, php_uname, preg_match, preg_replace, rtrim, str_replace, strcasecmp, stripslashes, strlen, substr, substr_replace, trim, urldecode;
13

14

15
/**
16
 * Represents an email message with support for HTML body, attachments, and embedded files.
17
 *
18
 * @property-deprecated   string $subject
19
 * @property-deprecated   string $htmlBody
20
 */
21
class Message extends MimePart
22
{
23
        /** Priority */
24
        public const
25
                High = 1,
26
                Normal = 3,
27
                Low = 5;
28

29
        #[\Deprecated('use Message::High')]
30
        public const HIGH = self::High;
31

32
        #[\Deprecated('use Message::Normal')]
33
        public const NORMAL = self::Normal;
34

35
        #[\Deprecated('use Message::Low')]
36
        public const LOW = self::Low;
37

38
        /** @var array<string, string> */
39
        public static array $defaultHeaders = [
40
                'MIME-Version' => '1.0',
41
                'X-Mailer' => 'Nette Framework',
42
        ];
43

44
        /** @var list<MimePart> */
45
        private array $attachments = [];
46

47
        /** @var array<MimePart> */
48
        private array $inlines = [];
49
        private string $htmlBody = '';
50

51

52
        public function __construct()
53
        {
54
                foreach (static::$defaultHeaders as $name => $value) {
1✔
55
                        $this->setHeader($name, $value);
1✔
56
                }
57

58
                $this->setHeader('Date', date('r'));
1✔
59
        }
1✔
60

61

62
        /**
63
         * Sets the sender of the message. Email or format "John Doe" <doe@example.com>
64
         */
65
        public function setFrom(string $email, ?string $name = null): static
1✔
66
        {
67
                $this->setHeader('From', $this->formatEmail($email, $name));
1✔
68
                return $this;
1✔
69
        }
70

71

72
        /**
73
         * Returns the sender of the message.
74
         * @return ?array<string, ?string>
75
         */
76
        public function getFrom(): ?array
77
        {
78
                $value = $this->getHeader('From');
1✔
79
                return is_array($value) ? $value : null;
1✔
80
        }
81

82

83
        /**
84
         * Adds the reply-to address. Email or format "John Doe" <doe@example.com>
85
         */
86
        public function addReplyTo(string $email, ?string $name = null): static
1✔
87
        {
88
                $this->setHeader('Reply-To', $this->formatEmail($email, $name), append: true);
1✔
89
                return $this;
1✔
90
        }
91

92

93
        public function setSubject(string $subject): static
1✔
94
        {
95
                $this->setHeader('Subject', $subject);
1✔
96
                return $this;
1✔
97
        }
98

99

100
        public function getSubject(): ?string
101
        {
102
                $value = $this->getHeader('Subject');
1✔
103
                return is_string($value) ? $value : null;
1✔
104
        }
105

106

107
        /**
108
         * Adds email recipient. Email or format "John Doe" <doe@example.com>
109
         */
110
        public function addTo(string $email, ?string $name = null): static // addRecipient()
1✔
111
        {
112
                $this->setHeader('To', $this->formatEmail($email, $name), append: true);
1✔
113
                return $this;
1✔
114
        }
115

116

117
        /**
118
         * Adds carbon copy email recipient. Email or format "John Doe" <doe@example.com>
119
         */
120
        public function addCc(string $email, ?string $name = null): static
1✔
121
        {
122
                $this->setHeader('Cc', $this->formatEmail($email, $name), append: true);
1✔
123
                return $this;
1✔
124
        }
125

126

127
        /**
128
         * Adds blind carbon copy email recipient. Email or format "John Doe" <doe@example.com>
129
         */
130
        public function addBcc(string $email, ?string $name = null): static
1✔
131
        {
132
                $this->setHeader('Bcc', $this->formatEmail($email, $name), append: true);
1✔
133
                return $this;
1✔
134
        }
135

136

137
        /**
138
         * Formats recipient email.
139
         * @return array<string, ?string>
140
         */
141
        private function formatEmail(string $email, ?string $name = null): array
1✔
142
        {
143
                if (!$name && preg_match('#^(.+) +<(.*)>$#D', $email, $matches)) {
1✔
144
                        [, $name, $email] = $matches;
1✔
145
                        $name = stripslashes($name);
1✔
146
                        $tmp = substr($name, 1, -1);
1✔
147
                        if ($name === '"' . $tmp . '"') {
1✔
148
                                $name = $tmp;
1✔
149
                        }
150
                }
151

152
                return [$email => $name];
1✔
153
        }
154

155

156
        public function setReturnPath(string $email): static
1✔
157
        {
158
                $this->setHeader('Return-Path', $email);
1✔
159
                return $this;
1✔
160
        }
161

162

163
        public function getReturnPath(): ?string
164
        {
NEW
165
                $value = $this->getHeader('Return-Path');
×
NEW
166
                return is_string($value) ? $value : null;
×
167
        }
168

169

170
        public function setPriority(int $priority): static
1✔
171
        {
172
                $this->setHeader('X-Priority', (string) $priority);
1✔
173
                return $this;
1✔
174
        }
175

176

177
        public function getPriority(): ?int
178
        {
179
                $priority = $this->getHeader('X-Priority');
1✔
180
                return is_numeric($priority) ? (int) $priority : null;
1✔
181
        }
182

183

184
        /**
185
         * Sets HTML body. If $basePath is provided, local images referenced in the HTML
186
         * are automatically embedded as inline attachments with their src rewritten to cid: URIs.
187
         * Also sets the subject from the HTML <title> if not already set, and auto-generates a plain-text alternative.
188
         */
189
        public function setHtmlBody(string $html, ?string $basePath = null): static
1✔
190
        {
191
                if ($basePath) {
1✔
192
                        $cids = [];
1✔
193
                        $matches = Strings::matchAll(
1✔
194
                                $html,
1✔
195
                                '#
1✔
196
                                        (<img[^<>]*\s src\s*=\s*
197
                                        |<body[^<>]*\s background\s*=\s*
198
                                        |<[^<>]+\s style\s*=\s* ["\'][^"\'>]+[:\s] url\(
199
                                        |<style[^>]*>[^<]+ [:\s] url\()
200
                                        (["\']?)(?![a-z]+:|[/\#])([^"\'>)\s]+)
201
                                        |\[\[ ([\w()+./@~-]+) \]\]
202
                                #ix',
203
                                captureOffset: true,
1✔
204
                        );
205
                        foreach (array_reverse($matches) as $m) {
1✔
206
                                $file = rtrim($basePath, '/\\') . '/' . (isset($m[4]) ? $m[4][0] : urldecode($m[3][0]));
1✔
207
                                if (!isset($cids[$file])) {
1✔
208
                                        $contentId = $this->addEmbeddedFile($file)->getHeader('Content-ID');
1✔
209
                                        $cids[$file] = is_string($contentId) ? substr($contentId, 1, -1) : '';
1✔
210
                                }
211

212
                                $html = substr_replace(
1✔
213
                                        $html,
1✔
214
                                        "{$m[1][0]}{$m[2][0]}cid:{$cids[$file]}",
1✔
215
                                        $m[0][1],
1✔
216
                                        strlen($m[0][0]),
1✔
217
                                );
218
                        }
219
                }
220

221
                if ($this->getSubject() == null) { // intentionally ==
1✔
222
                        $html = Strings::replace($html, '#<title>(.+?)</title>#is', function (array $m): void {
1✔
223
                                $this->setSubject(Nette\Utils\Html::htmlToText($m[1]));
1✔
224
                        });
1✔
225
                }
226

227
                $this->htmlBody = ltrim(str_replace("\r", '', $html), "\n");
1✔
228

229
                if ($this->getBody() === '' && $html !== '') {
1✔
230
                        $this->setBody($this->buildText($html));
1✔
231
                }
232

233
                return $this;
1✔
234
        }
235

236

237
        public function getHtmlBody(): string
238
        {
239
                return $this->htmlBody;
1✔
240
        }
241

242

243
        /**
244
         * Adds an embedded (inline) file. If $content is null, the file is read from disk.
245
         * In that case $file is the path; otherwise $file is used as the filename.
246
         */
247
        public function addEmbeddedFile(string $file, ?string $content = null, ?string $contentType = null): MimePart
1✔
248
        {
249
                return $this->inlines[$file] = $this->createAttachment($file, $content, $contentType, 'inline')
1✔
250
                        ->setHeader('Content-ID', $this->getRandomId());
1✔
251
        }
252

253

254
        /**
255
         * Adds a pre-built MIME part as an inline (embedded) attachment.
256
         */
257
        public function addInlinePart(MimePart $part): static
258
        {
UNCOV
259
                $this->inlines[] = $part;
×
UNCOV
260
                return $this;
×
261
        }
262

263

264
        /**
265
         * Adds an attachment. If $content is null, the file is read from disk.
266
         * In that case $file is the path; otherwise $file is used as the filename.
267
         */
268
        public function addAttachment(string $file, ?string $content = null, ?string $contentType = null): MimePart
1✔
269
        {
270
                return $this->attachments[] = $this->createAttachment($file, $content, $contentType, 'attachment');
1✔
271
        }
272

273

274
        /**
275
         * @return list<MimePart>
276
         */
277
        public function getAttachments(): array
278
        {
UNCOV
279
                return $this->attachments;
×
280
        }
281

282

283
        /**
284
         * Creates file MIME part.
285
         */
286
        private function createAttachment(
1✔
287
                string $file,
288
                ?string $content,
289
                ?string $contentType,
290
                string $disposition,
291
        ): MimePart
292
        {
293
                $part = new MimePart;
1✔
294
                if ($content === null) {
1✔
295
                        $content = Nette\Utils\FileSystem::read($file);
1✔
296
                        $file = Strings::fixEncoding(basename($file));
1✔
297
                }
298

299
                if (!$contentType) {
1✔
300
                        $finfo = finfo_open(FILEINFO_MIME_TYPE);
1✔
301
                        $contentType = $finfo ? finfo_buffer($finfo, $content) : false;
1✔
302
                        $contentType = $contentType ?: 'application/octet-stream';
1✔
303
                }
304

305
                if (!strcasecmp($contentType, 'message/rfc822')) { // not allowed for attached files
1✔
306
                        $contentType = 'application/octet-stream';
1✔
307
                } elseif (!strcasecmp($contentType, 'image/svg')) { // Troublesome for some mailers...
1✔
UNCOV
308
                        $contentType = 'image/svg+xml';
×
309
                }
310

311
                $part->setBody($content);
1✔
312
                $part->setContentType($contentType);
1✔
313
                $part->setEncoding(preg_match('#(multipart|message)/#A', $contentType) ? self::Encoding8Bit : self::EncodingBase64);
1✔
314
                $part->setHeader('Content-Disposition', $disposition . '; filename="' . addcslashes($file, '"\\') . '"');
1✔
315
                return $part;
1✔
316
        }
317

318

319
        /********************* building and sending ****************d*g**/
320

321

322
        /**
323
         * Returns encoded message.
324
         */
325
        public function generateMessage(): string
326
        {
327
                return $this->build()->getEncodedMessage();
1✔
328
        }
329

330

331
        /**
332
         * Builds email. Does not modify itself, but returns a new object.
333
         */
334
        public function build(): static
335
        {
336
                $mail = clone $this;
1✔
337
                $mail->setHeader('Message-ID', $mail->getHeader('Message-ID') ?? $this->getRandomId());
1✔
338

339
                $cursor = $mail;
1✔
340
                if ($mail->attachments) {
1✔
341
                        $tmp = $cursor->setContentType('multipart/mixed');
1✔
342
                        $cursor = $cursor->addPart();
1✔
343
                        foreach ($mail->attachments as $value) {
1✔
344
                                $tmp->addPart($value);
1✔
345
                        }
346
                }
347

348
                if ($mail->htmlBody !== '') {
1✔
349
                        $tmp = $cursor->setContentType('multipart/alternative');
1✔
350
                        $cursor = $cursor->addPart();
1✔
351
                        $alt = $tmp->addPart();
1✔
352
                        if ($mail->inlines) {
1✔
353
                                $tmp = $alt->setContentType('multipart/related');
1✔
354
                                $alt = $alt->addPart();
1✔
355
                                foreach ($mail->inlines as $value) {
1✔
356
                                        $tmp->addPart($value);
1✔
357
                                }
358
                        }
359

360
                        $alt->setContentType('text/html', 'UTF-8')
1✔
361
                                ->setEncoding(preg_match('#[^\n]{990}#', $mail->htmlBody)
1✔
UNCOV
362
                                        ? self::EncodingQuotedPrintable
×
363
                                        : (preg_match('#[\x80-\xFF]#', $mail->htmlBody) ? self::Encoding8Bit : self::Encoding7Bit))
1✔
364
                                ->setBody($mail->htmlBody);
1✔
365
                }
366

367
                $text = $mail->getBody();
1✔
368
                $mail->setBody('');
1✔
369
                $cursor->setContentType('text/plain', 'UTF-8')
1✔
370
                        ->setEncoding(preg_match('#[^\n]{990}#', $text)
1✔
371
                                ? self::EncodingQuotedPrintable
1✔
372
                                : (preg_match('#[\x80-\xFF]#', $text) ? self::Encoding8Bit : self::Encoding7Bit))
1✔
373
                        ->setBody($text);
1✔
374

375
                return $mail;
1✔
376
        }
377

378

379
        /**
380
         * Generates a plain-text alternative from HTML.
381
         */
382
        protected function buildText(string $html): string
1✔
383
        {
384
                $html = Strings::replace($html, [
1✔
385
                        '#<(style|script|head).*</\1>#Uis' => '',
1✔
386
                        '#<t[dh][ >]#i' => ' $0',
387
                        '#<a\s[^>]*href=(?|"([^"]+)"|\'([^\']+)\')[^>]*>(.*?)</a>#is' => '$2 &lt;$1&gt;',
388
                        '#[\r\n]+#' => ' ',
389
                        '#<(/?p|/?h\d|li|br|/tr)[ >/]#i' => "\n$0",
390
                ]);
391
                $text = Nette\Utils\Html::htmlToText($html);
1✔
392
                $text = Strings::replace($text, '#[ \t]+#', ' ');
1✔
393
                $text = implode("\n", array_map('trim', explode("\n", $text)));
1✔
394
                return trim($text);
1✔
395
        }
396

397

398
        private function getRandomId(): string
399
        {
400
                return '<' . Nette\Utils\Random::generate() . '@'
1✔
401
                        . preg_replace('#[^\w.-]+#', '', $_SERVER['HTTP_HOST'] ?? php_uname('n'))
1✔
402
                        . '>';
1✔
403
        }
404
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc