Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Mime;
13
 
14
use Symfony\Component\Mime\Exception\LogicException;
15
use Symfony\Component\Mime\Header\Headers;
16
use Symfony\Component\Mime\Part\AbstractPart;
17
use Symfony\Component\Mime\Part\TextPart;
18
 
19
/**
20
 * @author Fabien Potencier <fabien@symfony.com>
21
 */
22
class Message extends RawMessage
23
{
24
    private Headers $headers;
25
    private ?AbstractPart $body;
26
 
27
    public function __construct(Headers $headers = null, AbstractPart $body = null)
28
    {
29
        $this->headers = $headers ? clone $headers : new Headers();
30
        $this->body = $body;
31
    }
32
 
33
    public function __clone()
34
    {
35
        $this->headers = clone $this->headers;
36
 
37
        if (null !== $this->body) {
38
            $this->body = clone $this->body;
39
        }
40
    }
41
 
42
    /**
43
     * @return $this
44
     */
45
    public function setBody(AbstractPart $body = null): static
46
    {
47
        if (1 > \func_num_args()) {
48
            trigger_deprecation('symfony/mime', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
49
        }
50
        $this->body = $body;
51
 
52
        return $this;
53
    }
54
 
55
    public function getBody(): ?AbstractPart
56
    {
57
        return $this->body;
58
    }
59
 
60
    /**
61
     * @return $this
62
     */
63
    public function setHeaders(Headers $headers): static
64
    {
65
        $this->headers = $headers;
66
 
67
        return $this;
68
    }
69
 
70
    public function getHeaders(): Headers
71
    {
72
        return $this->headers;
73
    }
74
 
75
    public function getPreparedHeaders(): Headers
76
    {
77
        $headers = clone $this->headers;
78
 
79
        if (!$headers->has('From')) {
80
            if (!$headers->has('Sender')) {
81
                throw new LogicException('An email must have a "From" or a "Sender" header.');
82
            }
83
            $headers->addMailboxListHeader('From', [$headers->get('Sender')->getAddress()]);
84
        }
85
 
86
        if (!$headers->has('MIME-Version')) {
87
            $headers->addTextHeader('MIME-Version', '1.0');
88
        }
89
 
90
        if (!$headers->has('Date')) {
91
            $headers->addDateHeader('Date', new \DateTimeImmutable());
92
        }
93
 
94
        // determine the "real" sender
95
        if (!$headers->has('Sender') && \count($froms = $headers->get('From')->getAddresses()) > 1) {
96
            $headers->addMailboxHeader('Sender', $froms[0]);
97
        }
98
 
99
        if (!$headers->has('Message-ID')) {
100
            $headers->addIdHeader('Message-ID', $this->generateMessageId());
101
        }
102
 
103
        // remove the Bcc field which should NOT be part of the sent message
104
        $headers->remove('Bcc');
105
 
106
        return $headers;
107
    }
108
 
109
    public function toString(): string
110
    {
111
        if (null === $body = $this->getBody()) {
112
            $body = new TextPart('');
113
        }
114
 
115
        return $this->getPreparedHeaders()->toString().$body->toString();
116
    }
117
 
118
    public function toIterable(): iterable
119
    {
120
        if (null === $body = $this->getBody()) {
121
            $body = new TextPart('');
122
        }
123
 
124
        yield $this->getPreparedHeaders()->toString();
125
        yield from $body->toIterable();
126
    }
127
 
128
    public function ensureValidity()
129
    {
130
        if (!$this->headers->has('To') && !$this->headers->has('Cc') && !$this->headers->has('Bcc')) {
131
            throw new LogicException('An email must have a "To", "Cc", or "Bcc" header.');
132
        }
133
 
134
        if (!$this->headers->has('From') && !$this->headers->has('Sender')) {
135
            throw new LogicException('An email must have a "From" or a "Sender" header.');
136
        }
137
 
138
        parent::ensureValidity();
139
    }
140
 
141
    public function generateMessageId(): string
142
    {
143
        if ($this->headers->has('Sender')) {
144
            $sender = $this->headers->get('Sender')->getAddress();
145
        } elseif ($this->headers->has('From')) {
146
            $sender = $this->headers->get('From')->getAddresses()[0];
147
        } else {
148
            throw new LogicException('An email must have a "From" or a "Sender" header.');
149
        }
150
 
151
        return bin2hex(random_bytes(16)).strstr($sender->getAddress(), '@');
152
    }
153
 
154
    public function __serialize(): array
155
    {
156
        return [$this->headers, $this->body];
157
    }
158
 
159
    public function __unserialize(array $data): void
160
    {
161
        [$this->headers, $this->body] = $data;
162
    }
163
}