Skip to content

Commit 2750365

Browse files
Abandoned Swiftmailer over PHPMailer
It pains me but swiftmailer is is not working properly in the PHAR version of phpbu.
1 parent 3a3e10a commit 2750365

File tree

4 files changed

+50
-62
lines changed

4 files changed

+50
-62
lines changed

build.xml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
<include name="**/*.php"/>
121121
</fileset>
122122
</copy>
123-
<copy todir="${basedir}/build/phar/lib/collect">
123+
<copy todir="${basedir}/build/phar/lib/tightenco">
124124
<fileset dir="${basedir}/vendor/tightenco/collect/src">
125125
<include name="**/*.php"/>
126126
</fileset>
@@ -389,13 +389,12 @@
389389
</fileset>
390390
</copy>
391391

392-
<!-- SWIFTMAILER -->
393-
<copy file="${basedir}/vendor/swiftmailer/swiftmailer/LICENSE"
394-
tofile="${basedir}/build/phar/lib/swiftmailer/LICENSE"/>
395-
<copy todir="${basedir}/build/phar/lib/swiftmailer">
396-
<fileset dir="${basedir}/vendor/swiftmailer/swiftmailer/lib">
392+
<!-- PHPMAILER -->
393+
<copy file="${basedir}/vendor/phpmailer/phpmailer/LICENSE"
394+
tofile="${basedir}/build/phar/lib/phpmailer/LICENSE"/>
395+
<copy todir="${basedir}/build/phar/lib/phpmailer">
396+
<fileset dir="${basedir}/vendor/phpmailer/phpmailer/src">
397397
<include name="**/*.php"/>
398-
<exclude name="**/swift_required_pear.php"/>
399398
</fileset>
400399
</copy>
401400

build/phar-autoload.php.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ if ($execute) {
4545
require __PHPBU_PHAR_ROOT__ . '/lib/guzzlehttp/guzzle/functions_include.php';
4646
require __PHPBU_PHAR_ROOT__ . '/lib/guzzlehttp/psr7/functions_include.php';
4747
require __PHPBU_PHAR_ROOT__ . '/lib/guzzlehttp/promises/functions_include.php';
48-
// swift dependency init
49-
require __PHPBU_PHAR_ROOT__ . '/lib/swiftmailer/swift_init.php';
5048

5149
phpbu\App\Cmd::main();
5250
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"sebastian/environment": "~1.1|~2.0|~3.0",
3939
"sebastianfeldmann/cli": "~2.0",
4040
"sebastianfeldmann/ftp": "~0.9",
41-
"swiftmailer/swiftmailer": "~5.3|~6.0",
41+
"phpmailer/phpmailer": "~6.0",
4242
"symfony/event-dispatcher": "~2.6|~3.0|~4.0"
4343
},
4444
"require-dev": {

src/Log/Mail.php

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
use phpbu\App\Log\MailTemplate as TPL;
1010
use phpbu\App\Util\Arr;
1111
use 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

Comments
 (0)