Passed
Push — master ( 72d47d...8357d2 )
by Aimeos
13:03 queued 05:09
created

Swift::sender()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
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 $data Binary or string
219
	 * @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.)
220
	 * @param string|null $filename Name of the attached file (or null if inline disposition is used)
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 $mimetype, string $filename, string $disposition = 'attachment' ) : Iface
225
	{
226
		if( $data )
227
		{
228
			$part = new \Swift_Attachment( $data, $filename, $mimetype );
229
			$part->setDisposition( $disposition );
230
231
			$this->object->attach( $part );
232
		}
233
234
		return $this;
235
	}
236
237
238
	/**
239
	 * Embeds an attachment into the message and returns its reference.
240
	 *
241
	 * @param string $data Binary or string
242
	 * @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.)
243
	 * @param string|null $filename Name of the attached file
244
	 * @return string Content ID for referencing the attachment in the HTML body
245
	 */
246
	public function embed( string $data, string $mimetype, string $filename ) : string
247
	{
248
		$part = new \Swift_EmbeddedFile( $data, $filename, $mimetype );
249
250
		return $this->object->embed( $part );
251
	}
252
253
254
	/**
255
	 * Returns the internal Swift mail message object.
256
	 *
257
	 * @return \Swift_Message Swift mail message object
258
	 */
259
	public function object() : \Swift_Message
260
	{
261
		return $this->object;
262
	}
263
264
265
	/**
266
	 * Clones the internal objects.
267
	 */
268
	public function __clone()
269
	{
270
		$this->object = clone $this->object;
271
	}
272
}
273