Skip to content

Commit

Permalink
API Update SwiftMailer from v5 to v6
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Aug 9, 2021
1 parent 105a985 commit 5a5bbc8
Show file tree
Hide file tree
Showing 10 changed files with 508 additions and 14 deletions.
2 changes: 1 addition & 1 deletion _config/email.yml
Expand Up @@ -2,7 +2,7 @@
Name: emailconfig
---
SilverStripe\Core\Injector\Injector:
Swift_Transport: Swift_SendmailTransport
Swift_Transport: Swift_MailTransport
Swift_Mailer:
constructor:
- '%$Swift_Transport'
Expand Down
11 changes: 8 additions & 3 deletions docs/en/02_Developer_Guides/10_Email/index.md
Expand Up @@ -14,15 +14,20 @@ covers how to create an `Email` instance, customise it with a HTML template, the
Silverstripe CMS provides an API over the top of the [SwiftMailer](http://swiftmailer.org/) PHP library which comes with an
extensive list of "transports" for sending mail via different services.

Out of the box, Silverstripe CMS will use the built-in PHP `mail()` command via the `Swift_MailTransport` class. If you'd
like to use a more robust transport to send mail you can swap out the transport used by the `Mailer` via config:
Out of the box, Silverstripe CMS will use the built-in PHP `mail()` command via the `Swift_MailTransport` class.

It's highly recommended you upgrade to a more robust transport to send mail for additional security.

To swap out the transport used by the `Mailer`, create a file `app/_config/email.yml`

To use a `sendmail` binary:

```yml
SilverStripe\Core\Injector\Injector:
Swift_Transport: Swift_SendmailTransport
```

For example, to use SMTP, create a file `app/_config/email.yml`:
To use SMTP:

```yml
---
Expand Down
8 changes: 7 additions & 1 deletion src/Control/Email/Email.php
Expand Up @@ -10,6 +10,7 @@
use SilverStripe\Core\Convert;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\View\Requirements;
Expand Down Expand Up @@ -272,7 +273,9 @@ public function getSwiftMessage()
*/
public function setSwiftMessage($swiftMessage)
{
$swiftMessage->setDate(new DateTime());
$dateTime = new DateTime();
$dateTime->setTimestamp(DBDatetime::now()->getTimestamp());
$swiftMessage->setDate($dateTime);
if (!$swiftMessage->getFrom() && ($defaultFrom = $this->config()->get('admin_email'))) {
$swiftMessage->setFrom($defaultFrom);
}
Expand Down Expand Up @@ -454,6 +457,9 @@ public function addBCC($address, $name = null)
return $this;
}

/**
* @return mixed
*/
public function getReplyTo()
{
return $this->getSwiftMessage()->getReplyTo();
Expand Down
77 changes: 77 additions & 0 deletions src/Control/Email/Swift/MailTransport.php
@@ -0,0 +1,77 @@
<?php

/**
* This file was copied in from swiftmailer/swiftmailer v5.4.12 after it was removed from switftmailer v6
* It has been slightly modified to meet phpcs standards and initialise Swift_DependencyContainer
*/

/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE file (MIT)
* https://github.com/swiftmailer/swiftmailer/blob/181b89f18a90f8925ef805f950d47a7190e9b950/LICENSE
*/

/**
* Sends Messages using the mail() function.
*
* @author Chris Corbyn
*
* at deprecated since 5.4.5 (to be removed in 6.0)
*
* @internal
*/
// @codingStandardsIgnoreStart
// ignore missing namespace
class Swift_MailTransport extends Swift_Transport_MailTransport
// @codingStandardsIgnoreEnd
{
/**
* Create a new MailTransport, optionally specifying $extraParams.
*
* @param string $extraParams
*/
public function __construct($extraParams = '-f%s')
{
call_user_func_array(
[$this, 'Swift_Transport_MailTransport::__construct'],
$this->getDependencies()
);

$this->setExtraParams($extraParams);
}

/**
* Create a new MailTransport instance.
*
* @param string $extraParams To be passed to mail()
*
* @return self
*/
public static function newInstance($extraParams = '-f%s')
{
return new self($extraParams);
}

/**
* Add in deps for MailTransport which was removed as part of SwiftMailer v6
* @see transport_deps.php
*
* @return array
*/
private function getDependencies(): array
{
$deps = Swift_DependencyContainer::getInstance()->createDependenciesFor('transport.mail');
if (empty($deps)) {
Swift_DependencyContainer::getInstance()
->register('transport.mail')
->asNewInstanceOf('Swift_Transport_MailTransport')
->withDependencies(['transport.mailinvoker', 'transport.eventdispatcher'])
->register('transport.mailinvoker')
->asSharedInstanceOf('Swift_Transport_SimpleMailInvoker');
$deps = Swift_DependencyContainer::getInstance()->createDependenciesFor('transport.mail');
}
return $deps;
}
}
42 changes: 42 additions & 0 deletions src/Control/Email/Swift/Transport/MailInvoker.php
@@ -0,0 +1,42 @@
<?php

/**
* This file was copied in from swiftmailer/swiftmailer v5.4.12 after it was removed from switftmailer v6
* It has been slightly modified to meet phpcs standards
*/

/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE file (MIT)
* https://github.com/swiftmailer/swiftmailer/blob/181b89f18a90f8925ef805f950d47a7190e9b950/LICENSE
*/

/**
* This interface intercepts calls to the mail() function.
*
* @author Chris Corbyn
*
* @internal
*/
// @codingStandardsIgnoreStart
// ignore missing namespace
interface Swift_Transport_MailInvoker
// @codingStandardsIgnoreEnd
{
/**
* Send mail via the mail() function.
*
* This method takes the same arguments as PHP mail().
*
* @param string $to
* @param string $subject
* @param string $body
* @param string $headers
* @param string $extraParams
*
* @return bool
*/
public function mail($to, $subject, $body, $headers = null, $extraParams = null);
}

0 comments on commit 5a5bbc8

Please sign in to comment.