Passed
Push — master ( 0c310b...413f38 )
by Aimeos
07:41
created

Swift::cc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2014-2021
6
 * @package MW
7
 * @subpackage Mail
8
 */
9
10
11
namespace Aimeos\MW\Mail\Message;
12
13
14
/**
15
 * SwiftMailer implementation for creating e-mails.
16
 *
17
 * @package MW
18
 * @subpackage Mail
19
 */
20
class Swift implements \Aimeos\MW\Mail\Message\Iface
21
{
22
	private $mailer;
23
	private $object;
24
25
26
	/**
27
	 * Initializes the message instance.
28
	 *
29
	 * @param \Aimeos\MW\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\MW\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\MW\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\MW\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\MW\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 string $email Destination address for a hidden copy
94
	 * @param string|null $name Name of the user owning the target mailbox or null for no name
95
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
96
	 */
97
	public function bcc( string $email, string $name = null ) : Iface
98
	{
99
		if( $email ) {
100
			$this->object->addBcc( $email, $name );
101
		}
102
		return $this;
103
	}
104
105
106
	/**
107
	 * Adds the return e-mail address for the message.
108
	 *
109
	 * @param string $email E-mail address which should receive all replies
110
	 * @param string|null $name Name of the user which should receive all replies or null for no name
111
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
112
	 */
113
	public function replyTo( string $email, string $name = null ) : Iface
114
	{
115
		if( $email ) {
116
			$this->object->addReplyTo( $email, $name );
117
		}
118
		return $this;
119
	}
120
121
122
	/**
123
	 * Adds a custom header to the message.
124
	 *
125
	 * @param string $name Name of the custom e-mail header
126
	 * @param string $value Text content of the custom e-mail header
127
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
128
	 */
129
	public function header( string $name, string $value ) : Iface
130
	{
131
		if( $name )
132
		{
133
			$hs = $this->object->getHeaders();
134
			$hs->addTextHeader( $name, $value );
135
		}
136
		return $this;
137
	}
138
139
140
	/**
141
	 * Sends the e-mail message to the mail server.
142
	 *
143
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
144
	 */
145
	public function send() : Iface
146
	{
147
		$this->mailer->send( $this );
148
		return $this;
149
	}
150
151
152
	/**
153
	 * Sets the e-mail address and name of the sender of the message (higher precedence than "From").
154
	 *
155
	 * @param string $email Source e-mail address
156
	 * @param string|null $name Name of the user who sent the message or null for no name
157
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
158
	 */
159
	public function sender( string $email, string $name = null ) : Iface
160
	{
161
		if( $email ) {
162
			$this->object->setSender( $email, $name );
163
		}
164
		return $this;
165
	}
166
167
168
	/**
169
	 * Sets the subject of the message.
170
	 *
171
	 * @param string $subject Subject of the message
172
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
173
	 */
174
	public function subject( string $subject ) : Iface
175
	{
176
		if( $subject ) {
177
			$this->object->setSubject( $subject );
178
		}
179
		return $this;
180
	}
181
182
183
	/**
184
	 * Sets the text body of the message.
185
	 *
186
	 * @param string $message Text body of the message
187
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
188
	 */
189
	public function text( string $message ) : Iface
190
	{
191
		if( $message ) {
192
			$this->object->addPart( $message, 'text/plain' );
193
		}
194
		return $this;
195
	}
196
197
198
	/**
199
	 * Sets the HTML body of the message.
200
	 *
201
	 * @param string $message HTML body of the message
202
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
203
	 */
204
	public function html( string $message ) : Iface
205
	{
206
		if( $message ) {
207
			$this->object->setBody( $message, 'text/html' );
208
		}
209
		return $this;
210
	}
211
212
213
	/**
214
	 * Adds an attachment to the message.
215
	 *
216
	 * @param string $data Binary or string
217
	 * @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.)
218
	 * @param string|null $filename Name of the attached file (or null if inline disposition is used)
219
	 * @param string $disposition Type of the disposition ("attachment" or "inline")
220
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
221
	 */
222
	public function attach( string $data, string $mimetype, string $filename, string $disposition = 'attachment' ) : Iface
223
	{
224
		if( $data )
225
		{
226
			$part = new \Swift_Attachment( $data, $filename, $mimetype );
227
			$part->setDisposition( $disposition );
228
229
			$this->object->attach( $part );
230
		}
231
232
		return $this;
233
	}
234
235
236
	/**
237
	 * Embeds an attachment into the message and returns its reference.
238
	 *
239
	 * @param string $data Binary or string
240
	 * @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.)
241
	 * @param string|null $filename Name of the attached file
242
	 * @return string Content ID for referencing the attachment in the HTML body
243
	 */
244
	public function embed( string $data, string $mimetype, string $filename ) : string
245
	{
246
		$part = new \Swift_EmbeddedFile( $data, $filename, $mimetype );
247
248
		return $this->object->embed( $part );
249
	}
250
251
252
	/**
253
	 * Returns the internal Swift mail message object.
254
	 *
255
	 * @return \Swift_Message Swift mail message object
256
	 */
257
	public function object() : \Swift_Message
258
	{
259
		return $this->object;
260
	}
261
262
263
	/**
264
	 * Clones the internal objects.
265
	 */
266
	public function __clone()
267
	{
268
		$this->object = clone $this->object;
269
	}
270
}
271