99use phpbu \App \Log \MailTemplate as TPL ;
1010use phpbu \App \Util \Arr ;
1111use phpbu \App \Util \Str ;
12- use Swift_Mailer ;
13- use Swift_Message ;
12+ use PHPMailer \PHPMailer \PHPMailer ;
1413
1514/**
1615 * Mail Logger
@@ -28,7 +27,7 @@ class Mail implements Listener, Logger
2827 /**
2928 * Mailer instance
3029 *
31- * @var Swift_Mailer
30+ * @var \PHPMailer\PHPMailer\PHPMailer;
3231 */
3332 protected $ mailer ;
3433
@@ -171,8 +170,8 @@ public function setup(array $options)
171170 $ this ->isSimulation = Arr::getValue ($ options , '__simulate__ ' , false );
172171
173172 // create transport an mailer
174- $ transport = $ this ->createTransport ( $ this -> transportType , $ options );
175- $ this ->mailer = new Swift_Mailer ( $ transport );
173+ $ this ->mailer = new PHPMailer ( );
174+ $ this ->setupMailer ( $ this -> transportType , $ options );
176175 }
177176
178177 /**
@@ -201,20 +200,16 @@ public function onPhpbuEnd(Event\App\End $event)
201200 $ sent = null ;
202201 $ state = $ result ->allOk () ? 'OK ' : ($ result ->backupOkButSkipsOrFails () ? 'WARNING ' : 'ERROR ' );
203202
204- try {
205- /** @var \Swift_Message $message */
206- $ message = new Swift_Message ();
207- $ message ->setSubject ($ this ->subject . ' [ ' . $ state . '] ' )
208- ->setFrom ($ this ->senderMail , $ this ->senderName )
209- ->setTo ($ this ->recipients )
210- ->setBody ($ body , 'text/html ' );
211-
212- $ sent = $ this ->mailer ->send ($ message );
213- } catch (\Exception $ e ) {
214- throw new Exception ($ e ->getMessage ());
203+ $ this ->mailer ->Subject = $ this ->subject . ' [ ' . $ state . '] ' ;
204+ $ this ->mailer ->setFrom ($ this ->senderMail , $ this ->senderName );
205+ $ this ->mailer ->msgHTML ($ body );
206+
207+ foreach ($ this ->recipients as $ recipient ) {
208+ $ this ->mailer ->addAddress ($ recipient );
215209 }
216- if (!$ sent ) {
217- throw new Exception ('mail could not be sent ' );
210+
211+ if (!$ this ->mailer ->send ()) {
212+ throw new Exception ($ this ->mailer ->ErrorInfo );
218213 }
219214 }
220215 }
@@ -270,40 +265,36 @@ public function onCleanupStart(Event\Cleanup\Start $event)
270265 }
271266
272267 /**
273- * Create a Swift_Mailer_Transport.
268+ * Configure PHPMailer
274269 *
275- * @param string $type
276- * @param array $options
270+ * @param string $type
271+ * @param array $options
277272 * @throws \phpbu\App\Exception
278- * @return \Swift_Transport
279273 */
280- protected function createTransport ($ type , array $ options )
274+ protected function setupMailer ($ type , array $ options )
281275 {
282276 switch ($ type ) {
283- // null transport, don't send any mails
284277 case 'null ' :
285- /* @var $transport \Swift_NullTransport */
286- $ transport = new \Swift_NullTransport ();
278+ $ this ->isSimulation = true ;
287279 break ;
288280
289281 case 'smtp ' :
290- $ transport = $ this ->getSmtpTransport ($ options );
282+ $ this ->setupSmtpMailer ($ options );
291283 break ;
292284
293285 case 'mail ' :
294286 case 'sendmail ' :
295- $ transport = $ this ->getSendmailTransport ($ options );
287+ $ this ->setupSendmailMailer ($ options );
296288 break ;
297289
298290 // UPS! no transport given
299291 default :
300292 throw new Exception (sprintf ('mail transport not supported: \'%s \'' , $ type ));
301293 }
302- return $ transport ;
303294 }
304295
305296 /**
306- * Should a mail be send.
297+ * Should a mail be send
307298 *
308299 * @param \phpbu\App\Result $result
309300 * @return bool
@@ -318,13 +309,13 @@ protected function shouldMailBeSend(Result $result) : bool
318309 }
319310
320311 /**
321- * Create Swift Smtp Transport.
312+ * Setup smtp mailing
322313 *
323314 * @param array $options
324- * @return \Swift_SmtpTransport
315+ * @return void
325316 * @throws \phpbu\App\Exception
326317 */
327- protected function getSmtpTransport (array $ options )
318+ protected function setupSmtpMailer (array $ options )
328319 {
329320 if (!isset ($ options ['smtp.host ' ])) {
330321 throw new Exception ('option \'smtp.host \' ist missing ' );
@@ -335,40 +326,36 @@ protected function getSmtpTransport(array $options)
335326 $ password = Arr::getValue ($ options , 'smtp.password ' );
336327 $ encryption = Arr::getValue ($ options , 'smtp.encryption ' );
337328
338- /* @var $transport \Swift_SmtpTransport */
339- $ transport = new \Swift_SmtpTransport ($ host , $ port );
329+ $ this ->mailer ->isSMTP ();
330+ $ this ->mailer ->Host = $ host ;
331+ $ this ->mailer ->Port = $ port ;
340332
341333 if ($ username && $ password ) {
342- $ transport ->setUsername ($ username )
343- ->setPassword ($ password );
334+ $ this ->mailer ->SMTPAuth = true ;
335+ $ this ->mailer ->Username = $ username ;
336+ $ this ->mailer ->Password = $ password ;
344337 }
345338 if ($ encryption ) {
346- $ transport -> setEncryption ( $ encryption) ;
339+ $ this -> mailer -> SMTPSecure = $ encryption ;
347340 }
348- return $ transport ;
349341 }
350342
351343 /**
352- * Create a Swift Sendmail Transport.
344+ * Setup the php mail transport
353345 *
354346 * @param array $options
355- * @return \Swift_SendmailTransport
347+ * @return void
356348 */
357- protected function getSendmailTransport (array $ options )
349+ protected function setupSendmailMailer (array $ options )
358350 {
359- if (isset ($ options ['sendmail.path ' ])) {
360- $ path = $ options ['sendmail.path ' ];
361- $ options = isset ($ options ['sendmail.options ' ]) ? ' ' . $ options ['sendmail.options ' ] : '' ;
362- /* @var $transport \Swift_SendmailTransport */
363- return new \Swift_SendmailTransport ($ path . $ options );
364- }
365- return new \Swift_SendmailTransport ();
351+ // nothing to do here
366352 }
367353
368354 /**
369355 * Return mail header html
370356 *
371357 * @return string
358+ * @throws \phpbu\App\Exception
372359 */
373360 protected function getHeaderHtml ()
374361 {
@@ -381,6 +368,7 @@ protected function getHeaderHtml()
381368 *
382369 * @param \phpbu\App\Result $result
383370 * @return string
371+ * @throws \phpbu\App\Exception
384372 */
385373 protected function getStatusHtml (Result $ result )
386374 {
@@ -422,10 +410,11 @@ protected function getStatusHtml(Result $result)
422410 }
423411
424412 /**
425- * Get error information.
413+ * Get error information
426414 *
427415 * @param \phpbu\App\Result $result
428416 * @return string
417+ * @throws \phpbu\App\Exception
429418 */
430419 protected function getErrorHtml (Result $ result )
431420 {
@@ -452,10 +441,11 @@ protected function getErrorHtml(Result $result)
452441 }
453442
454443 /**
455- * Return backup html information.
444+ * Return backup html information
456445 *
457446 * @param \phpbu\App\Result $result
458447 * @return string
448+ * @throws \phpbu\App\Exception
459449 */
460450 protected function getInfoHtml (Result $ result )
461451 {
@@ -543,9 +533,10 @@ protected function getInfoHtml(Result $result)
543533 }
544534
545535 /**
546- * Return mail body footer.
536+ * Return mail body footer
547537 *
548538 * @return string
539+ * @throws \phpbu\App\Exception
549540 */
550541 protected function getFooterHtml ()
551542 {
0 commit comments