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

nette / mail / 24862033768

23 Apr 2026 10:27PM UTC coverage: 80.194% (+3.3%) from 76.863%
24862033768

push

github

dg
added CLAUDE.md

413 of 515 relevant lines covered (80.19%)

0.8 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
        {
165
                $value = $this->getHeader('Return-Path');
×
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 src\s*=\s*
197
                                        |<body(?:(?!\s background\s*=)[^<>])*+\s background\s*=\s*
198
                                        |<(?:(?!\s style\s*=)[^<>])++\s style\s*=\s* ["\'][^"\'>]+[:\s] url\(
199
                                        |<style[^>]*>[^<]+ [:\s] url\()
200
                                        (?|
201
                                                (["\'])(?![a-z]+:|[/\#])([^"\'>]+)
202
                                                |()(?![a-z]+:|[/\#])([^"\'>)\s]+)
203
                                        )
204
                                        |\[\[ ([\w()+./@~-]+) \]\]
205
                                #ix',
206
                                captureOffset: true,
1✔
207
                        );
208
                        foreach (array_reverse($matches) as $m) {
1✔
209
                                $file = rtrim($basePath, '/\\') . '/' . (isset($m[4]) ? $m[4][0] : urldecode($m[3][0]));
1✔
210
                                if (!isset($cids[$file])) {
1✔
211
                                        $contentId = $this->addEmbeddedFile($file)->getHeader('Content-ID');
1✔
212
                                        $cids[$file] = is_string($contentId) ? substr($contentId, 1, -1) : '';
1✔
213
                                }
214

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

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

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

232
                if ($this->getBody() === '' && $html !== '') {
1✔
233
                        $this->setBody($this->buildText($html));
1✔
234
                }
235

236
                return $this;
1✔
237
        }
238

239

240
        public function getHtmlBody(): string
241
        {
242
                return $this->htmlBody;
1✔
243
        }
244

245

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

256

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

266

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

276

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

285

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

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

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

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

321

322
        /********************* building and sending ****************d*g**/
323

324

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

333

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

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

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

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

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

378
                return $mail;
1✔
379
        }
380

381

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

400

401
        private function getRandomId(): string
402
        {
403
                return '<' . Nette\Utils\Random::generate() . '@'
1✔
404
                        . preg_replace('#[^\w.-]+#', '', $_SERVER['HTTP_HOST'] ?? php_uname('n'))
1✔
405
                        . '>';
1✔
406
        }
407
}
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