Add congratulatory MSS sending email & endpoint to send it

[MAILPOET-2831]
This commit is contained in:
Jan Jakeš
2020-04-13 12:13:53 +02:00
committed by Veljko V
parent e748391602
commit 65b75453ab
6 changed files with 251 additions and 0 deletions

View File

@ -5,6 +5,7 @@ namespace MailPoet\API\JSON\v1;
use MailPoet\Analytics\Analytics as AnalyticsHelper;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;
use MailPoet\API\JSON\Response;
use MailPoet\Config\AccessControl;
use MailPoet\Config\Installer;
use MailPoet\Config\ServicesChecker;
@ -12,6 +13,7 @@ use MailPoet\Cron\Workers\KeyCheck\PremiumKeyCheck;
use MailPoet\Cron\Workers\KeyCheck\SendingServiceKeyCheck;
use MailPoet\Mailer\MailerLog;
use MailPoet\Services\Bridge;
use MailPoet\Services\CongratulatoryMssEmailController;
use MailPoet\Services\SPFCheck;
use MailPoet\Settings\SettingsController;
use MailPoet\WP\DateTime;
@ -42,6 +44,9 @@ class Services extends APIEndpoint {
/** @var ServicesChecker */
private $servicesChecker;
/** @var CongratulatoryMssEmailController */
private $congratulatoryMssEmailController;
/** @var WPFunctions */
private $wp;
@ -57,6 +62,7 @@ class Services extends APIEndpoint {
SendingServiceKeyCheck $mssWorker,
PremiumKeyCheck $premiumWorker,
ServicesChecker $servicesChecker,
CongratulatoryMssEmailController $congratulatoryMssEmailController,
WPFunctions $wp
) {
$this->bridge = $bridge;
@ -67,6 +73,7 @@ class Services extends APIEndpoint {
$this->premiumWorker = $premiumWorker;
$this->dateTime = new DateTime();
$this->servicesChecker = $servicesChecker;
$this->congratulatoryMssEmailController = $congratulatoryMssEmailController;
$this->wp = $wp;
}
@ -226,6 +233,37 @@ class Services extends APIEndpoint {
return $this->successResponse();
}
public function sendCongratulatoryMssEmail() {
if (!Bridge::isMPSendingServiceEnabled()) {
return $this->createBadRequest(__('MailPoet Sending Service is not active.', 'mailpoet'));
}
$authorizedEmails = $this->bridge->getAuthorizedEmailAddresses();
if (!$authorizedEmails) {
return $this->createBadRequest(__('No FROM email addresses are authorized.', 'mailpoet'));
}
$fromEmail = $this->settings->get('sender.address');
if (!$fromEmail) {
return $this->createBadRequest(__('Sender email address is not set.', 'mailpoet'));
}
if (!in_array($fromEmail, $authorizedEmails, true)) {
return $this->createBadRequest(sprintf(__("Sender email address '%s' is not authorized.", 'mailpoet'), $fromEmail));
}
try {
// congratulatory email is sent to the current FROM address (authorized at this point)
$this->congratulatoryMssEmailController->sendCongratulatoryEmail($fromEmail);
} catch (\Throwable $e) {
return $this->errorResponse([
APIError::UNKNOWN => __('Sending of congratulatory email failed.', 'mailpoet'),
], [], Response::STATUS_UNKNOWN);
}
return $this->successResponse([
'email_address' => $fromEmail,
]);
}
private function getErrorDescriptionByCode($code) {
switch ($code) {
case Bridge::CHECK_ERROR_UNAVAILABLE:
@ -241,4 +279,10 @@ class Services extends APIEndpoint {
return $text;
}
private function createBadRequest(string $message) {
return $this->badRequest([
APIError::BAD_REQUEST => $message,
]);
}
}

View File

@ -238,6 +238,7 @@ class ContainerConfigurator implements IContainerConfigurator {
// Services
$container->autowire(\MailPoet\Services\Bridge::class)->setPublic(true);
$container->autowire(\MailPoet\Services\AuthorizedEmailsController::class);
$container->autowire(\MailPoet\Services\CongratulatoryMssEmailController::class);
$container->autowire(\MailPoet\Services\SPFCheck::class)->setPublic(true);
// Tasks
$container->autowire(\MailPoet\Tasks\State::class);

View File

@ -0,0 +1,43 @@
<?php declare(strict_types = 1);
namespace MailPoet\Services;
use MailPoet\Config\Renderer;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MetaInfo;
class CongratulatoryMssEmailController {
/** @var Mailer */
private $mailer;
/** @var MetaInfo */
private $mailerMetaInfo;
/** @var Renderer */
private $renderer;
public function __construct(
Mailer $mailer,
MetaInfo $mailerMetaInfo,
Renderer $renderer
) {
$this->mailer = $mailer;
$this->mailerMetaInfo = $mailerMetaInfo;
$this->renderer = $renderer;
}
public function sendCongratulatoryEmail(string $toEmailAddress) {
$renderedNewsletter = [
'subject' => _x('Sending with MailPoet works!', 'Subject of an email confirming that MailPoet Sending Service works', 'mailpoet'),
'body' => [
'html' => $this->renderer->render('emails/congratulatoryMssEmail.html'),
'text' => $this->renderer->render('emails/congratulatoryMssEmail.txt'),
],
];
$extraParams = [
'meta' => $this->mailerMetaInfo->getSendingTestMetaInfo(),
];
$this->mailer->send($renderedNewsletter, $toEmailAddress, $extraParams);
}
}

View File

@ -13,6 +13,7 @@ use MailPoet\Cron\Workers\KeyCheck\SendingServiceKeyCheck;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MailerLog;
use MailPoet\Services\Bridge;
use MailPoet\Services\CongratulatoryMssEmailController;
use MailPoet\Services\SPFCheck;
use MailPoet\Settings\SettingsController;
use MailPoet\Settings\SettingsRepository;
@ -487,6 +488,7 @@ class ServicesTest extends \MailPoetTest {
$this->diContainer->get(SendingServiceKeyCheck::class),
$this->diContainer->get(PremiumKeyCheck::class),
$this->diContainer->get(ServicesChecker::class),
$this->diContainer->get(CongratulatoryMssEmailController::class),
$this->diContainer->get(WPFunctions::class)
);
}
@ -500,6 +502,7 @@ class ServicesTest extends \MailPoetTest {
$this->diContainer->get(SendingServiceKeyCheck::class),
$this->diContainer->get(PremiumKeyCheck::class),
$this->diContainer->get(ServicesChecker::class),
$this->diContainer->get(CongratulatoryMssEmailController::class),
$this->diContainer->get(WPFunctions::class)
);
}

View File

@ -0,0 +1,155 @@
<html lang="en" style="margin:0;padding:0">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="format-detection" content="telephone=no" />
<title>Subject</title>
<style type="text/css"> @media screen and (max-width: 480px) {
.mailpoet_button {width:100% !important;}
}
@media screen and (max-width: 599px) {
.mailpoet_header {
padding: 10px 20px;
}
.mailpoet_button {
width: 100% !important;
padding: 5px 0 !important;
box-sizing:border-box !important;
}
div, .mailpoet_cols-two, .mailpoet_cols-three {
max-width: 100% !important;
}
}
</style>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" style="margin:0;padding:0;background-color:#f0f0f0">
<table class="mailpoet_template" border="0" width="100%" cellpadding="0" cellspacing="0" style="border-collapse:collapse;border-spacing:0;mso-table-lspace:0;mso-table-rspace:0">
<tbody>
<tr>
<td class="mailpoet_preheader" style="border-collapse:collapse;display:none;visibility:hidden;mso-hide:all;font-size:1px;color:#333333;line-height:1px;max-height:0;max-width:0;opacity:0;overflow:hidden;-webkit-text-size-adjust:none" height="1">
</td>
</tr>
<tr>
<td align="center" class="mailpoet-wrapper" valign="top" style="border-collapse:collapse;background-color:#f0f0f0"><!--[if mso]>
<table align="center" border="0" cellspacing="0" cellpadding="0"
width="660">
<tr>
<td class="mailpoet_content-wrapper" align="center" valign="top" width="660">
<![endif]--><table class="mailpoet_content-wrapper" border="0" width="660" cellpadding="0" cellspacing="0" style="border-collapse:collapse;background-color:#ffffff;border-spacing:0;mso-table-lspace:0;mso-table-rspace:0;max-width:660px;width:100%">
<tbody>
<tr>
<td class="mailpoet_content" align="center" style="border-collapse:collapse">
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-collapse:collapse;border-spacing:0;mso-table-lspace:0;mso-table-rspace:0">
<tbody>
<tr>
<td style="border-collapse:collapse;padding-left:0;padding-right:0">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="mailpoet_cols-one" style="border-collapse:collapse;border-spacing:0;mso-table-lspace:0;mso-table-rspace:0;table-layout:fixed;margin-left:auto;margin-right:auto;padding-left:0;padding-right:0">
<tbody>
<tr>
<td class="mailpoet_spacer" height="36" valign="top" style="border-collapse:collapse"></td>
</tr>
<tr>
<td class="mailpoet_image mailpoet_padded_vertical mailpoet_padded_side" align="center" valign="top" style="border-collapse:collapse;padding-top:10px;padding-bottom:10px;padding-left:20px;padding-right:20px">
<img src="http://newsletters.mailpoet.com/wp-content/uploads/2018/10/new_logo_orange.png" width="80" alt="new_logo_orange" style="height:auto;max-width:100%;-ms-interpolation-mode:bicubic;border:0;display:block;outline:none;text-align:center" />
</td>
</tr>
<tr>
<td class="mailpoet_spacer" height="26" valign="top" style="border-collapse:collapse"></td>
</tr>
<tr>
<td class="mailpoet_text mailpoet_padded_vertical mailpoet_padded_side" valign="top" style="border-collapse:collapse;padding-top:10px;padding-bottom:10px;padding-left:20px;padding-right:20px;word-break:break-word;word-wrap:break-word">
<h1 style="margin:0 0 12px;color:#111111;font-family:'Trebuchet MS','Lucida Grande','Lucida Sans Unicode','Lucida Sans',Tahoma,sans-serif;font-size:40px;line-height:64px;margin-bottom:0;text-align:center;padding:0;font-style:normal;font-weight:normal"><strong><%= __('Congrats!') %><br /><%= __('MailPoet is now sending your emails') %></strong></h1>
</td>
</tr>
<tr>
<td class="mailpoet_spacer" height="41" valign="top" style="border-collapse:collapse"></td>
</tr>
<tr>
<td class="mailpoet_text mailpoet_padded_vertical mailpoet_padded_side" valign="top" style="border-collapse:collapse;padding-top:10px;padding-bottom:10px;padding-left:20px;padding-right:20px;word-break:break-word;word-wrap:break-word">
<table style="border-collapse:collapse;border-spacing:0;mso-table-lspace:0;mso-table-rspace:0" width="100%" cellpadding="0">
<tr>
<td class="mailpoet_paragraph" style="border-collapse:collapse;color:#000000;font-family:Arial,'Helvetica Neue',Helvetica,sans-serif;font-size:16px;line-height:25.6px;word-break:break-word;word-wrap:break-word;text-align:center">
<%= __('This email was sent automatically with the MailPoet Sending Service after you activated your key in your MailPoet settings.') %>
</td>
</tr></table>
</td>
</tr>
<tr>
<td class="mailpoet_spacer" height="55" valign="top" style="border-collapse:collapse"></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td class="mailpoet_content-cols-two" align="left" style="border-collapse:collapse;background-color:#fe5301!important" bgcolor="#fe5301">
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-collapse:collapse;border-spacing:0;mso-table-lspace:0;mso-table-rspace:0">
<tbody>
<tr>
<td align="center" style="border-collapse:collapse;font-size:0"><!--[if mso]>
<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td width="330" valign="top">
<![endif]--><div style="display:inline-block; max-width:330px; vertical-align:top; width:100%;">
<table width="330" class="mailpoet_cols-two" border="0" cellpadding="0" cellspacing="0" align="left" style="border-collapse:collapse;width:100%;max-width:330px;border-spacing:0;mso-table-lspace:0;mso-table-rspace:0;table-layout:fixed;margin-left:auto;margin-right:auto;padding-left:0;padding-right:0">
<tbody>
<tr>
<td class="mailpoet_spacer" bgcolor="#fe5301" height="22" valign="top" style="border-collapse:collapse"></td>
</tr>
<tr>
<td class="mailpoet_image mailpoet_padded_vertical mailpoet_padded_side" align="left" valign="top" style="border-collapse:collapse;padding-top:10px;padding-bottom:10px;padding-left:20px;padding-right:20px">
<img src="http://newsletters.mailpoet.com/wp-content/uploads/2018/10/new_logo_white-300x95.png" width="130" alt="new_logo_white" style="height:auto;max-width:100%;-ms-interpolation-mode:bicubic;border:0;display:block;outline:none;text-align:center" />
</td>
</tr>
<tr>
<td class="mailpoet_spacer" height="26" valign="top" style="border-collapse:collapse"></td>
</tr>
</tbody>
</table>
</div><!--[if mso]>
</td>
<td width="330" valign="top">
<![endif]--><div style="display:inline-block; max-width:330px; vertical-align:top; width:100%;">
<table width="330" class="mailpoet_cols-two" border="0" cellpadding="0" cellspacing="0" align="left" style="border-collapse:collapse;width:100%;max-width:330px;border-spacing:0;mso-table-lspace:0;mso-table-rspace:0;table-layout:fixed;margin-left:auto;margin-right:auto;padding-left:0;padding-right:0">
<tbody>
<tr>
<td class="mailpoet_spacer" bgcolor="#fe5301" height="20" valign="top" style="border-collapse:collapse"></td>
</tr>
<tr>
<td class="mailpoet_header_footer_padded mailpoet_footer" style="border-collapse:collapse;padding:10px 20px;line-height:25.6px;text-align:right;color:#222222;font-family:Arial, 'Helvetica Neue', Helvetica, sans-serif;font-size:16px">
</td>
</tr>
</tbody>
</table>
</div><!--[if mso]>
</td>
</tr>
</tbody>
</table>
<![endif]--></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table><!--[if mso]>
</td>
</tr>
</table>
<![endif]--></td>
</tr>
</tbody>
</table>
</body>
</html>

View File

@ -0,0 +1,5 @@
<%= __('Congrats!') %>
<%= __('MailPoet is now sending your emails') %>
<%= __('This email was sent automatically with the MailPoet Sending Service after you activated your key in your MailPoet settings.') %>