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 12, 2019
1 parent b5fb652 commit b20f889
Show file tree
Hide file tree
Showing 3 changed files with 322 additions and 6 deletions.
249 changes: 249 additions & 0 deletions site/app/Resources/views/template/email_template.html.twig
@@ -0,0 +1,249 @@
<!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="http://alistapart.com/article/can-email-be-responsive/" target="_blank"><img src="https://blogdotoshparkdotcom.files.wordpress.com/2017/06/screenshot-from-2017-06-14-20-41-11.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>
{# TODO:Update the link after merge of notification settings page #}
<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 by default.
You can snooze these notifications if you want to. For detailed preferences,
go to <a href="#">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>
71-75 Shelton Street, London, WC2H 9JQ, United Kingdom
</p>
</span>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>

</body>
</html>
1 change: 0 additions & 1 deletion site/app/config/packages/swiftmailer.yml
Expand Up @@ -5,4 +5,3 @@ swiftmailer:
port: "%mailer_port%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
78 changes: 73 additions & 5 deletions site/src/Consumer/EmailNotificationConsumer.php
Expand Up @@ -2,36 +2,104 @@

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
*
* @author Aquib Baig <aquibbaig97@gmail.com>
*/
class EmailNotificationConsumer extends AbstractNotificationConsumer
{
/**
* @var UserManagerInterface
*/
protected $userManager;

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

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

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

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

protected function shouldHandle()
{
$type = $this->notification->getType();
// TODO: Check subscription status also from database, after merge of #387
switch ($type) {
case "site_information":
$this->logger->info('Processing the notification and sending an email...');

return true;
default:
$this->logger->error('Something went wrong. Please check the notification type');

return false;
}
}

/**
* 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());

$mailer = $this->container->getParameter('mailer_user');

$message = (new \Swift_Message($this->notification->getSubject()))
// Set sender's email from a yaml file
->setFrom($mailer)
->setTo($userEmail)
->setBody($this->twig->render('template/email_template.html.twig', [
'username' => $this->notification->getRecipient()->getUsername(),
'content' => $this->notification->getMessage(),
]), 'text/html')
// Alternate body for users who have text/plain preferences
->addPart($this->notification->getMessage(), 'text/plain');

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

return true;
}
}
Expand Down

0 comments on commit b20f889

Please sign in to comment.