Passed
Push — master ( b328f8...27b7aa )
by Aimeos
02:15
created

Swift::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
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), 2014-2020
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 addFrom( string $email, string $name = null ) : Iface
50
	{
51
		$this->object->addFrom( $email, $name );
52
		return $this;
53
	}
54
55
56
	/**
57
	 * Adds a destination e-mail address of the target user mailbox.
58
	 *
59
	 * @param string $email Destination address of the target mailbox
60
	 * @param string|null $name Name of the user owning the target mailbox or null for no name
61
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
62
	 */
63
	public function addTo( string $email, string $name = null ) : Iface
64
	{
65
		$this->object->addTo( $email, $name );
66
		return $this;
67
	}
68
69
70
	/**
71
	 * Adds a destination e-mail address for a copy of the message.
72
	 *
73
	 * @param string $email Destination address for a copy
74
	 * @param string|null $name Name of the user owning the target mailbox or null for no name
75
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
76
	 */
77
	public function addCc( string $email, string $name = null ) : Iface
78
	{
79
		$this->object->addCc( $email, $name );
80
		return $this;
81
	}
82
83
84
	/**
85
	 * Adds a destination e-mail address for a hidden copy of the message.
86
	 *
87
	 * @param string $email Destination address for a hidden copy
88
	 * @param string|null $name Name of the user owning the target mailbox or null for no name
89
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
90
	 */
91
	public function addBcc( string $email, string $name = null ) : Iface
92
	{
93
		$this->object->addBcc( $email, $name );
94
		return $this;
95
	}
96
97
98
	/**
99
	 * Adds the return e-mail address for the message.
100
	 *
101
	 * @param string $email E-mail address which should receive all replies
102
	 * @param string|null $name Name of the user which should receive all replies or null for no name
103
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
104
	 */
105
	public function addReplyTo( string $email, string $name = null ) : Iface
106
	{
107
		$this->object->addReplyTo( $email, $name );
108
		return $this;
109
	}
110
111
112
	/**
113
	 * Adds a custom header to the message.
114
	 *
115
	 * @param string $name Name of the custom e-mail header
116
	 * @param string $value Text content of the custom e-mail header
117
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
118
	 */
119
	public function addHeader( string $name, string $value ) : Iface
120
	{
121
		$hs = $this->object->getHeaders();
122
		$hs->addTextHeader( $name, $value );
123
		return $this;
124
	}
125
126
127
	/**
128
	 * Sends the e-mail message to the mail server.
129
	 *
130
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
131
	 */
132
	public function send() : Iface
133
	{
134
		$this->mailer->send( $this );
135
		return $this;
136
	}
137
138
139
	/**
140
	 * Sets the e-mail address and name of the sender of the message (higher precedence than "From").
141
	 *
142
	 * @param string $email Source e-mail address
143
	 * @param string|null $name Name of the user who sent the message or null for no name
144
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
145
	 */
146
	public function setSender( string $email, string $name = null ) : Iface
147
	{
148
		$this->object->setSender( $email, $name );
149
		return $this;
150
	}
151
152
153
	/**
154
	 * Sets the subject of the message.
155
	 *
156
	 * @param string $subject Subject of the message
157
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
158
	 */
159
	public function setSubject( string $subject ) : Iface
160
	{
161
		$this->object->setSubject( $subject );
162
		return $this;
163
	}
164
165
166
	/**
167
	 * Sets the text body of the message.
168
	 *
169
	 * @param string $message Text body of the message
170
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
171
	 */
172
	public function setBody( string $message ) : Iface
173
	{
174
		$this->object->addPart( $message, 'text/plain' );
175
		return $this;
176
	}
177
178
179
	/**
180
	 * Sets the HTML body of the message.
181
	 *
182
	 * @param string $message HTML body of the message
183
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
184
	 */
185
	public function setBodyHtml( string $message ) : Iface
186
	{
187
		$this->object->setBody( $message, 'text/html' );
188
		return $this;
189
	}
190
191
192
	/**
193
	 * Adds an attachment to the message.
194
	 *
195
	 * @param string $data Binary or string
196
	 * @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.)
197
	 * @param string|null $filename Name of the attached file (or null if inline disposition is used)
198
	 * @param string $disposition Type of the disposition ("attachment" or "inline")
199
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
200
	 */
201
	public function addAttachment( string $data, string $mimetype, string $filename, string $disposition = 'attachment' ) : Iface
202
	{
203
		$part = new \Swift_Attachment( $data, $filename, $mimetype );
204
		$part->setDisposition( $disposition );
205
206
		$this->object->attach( $part );
207
		return $this;
208
	}
209
210
211
	/**
212
	 * Embeds an attachment into the message and returns its reference.
213
	 *
214
	 * @param string $data Binary or string
215
	 * @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.)
216
	 * @param string|null $filename Name of the attached file
217
	 * @return string Content ID for referencing the attachment in the HTML body
218
	 */
219
	public function embedAttachment( string $data, string $mimetype, string $filename ) : string
220
	{
221
		$part = new \Swift_EmbeddedFile( $data, $filename, $mimetype );
222
223
		return $this->object->embed( $part );
224
	}
225
226
227
	/**
228
	 * Returns the internal Swift mail message object.
229
	 *
230
	 * @return \Swift_Message Swift mail message object
231
	 */
232
	public function getObject() : \Swift_Message
233
	{
234
		return $this->object;
235
	}
236
237
238
	/**
239
	 * Clones the internal objects.
240
	 */
241
	public function __clone()
242
	{
243
		$this->object = clone $this->object;
244
	}
245
}
246