Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
Email Notification Consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
aquibbaig committed Aug 20, 2019
1 parent cb4f8dd commit fb091b8
Show file tree
Hide file tree
Showing 4 changed files with 342 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>
New Notification from LibreCores
</title>
<!--
SALTED | A RESPONSIVE EMAIL TEMPLATE
=====================================
Based on code used and tested by Litmus (@litmusapp)
Originally developed by Kevin Mandeville (@KEVINgotbounce)
Cleaned up by Jason Rodriguez (@rodriguezcommaj)
Presented by A List Apart (@alistapart)
Email is surprisingly hard. While this has been thoroughly tested, your mileage may vary.
It's highly recommended that you test using a service like Litmus and your own devices.
Enjoy!
-->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<style type="text/css">
/* CLIENT-SPECIFIC STYLES */
#outlook a{padding:0;} /* Force Outlook to provide a "view in browser" message */
.ReadMsgBody{width:100%;} .ExternalClass{width:100%;} /* Force Hotmail to display emails at full width */
.ExternalClass, .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td, .ExternalClass div {line-height: 100%;} /* Force Hotmail to display normal line spacing */
body, table, td, a{-webkit-text-size-adjust:100%; -ms-text-size-adjust:100%;} /* Prevent WebKit and Windows mobile changing default text sizes */
table, td{mso-table-lspace:0pt; mso-table-rspace:0pt;} /* Remove spacing between tables in Outlook 2007 and up */
img{-ms-interpolation-mode:bicubic;} /* Allow smoother rendering of resized image in Internet Explorer */
/* RESET STYLES */
body{margin:0; padding:0;}
img{border:0; height:auto; line-height:100%; outline:none; text-decoration:none;}
table{border-collapse:collapse !important;}
body{height:100% !important; margin:0; padding:0; width:100% !important;}
/* iOS BLUE LINKS */
.appleBody a {color:#68440a; text-decoration: none;}
.appleFooter a {color:#999999; text-decoration: none;}
/* MOBILE STYLES */
@media screen and (max-width: 525px) {
/* ALLOWS FOR FLUID TABLES */
table[class="wrapper"]{
width:100% !important;
}
/* ADJUSTS LAYOUT OF LOGO IMAGE */
td[class="logo"]{
text-align: left;
padding: 20px 0 20px 0 !important;
}
td[class="logo"] img{
margin:0 auto!important;
}
/* USE THESE CLASSES TO HIDE CONTENT ON MOBILE */
td[class="mobile-hide"]{
display:none;}
img[class="mobile-hide"]{
display: none !important;
}
img[class="img-max"]{
max-width: 100% !important;
height:auto !important;
}
/* FULL-WIDTH TABLES */
table[class="responsive-table"]{
width:100%!important;
}
/* UTILITY CLASSES FOR ADJUSTING PADDING ON MOBILE */
td[class="padding"]{
padding: 10px 5% 15px 5% !important;
}
td[class="padding-copy"]{
padding: 10px 5% 10px 5% !important;
text-align: center;
}
td[class="padding-meta"]{
padding: 30px 5% 0px 5% !important;
text-align: center;
}
td[class="no-pad"]{
padding: 0 0 20px 0 !important;
}
td[class="no-padding"]{
padding: 0 !important;
}
td[class="section-padding"]{
padding: 50px 15px 50px 15px !important;
}
td[class="section-padding-bottom-image"]{
padding: 50px 15px 0 15px !important;
}
/* ADJUST BUTTONS ON MOBILE */
td[class="mobile-wrapper"]{
padding: 10px 5% 15px 5% !important;
}
table[class="mobile-button-container"]{
margin:0 auto;
width:100% !important;
}
a[class="mobile-button"]{
width:80% !important;
padding: 15px !important;
border: 0 !important;
font-size: 16px !important;
}
}
</style>
</head>
<body style="margin: 0; padding: 0;">

{#<!-- HEADER -->#}
{#<table border="0" cellpadding="0" cellspacing="0" width="100%" style="table-layout: fixed;">#}
{#<tr>#}
{#<td bgcolor="#ffffff">#}
{#<div align="center" style="padding: 0px 15px 0px 15px;">#}
{#<table border="0" cellpadding="0" cellspacing="0" width="500" class="wrapper">#}
{#<!-- LOGO/PREHEADER TEXT -->#}
{#<tr>#}
{#<td style="padding: 20px 0px 30px 0px;" class="logo">#}
{#<table border="0" cellpadding="0" cellspacing="0" width="100%">#}
{#<tr>#}
{#<td bgcolor="#fff" width="100" align="center"><a href="https://www.librecores.org" target="_blank"><img src="https://www.librecores.org/img/logo_small.png" width="200" height="78" style="display: block; font-family: Helvetica, Arial, sans-serif; color: #666666; font-size: 16px;" border="0"></a></td>#}
{#</tr>#}
{#</table>#}
{#</td>#}
{#</tr>#}
{#</table>#}
{#</div>#}
{#</td>#}
{#</tr>#}
{#</table>#}

<!-- ONE COLUMN SECTION -->
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="table-layout: fixed;">
<tr>
<td bgcolor="#ffffff" align="center" class="section-padding">
<table border="0" cellpadding="0" cellspacing="0" width="500" class="responsive-table">
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<!-- COPY -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" style="font-size: 25px; font-family: Helvetica, Arial, sans-serif; color: rgba(63, 176, 172, 1); padding-top: 30px;" class="padding-copy">Hi {{ username }},</td>
</tr>
<tr>
<td align="left" style="font-size:14px;padding: 20px 0 5px 0;border-bottom: 2px solid #e2e2e2">You have a new Notification from <b>LibreCores</b>.</td>
</tr>
<tr>
<td align="justify" style="padding: 20px 0 0 0; font-size: 16px; line-height: 25px; font-family: Helvetica, Arial, sans-serif; color: #666666;" class="padding-copy">
<p>{{ content }}</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>

<!-- ONE COLUMN W/ BOTTOM IMAGE SECTION -->
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="table-layout: fixed;">
<tr>
<td bgcolor="#f8f8f8" align="center" style="padding: 70px 15px 70px 15px;" class="section-padding-bottom-image">
<table border="0" cellpadding="0" cellspacing="0" width="500" class="responsive-table">
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<!-- COPY -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="justify" style="padding: 10px 0 0 0; font-size: 16px; line-height: 25px; font-family: Helvetica, Arial, sans-serif; color: #666666;" class="padding-copy">
You are recieving this notification because you are subscribed to email notifications from LibreCores.
You can unsubscribe from these notifications if you want to. For detailed preferences,
go to <a href="{{ notificationSettingsUrl }}">notification settings</a> of your LibreCores account.
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>

<!-- FOOTER -->
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="table-layout: fixed;">
<tr>
<td bgcolor="#ffffff" align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td style="padding: 20px 0px 20px 0px;">
<!-- UNSUBSCRIBE COPY -->
<table width="500" border="0" cellspacing="0" cellpadding="0" align="center" class="responsive-table">
<tr>
<td align="center" valign="middle"
style="font-size: 12px; line-height: 18px; font-family: Helvetica, Arial, sans-serif; color:#666666;">
<span class="appleFooter" style="color:#666666;">
<p>
LibreCores is a project by The Free and Open Source Silicon Foundation C.i.C.<br>
Unit C5, Tenterfields Business Park, Halifax, England, HX2 6EQ, United Kingdom
</p>
</span>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Hi {{ username }},

You have a new notification from LibreCores.

{{ content }}

You are recieving this notification because you are subscribed
to email notifications from LibreCores. You can unsubscribe from
these notifications if you want to. For detailed preferences, go
to {{ notificationSettingsUrl }} of your LibreCores account.

LibreCores is a project by The Free and Open Source Silicon Foundation C.i.C.
Unit C5, Tenterfields Business Park, Halifax, England, HX2 6EQ, United Kingdom
1 change: 0 additions & 1 deletion site/app/config/packages/swiftmailer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ swiftmailer:
port: "%mailer_port%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
86 changes: 81 additions & 5 deletions site/src/Consumer/EmailNotificationConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace App\Consumer;

use App\Util\Notification;
use FOS\UserBundle\Model\UserManagerInterface;
use Psr\Log\LoggerInterface;
use Twig\Environment;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Sends out Notifications over email
Expand All @@ -12,26 +14,100 @@
*/
class EmailNotificationConsumer extends AbstractNotificationConsumer
{
/**
* @var UserManagerInterface
*/
protected $userManager;

/**
* @var \Swift_Mailer
*/
protected $mailer;

/**
* @var Environment
*/
protected $twigEnvironment;

/**
* @var ContainerInterface
*/
protected $container;

/**
* EmailNotificationConsumer constructor
*
* @param LoggerInterface $logger
* @param LoggerInterface $logger
* @param UserManagerInterface $userManager
* @param Environment $twigEnvironment
* @param \Swift_Mailer $mailer
* @param ContainerInterface $container
*/
public function __construct(LoggerInterface $logger)
{
public function __construct(
LoggerInterface $logger,
UserManagerInterface $userManager,
Environment $twigEnvironment,
ContainerInterface $container,
\Swift_Mailer $mailer
) {
parent::__construct($logger);
$this->userManager = $userManager;
$this->twigEnvironment = $twigEnvironment;
$this->container = $container;
$this->mailer = $mailer;
}

protected function shouldHandle()
{
$user = $this->userManager->findUserByUsername($this->notification->getRecipient()->getUsername());

$emailSubscription = $user->isSubscribedToEmailNotifications();

if (!$emailSubscription) {
$this->logger->info('You have unsubscribed to email notifications from LibreCores');

return false;
}
$this->logger->info('Processing the notification and sending an email...');

return true;
}

/**
* Handles an email Notification
*
* @return bool
*
* @throws \Exception
*/
protected function handle(): bool
{
// TODO: Process the Notification($this->notification)
echo "Email Notification Consumer";

$userEmail = $this->userManager->findUserByUsername($this->notification->getRecipient()->getUsername());

$settingsUrl = $this->container->getParameter(
'app.librecores_url'
)."/user/settings/notifications";

$message = (new \Swift_Message($this->notification->getSubject()))
// Set sender's email from dev.secrets.yml
->setFrom($this->container->getParameter('notification_mailer_user'))
->setTo($userEmail)
->setBody($this->twigEnvironment->render('template/email_notification_template.html.twig', [
'username' => $this->notification->getRecipient()->getUsername(),
'content' => $this->notification->getMessage(),
'notificationSettingsUrl' => $settingsUrl,
]), 'text/html')
// Alternate body for users who have text/plain preferences
->addPart($this->twigEnvironment->render('template/email_notification_template.txt.twig', [
'username' => $this->notification->getRecipient()->getUsername(),
'content' => $this->notification->getMessage(),
'notificationSettingsUrl' => $settingsUrl,
]), 'text/plain');

$this->mailer->send($message);

return true;
}
}
Expand Down

0 comments on commit fb091b8

Please sign in to comment.