Skip to content

Commit

Permalink
Use Symfony/mailer instead of deprecated SwiftMailer
Browse files Browse the repository at this point in the history
  • Loading branch information
tchapi committed Dec 19, 2021
1 parent 447165b commit 538b080
Show file tree
Hide file tree
Showing 12 changed files with 804 additions and 790 deletions.
9 changes: 3 additions & 6 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ APP_SECRET=630dc0d699fd37e720aff268f75583ed
DATABASE_URL=mysql://davis:[email protected]:3306/davis
###< doctrine/doctrine-bundle ###

###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=smtp://localhost:465?encryption=ssl&auth_mode=login&username=&password=
###< symfony/swiftmailer-bundle ###
###> symfony/mailer ###
MAILER_DSN=smtp://localhost:465?encryption=ssl&auth_mode=login&username=&password=
###< symfony/mailer ###

# The admin password for the backend
ADMIN_LOGIN=admin
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
"symfony/framework-bundle": "*",
"symfony/http-client": "*",
"symfony/intl": "*",
"symfony/mailer": "*",
"symfony/monolog-bundle": "^3.1",
"symfony/process": "*",
"symfony/property-access": "5.3.*",
"symfony/property-info": "5.3.*",
"symfony/proxy-manager-bridge": "5.3.*",
"symfony/security-bundle": "*",
"symfony/serializer": "5.3.*",
"symfony/swiftmailer-bundle": "^3.4",
"symfony/translation": "*",
"symfony/twig-bundle": "*",
"symfony/validator": "*",
Expand Down
1,404 changes: 737 additions & 667 deletions composer.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
];
4 changes: 0 additions & 4 deletions config/packages/dev/swiftmailer.yaml

This file was deleted.

3 changes: 3 additions & 0 deletions config/packages/mailer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
framework:
mailer:
dsn: '%env(MAILER_DSN)%'
3 changes: 0 additions & 3 deletions config/packages/swiftmailer.yaml

This file was deleted.

2 changes: 0 additions & 2 deletions config/packages/test/swiftmailer.yaml

This file was deleted.

28 changes: 5 additions & 23 deletions src/Controller/DAVController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment as TwigEnvironment;

class DAVController extends AbstractController
{
Expand Down Expand Up @@ -68,26 +68,9 @@ class DAVController extends AbstractController
*/
protected $tmpDir;

/**
* Doctrine entity manager.
*
* @var EntityManagerInterface
*/
protected $em;

/**
* The Twig engine.
*
* @var Twig\Environment
*/
protected $twig;
protected EntityManagerInterface $em;

/**
* The Swift_Mailer mailer service.
*
* @var \Swift_Mailer
*/
protected $mailer;
protected MailerInterface $mailer;

/**
* Base URI of the server.
Expand Down Expand Up @@ -117,7 +100,7 @@ class DAVController extends AbstractController
*/
protected $IMAPAuthUrl;

public function __construct(\Swift_Mailer $mailer, TwigEnvironment $twig, BasicAuth $basicAuthBackend, UrlGeneratorInterface $router, EntityManagerInterface $entityManager, bool $calDAVEnabled = true, bool $cardDAVEnabled = true, bool $webDAVEnabled = false, ?string $inviteAddress = null, ?string $authMethod = null, ?string $authRealm = null, ?string $publicDir = null, ?string $tmpDir = null, ?string $IMAPAuthUrl = null, ?string $mapboxApiKey = null)
public function __construct(MailerInterface $mailer, BasicAuth $basicAuthBackend, UrlGeneratorInterface $router, EntityManagerInterface $entityManager, bool $calDAVEnabled = true, bool $cardDAVEnabled = true, bool $webDAVEnabled = false, ?string $inviteAddress = null, ?string $authMethod = null, ?string $authRealm = null, ?string $publicDir = null, ?string $tmpDir = null, ?string $IMAPAuthUrl = null, ?string $mapboxApiKey = null)
{
$this->calDAVEnabled = $calDAVEnabled;
$this->cardDAVEnabled = $cardDAVEnabled;
Expand All @@ -131,7 +114,6 @@ public function __construct(\Swift_Mailer $mailer, TwigEnvironment $twig, BasicA
$this->tmpDir = $tmpDir;

$this->em = $entityManager;
$this->twig = $twig;
$this->mailer = $mailer;
$this->baseUri = $router->generate('dav', ['path' => '']);

Expand Down Expand Up @@ -227,7 +209,7 @@ private function initServer()
$this->server->addPlugin(new \Sabre\CalDAV\SharingPlugin());
$this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
if ($this->inviteAddress) {
$this->server->addPlugin(new DavisIMipPlugin($this->twig, $this->mailer, $this->inviteAddress, $this->mapboxApiKey));
$this->server->addPlugin(new DavisIMipPlugin($this->mailer, $this->inviteAddress, $this->mapboxApiKey));
}
}

Expand Down
101 changes: 36 additions & 65 deletions src/Plugins/DavisIMipPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Sabre\CalDAV\Schedule\IMipPlugin as SabreBaseIMipPlugin;
use Sabre\DAV;
use Sabre\VObject\ITip;
use Twig\Environment as TwigEnvironment;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;

/**
* iMIP handler.
Expand All @@ -14,23 +16,11 @@ class DavisIMipPlugin extends SabreBaseIMipPlugin
{
public const MESSAGE_ORIGIN_INDICATOR = '(via Davis)';

/**
* The Twig engine.
*
* @var Twig\Environment
*/
protected $twig;

/**
* The Swift_Mailer mailer service.
*
* @var \Swift_Mailer
*/
protected $mailer;
private MailerInterface $mailer;

protected $senderEmail;

protected $mapboxApiKey;
private string $mapboxApiKey;

/**
* Creates the email handler.
Expand All @@ -42,9 +32,8 @@ class DavisIMipPlugin extends SabreBaseIMipPlugin
* @param string $mapboxApiKey. The key to display the Mapbox static tile
* in the invitation email.
*/
public function __construct(TwigEnvironment $twig, \Swift_Mailer $mailer, string $senderEmail, ?string $mapboxApiKey = null)
public function __construct(MailerInterface $mailer, string $senderEmail, ?string $mapboxApiKey = null)
{
$this->twig = $twig;
$this->mailer = $mailer;
$this->senderEmail = $senderEmail;
$this->mapboxApiKey = $mapboxApiKey;
Expand Down Expand Up @@ -167,7 +156,7 @@ public function schedule(ITip\Message $itip)
$url = $notEmpty('URL', false);
$description = $notEmpty('DESCRIPTION', false);
$location = $notEmpty('LOCATION', false);
$locationImage = null;
$locationImagePath = null;
$locationImageContentId = false;
$locationLink = false;

Expand All @@ -182,8 +171,7 @@ public function schedule(ITip\Message $itip)
$zoom = 16;
$width = 500;
$height = 220;
$locationImage = \Swift_Image::fromPath(
'https://api.mapbox.com/styles/v1'.
$locationImagePath = 'https://api.mapbox.com/styles/v1'.
'/mapbox/streets-v11/static'.
'/pin-m-star+285A98'.
'('.$coordinates['longitude'].
Expand All @@ -193,9 +181,7 @@ public function schedule(ITip\Message $itip)
','.$coordinates['latitude'].
','.$zoom.
'/'.$width.'x'.$height.
'?access_token='.$this->mapboxApiKey)
->setFilename('event_map.png')
->setContentType('image/png');
'?access_token='.$this->mapboxApiKey;
}
$locationLink =
'http://www.openstreetmap.org'.
Expand All @@ -207,58 +193,43 @@ public function schedule(ITip\Message $itip)
}
}

$message = (new \Swift_Message($subject))
->setFrom([$this->senderEmail => $senderName.' '.static::MESSAGE_ORIGIN_INDICATOR])
->setTo([$recipientEmail => $recipientName])
->setReplyTo([$senderEmail => $senderName])
->setContentType('Content-Type: text/calendar; charset=UTF-8; method='.$itip->method);
$message = (new TemplatedEmail())
->from(new Address($this->senderEmail, $senderName.' '.static::MESSAGE_ORIGIN_INDICATOR))
->to(new Address($recipientEmail, $recipientName))
->replyTo(new Address($senderEmail, $senderName))
->subject($subject);

if (DAV\Server::$exposeVersion) {
$headers = $message->getHeaders();
$headers->addTextHeader('X-Sabre-Version: ', DAV\Version::VERSION);
$message->getHeaders()
->addTextHeader('X-Sabre-Version: ', DAV\Version::VERSION)
->addTextHeader('X-Auto-Response-Suppress', 'OOF, DR, RN, NRN, AutoReply');
}

if (null !== $locationImage) {
$locationImageContentId = $message->embed($locationImage);
if (null !== $locationImagePath) {
$locationImageContentId = 'map_image';
$message->embedFromPath($locationImagePath, $locationImageContentId);
}

// Now that we have everything, we can set the message body
$params = [
'senderName' => $senderName,
'summary' => $summary,
'action' => $action,
'dateTime' => $dateTime,
'allDay' => $allDay,
'attendees' => $attendees,
'location' => $location,
'locationImageContentId' => $locationImageContentId,
'locationLink' => $locationLink,
'url' => $url,
'description' => $description,
];

$message->setBody(
$this->twig->render(
'mails/scheduling.html.twig',
$params
),
'text/html'
)
->addPart(
$this->twig->render(
'mails/scheduling.txt.twig',
$params
),
'text/plain'
);
$message->htmlTemplate('mails/scheduling.html.twig')
->textTemplate('mails/scheduling.txt.twig')
->context([
'senderName' => $senderName,
'summary' => $summary,
'action' => $action,
'dateTime' => $dateTime,
'allDay' => $allDay,
'attendees' => $attendees,
'location' => $location,
'locationImageContentId' => $locationImageContentId,
'locationLink' => $locationLink,
'url' => $url,
'description' => $description,
]);

if (false === $deliveredLocally) {
// Attach the event file (invite.ics)
$attachment = (new \Swift_Attachment())
->setFilename('invite.ics')
->setContentType('text/calendar; method='.(string) $itip->method.'; charset=UTF-8')
->setBody($itip->message->serialize());
$message->attach($attachment);
$message->attach($itip->message->serialize(), 'invite.ics', 'text/calendar; method='.(string) $itip->method.'; charset=UTF-8');
}

$this->mailer->send($message);
Expand Down
35 changes: 18 additions & 17 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"composer/package-versions-deprecated": {
"version": "1.11.99"
},
"composer/pcre": {
"version": "1.0.0"
},
"composer/semver": {
"version": "1.5.1"
},
Expand Down Expand Up @@ -275,9 +278,6 @@
"config/packages/sensio_framework_extra.yaml"
]
},
"swiftmailer/swiftmailer": {
"version": "v6.2.3"
},
"symfony/asset": {
"version": "v5.1.5"
},
Expand Down Expand Up @@ -402,6 +402,18 @@
"symfony/intl": {
"version": "v5.1.5"
},
"symfony/mailer": {
"version": "5.3",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "4.3",
"ref": "bbfc7e27257d3a3f12a6fb0a42540a42d9623a37"
},
"files": [
"config/packages/mailer.yaml"
]
},
"symfony/maker-bundle": {
"version": "1.0",
"recipe": {
Expand All @@ -411,6 +423,9 @@
"ref": "fadbfe33303a76e25cb63401050439aa9b1a9c7f"
}
},
"symfony/mime": {
"version": "v5.3.11"
},
"symfony/monolog-bridge": {
"version": "v5.1.5"
},
Expand Down Expand Up @@ -539,20 +554,6 @@
"symfony/string": {
"version": "v5.1.5"
},
"symfony/swiftmailer-bundle": {
"version": "2.5",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "2.5",
"ref": "ae4d22af30bbd484506bc1817c5a3ef72c855b93"
},
"files": [
"config/packages/dev/swiftmailer.yaml",
"config/packages/swiftmailer.yaml",
"config/packages/test/swiftmailer.yaml"
]
},
"symfony/translation": {
"version": "3.3",
"recipe": {
Expand Down
2 changes: 1 addition & 1 deletion templates/mails/scheduling.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<tr>
<td colspan="2">
<a href="{{ locationLink }}" title="Navigate on this map">
<img src="{{ locationImageContentId }}" width="100%"/>
<img src="cid:{{ locationImageContentId }}" width="100%"/>
</a>
</td>
</tr>
Expand Down

0 comments on commit 538b080

Please sign in to comment.