Laravel   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 32
eloc 53
c 5
b 0
f 0
dl 0
loc 257
rs 9.84

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 3 1
A html() 0 7 2
A header() 0 7 2
A to() 0 7 2
A bcc() 0 10 3
A replyTo() 0 7 2
A __construct() 0 5 1
A embed() 0 12 4
A from() 0 7 2
A send() 0 6 1
A attach() 0 11 4
A sender() 0 7 2
A text() 0 7 2
A subject() 0 7 2
A cc() 0 7 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2022-2025
6
 * @package Base
7
 * @subpackage Mail
8
 */
9
10
11
namespace Aimeos\Base\Mail\Message;
12
13
use Symfony\Component\Mime\Address;
14
15
16
/**
17
 * Laravel implementation for creating e-mails.
18
 *
19
 * @package Base
20
 * @subpackage Mail
21
 */
22
class Laravel implements \Aimeos\Base\Mail\Message\Iface
23
{
24
	private \Symfony\Component\Mime\Email $message;
25
	private \Illuminate\Mail\Mailer $mailer;
26
	private string $charset;
27
28
29
	/**
30
	 * Initializes the message instance.
31
	 *
32
	 * @param \Illuminate\Mail\Mailer $object Laravel mailer object
33
	 * @param \Symfony\Component\Mime\Email $message Message object
34
	 * @param string $charset Default charset of the message
35
	 */
36
	public function __construct( \Illuminate\Mail\Mailer $mailer, \Symfony\Component\Mime\Email $message, string $charset )
37
	{
38
		$this->charset = $charset;
39
		$this->message = $message;
40
		$this->mailer = $mailer;
41
	}
42
43
44
	/**
45
	 * Adds a source e-mail address of the message.
46
	 *
47
	 * @param string $email Source e-mail address
48
	 * @param string|null $name Name of the user sending the e-mail or null for no name
49
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
50
	 */
51
	public function from( string $email, ?string $name = null ) : Iface
52
	{
53
		if( $email ) {
54
			$this->message->from( new Address( $email, (string) $name ) );
55
		}
56
57
		return $this;
58
	}
59
60
61
	/**
62
	 * Adds a destination e-mail address of the target user mailbox.
63
	 *
64
	 * @param string $email Destination address of the target mailbox
65
	 * @param string|null $name Name of the user owning the target mailbox or null for no name
66
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
67
	 */
68
	public function to( string $email, ?string $name = null ) : Iface
69
	{
70
		if( $email ) {
71
			$this->message->to( new Address( $email, (string) $name ) );
72
		}
73
74
		return $this;
75
	}
76
77
78
	/**
79
	 * Adds a destination e-mail address for a copy of the message.
80
	 *
81
	 * @param string $email Destination address for a copy
82
	 * @param string|null $name Name of the user owning the target mailbox or null for no name
83
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
84
	 */
85
	public function cc( string $email, ?string $name = null ) : Iface
86
	{
87
		if( $email ) {
88
			$this->message->cc( new Address( $email, (string) $name ) );
89
		}
90
91
		return $this;
92
	}
93
94
95
	/**
96
	 * Adds a destination e-mail address for a hidden copy of the message.
97
	 *
98
	 * @param array|string $email Destination address for a hidden copy
99
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
100
	 */
101
	public function bcc( $email ) : Iface
102
	{
103
		if( !empty( $email ) )
104
		{
105
			foreach( (array) $email as $addr ) {
106
				$this->message->addBcc( $addr );
107
			}
108
		}
109
110
		return $this;
111
	}
112
113
114
	/**
115
	 * Adds the return e-mail address for the message.
116
	 *
117
	 * @param string $email E-mail address which should receive all replies
118
	 * @param string|null $name Name of the user which should receive all replies or null for no name
119
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
120
	 */
121
	public function replyTo( string $email, ?string $name = null ) : Iface
122
	{
123
		if( $email ) {
124
			$this->message->replyTo( new Address( $email, (string) $name ) );
125
		}
126
127
		return $this;
128
	}
129
130
131
	/**
132
	 * Adds a custom header to the message.
133
	 *
134
	 * @param string $name Name of the custom e-mail header
135
	 * @param string $value Text content of the custom e-mail header
136
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
137
	 */
138
	public function header( string $name, string $value ) : Iface
139
	{
140
		if( $name ) {
141
			$this->message->getHeaders()->add( new \Symfony\Component\Mime\Header\UnstructuredHeader( $name, $value ) );
142
		}
143
144
		return $this;
145
	}
146
147
148
	/**
149
	 * Sends the e-mail message to the mail server.
150
	 *
151
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
152
	 */
153
	public function send() : Iface
154
	{
155
		$this->mailer->getSymfonyTransport()
156
			->send( $this->message, \Symfony\Component\Mailer\Envelope::create( $this->message ) );
157
158
		return $this;
159
	}
160
161
162
	/**
163
	 * Sets the e-mail address and name of the sender of the message (higher precedence than "From").
164
	 *
165
	 * @param string $email Source e-mail address
166
	 * @param string|null $name Name of the user who sent the message or null for no name
167
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
168
	 */
169
	public function sender( string $email, ?string $name = null ) : Iface
170
	{
171
		if( $email ) {
172
			$this->message->sender( new Address( $email, (string) $name ) );
173
		}
174
175
		return $this;
176
	}
177
178
179
	/**
180
	 * Sets the subject of the message.
181
	 *
182
	 * @param string $subject Subject of the message
183
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
184
	 */
185
	public function subject( string $subject ) : Iface
186
	{
187
		if( $subject ) {
188
			$this->message->subject( $subject );
189
		}
190
191
		return $this;
192
	}
193
194
195
	/**
196
	 * Sets the text body of the message.
197
	 *
198
	 * @param string $message Text body of the message
199
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
200
	 */
201
	public function text( string $message ) : Iface
202
	{
203
		if( $message ) {
204
			$this->message->text( $message, $this->charset );
205
		}
206
207
		return $this;
208
	}
209
210
211
	/**
212
	 * Sets the HTML body of the message.
213
	 *
214
	 * @param string $message HTML body of the message
215
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
216
	 */
217
	public function html( string $message ) : Iface
218
	{
219
		if( $message ) {
220
			$this->message->html( $message, $this->charset );
221
		}
222
223
		return $this;
224
	}
225
226
227
	/**
228
	 * Adds an attachment to the message.
229
	 *
230
	 * @param string|null $data Binary or string @author nose
231
	 * @param string|null $filename Name of the attached file (or null if inline disposition is used)
232
	 * @param string|null $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.)
233
	 * @param string $disposition Type of the disposition ("attachment" or "inline")
234
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
235
	 */
236
	public function attach( ?string $data, ?string $filename = null, ?string $mimetype = null, string $disposition = 'attachment' ) : Iface
237
	{
238
		if( $data )
239
		{
240
			$mimetype = $mimetype ?: (new \finfo( FILEINFO_MIME_TYPE ))->buffer( $data );
241
			$filename = $filename ?: md5( $data );
242
243
			$this->message->attach( $data, $filename, $mimetype );
244
		}
245
246
		return $this;
247
	}
248
249
250
	/**
251
	 * Embeds an attachment into the message and returns its reference.
252
	 *
253
	 * @param string|null $data Binary or string
254
	 * @param string|null $filename Name of the attached file
255
	 * @param string|null $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.)
256
	 * @return string Content ID for referencing the attachment in the HTML body
257
	 */
258
	public function embed( ?string $data, ?string $filename = null, ?string $mimetype = null ) : string
259
	{
260
		if( $data )
261
		{
262
			$mimetype = $mimetype ?: (new \finfo( FILEINFO_MIME_TYPE ))->buffer( $data );
263
			$filename = $filename ?: md5( $data );
264
265
			$this->message->embed( $data, $filename, $mimetype );
266
			return 'cid:' . $filename;
267
		}
268
269
		return '';
270
	}
271
272
273
	/**
274
	 * Clones the internal objects.
275
	 */
276
	public function __clone()
277
	{
278
		$this->message = clone $this->message;
279
	}
280
}
281