Passed
Push — master ( 1f914d...5ffc4b )
by Aimeos
12:32
created

Symfony::object()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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