Swift   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 52
c 2
b 0
f 0
dl 0
loc 257
rs 9.76
wmc 33

16 Methods

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