Skip to content

Commit

Permalink
Merge pull request mautic#2174 from acquia/MAUT-11379
Browse files Browse the repository at this point in the history
Maut 11379 - Email Preheader
  • Loading branch information
avikarshasaha authored and escopecz committed May 29, 2024
1 parent 25e3c42 commit e76a52a
Show file tree
Hide file tree
Showing 13 changed files with 584 additions and 10 deletions.
48 changes: 47 additions & 1 deletion app/bundles/EmailBundle/Entity/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Mautic\LeadBundle\Entity\LeadList;
use Mautic\PageBundle\Entity\Page;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
Expand Down Expand Up @@ -58,8 +59,14 @@ class Email extends FormEntity implements VariantEntityInterface, TranslationEnt
*/
private $useOwnerAsMailer;

/**
* @Groups({"email:read", "email:write", "download:read"})
*/
private ?string $preheaderText = null;

/**
* @var string|null
* @Groups({"email:read", "email:write", "download:read"})
*/
private $fromAddress;

Expand Down Expand Up @@ -263,6 +270,7 @@ public static function loadMetadata(ORM\ClassMetadata $metadata): void

$builder->addIdColumns();
$builder->addNullableField('subject', Types::TEXT);
$builder->addNullableField('preheaderText', Types::STRING, 'preheader_text');
$builder->addNullableField('fromAddress', Types::STRING, 'from_address');
$builder->addNullableField('fromName', Types::STRING, 'from_name');
$builder->addNullableField('replyToAddress', Types::STRING, 'reply_to_address');
Expand Down Expand Up @@ -349,6 +357,26 @@ public static function loadValidatorMetadata(ClassMetadata $metadata): void
)
);

$metadata->addPropertyConstraint(
'subject',
new Length(
[
'max' => 190,
'maxMessage' => 'mautic.email.subject.length',
]
)
);

$metadata->addPropertyConstraint(
'preheaderText',
new Length(
[
'max' => 130,
'maxMessage' => 'mautic.email.preheader_text.length',
]
)
);

$metadata->addPropertyConstraint(
'fromAddress',
new EmailOrEmailTokenList(),
Expand Down Expand Up @@ -420,6 +448,7 @@ public static function loadApiMetadata(ApiMetadataDriver $metadata): void
'bccAddress',
'useOwnerAsMailer',
'utmTags',
'preheaderText',
'customHtml',
'plainText',
'template',
Expand Down Expand Up @@ -471,10 +500,13 @@ public function getName()
}

/**
* @param ?string $name
*
* @return $this
*/
public function setName($name)
{
$this->isChanged('name', $name);
$this->name = $name;

return $this;
Expand All @@ -489,12 +521,13 @@ public function getDescription()
}

/**
* @param mixed $description
* @param ?string $description
*
* @return Email
*/
public function setDescription($description)
{
$this->isChanged('description', $description);
$this->description = $description;

return $this;
Expand Down Expand Up @@ -733,6 +766,19 @@ public function setReplyToAddress($replyToAddress)
return $this;
}

public function getPreheaderText(): ?string
{
return $this->preheaderText;
}

public function setPreheaderText(?string $preheaderText): Email
{
$this->isChanged('preheaderText', $preheaderText);
$this->preheaderText = $preheaderText;

return $this;
}

/**
* @return mixed
*/
Expand Down
2 changes: 1 addition & 1 deletion app/bundles/EmailBundle/Event/EmailSendEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function inTokenizationMode(): bool
/**
* Returns the Email entity.
*
* @return Email|null
* @return ?Email
*/
public function getEmail()
{
Expand Down
27 changes: 27 additions & 0 deletions app/bundles/EmailBundle/EventListener/EmailSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

class EmailSubscriber implements EventSubscriberInterface
{
public const PREHEADER_HTML_ELEMENT_BEFORE = '<div class="preheader" style="font-size:1px;line-height:1px;display:none;color:#fff;max-height:0;max-width:0;opacity:0;overflow:hidden">';
public const PREHEADER_HTML_ELEMENT_AFTER = '</div>';
public const PREHEADER_HTML_SEARCH_PATTERN = '/<body[^>]*>.*?<div class="preheader"[^>]*>(.*?)<\/div>/s';
public const PREHEADER_HTML_REPLACE_PATTERN = '/<div class="preheader"[^>]*>(.*?)<\/div>/s';

private const RETRY_COUNT = 3;

public function __construct(
Expand All @@ -27,6 +32,8 @@ public static function getSubscribedEvents(): array
{
return [
EmailEvents::EMAIL_POST_SAVE => ['onEmailPostSave', 0],
EmailEvents::EMAIL_ON_SEND => ['onEmailSendAddPreheaderText', 200],
EmailEvents::EMAIL_ON_DISPLAY => ['onEmailSendAddPreheaderText', 200],
EmailEvents::EMAIL_POST_DELETE => ['onEmailDelete', 0],
EmailEvents::EMAIL_FAILED => ['onEmailFailed', 0],
EmailEvents::EMAIL_RESEND => ['onEmailResend', 0],
Expand All @@ -52,6 +59,26 @@ public function onEmailPostSave(Events\EmailEvent $event): void
}
}

/**
* Add preheader text to email body.
*/
public function onEmailSendAddPreheaderText(Events\EmailSendEvent $event): void
{
$email = $event->getEmail();
$html = $event->getContent();

if ($email && $email->getPreheaderText()) {
$preheaderTextElement = self::PREHEADER_HTML_ELEMENT_BEFORE.$email->getPreheaderText().self::PREHEADER_HTML_ELEMENT_AFTER;
$preheaderExists = preg_match(self::PREHEADER_HTML_SEARCH_PATTERN, $html, $preheaderMatches);
if ($preheaderExists) {
$html = preg_replace(self::PREHEADER_HTML_REPLACE_PATTERN, $preheaderTextElement, $html);
} elseif (preg_match('/(<body[^\>]*>)/i', $html, $contentMatches)) {
$html = str_ireplace($contentMatches[0], $contentMatches[0]."\n".$preheaderTextElement, $html);
}
$event->setContent($html);
}
}

/**
* Add a delete entry to the audit log.
*/
Expand Down
16 changes: 16 additions & 0 deletions app/bundles/EmailBundle/Form/Type/EmailType.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,22 @@ function (FormEvent $event) use ($variantSettingsModifier): void {
]
);

$builder->add(
$builder->create(
'preheaderText',
TextType::class,
[
'label' => 'mautic.email.preheader_text',
'label_attr' => ['class' => 'control-label'],
'attr' => [
'class' => 'form-control',
'tooltip' => 'mautic.email.preheader_text.tooltip',
],
'required' => false,
]
)
);

if (!empty($options['update_select'])) {
$builder->add(
'updateSelect',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@
<td>{{ email.bccAddress }}</td>
</tr>
{% endif %}
{% if email.getPreheaderText() %}
<tr>
<td width="20%">
<span class="fw-b">{{ 'mautic.email.preheader_text'|trans }}</span>
</td>
<td>{{ email.getPreheaderText() }}</td>
</tr>
{% endif %}
{% if email.headers %}
<tr>
<td width="20%">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<div class="pr-lg pl-lg pt-md pb-md">
{{ form_row(form.subject) }}
{{ form_row(form.name) }}
{{ form_row(form.preheaderText) }}
{% if isVariant %}
{{ form_row(form.variantSettings) }}
{{ form_row(form.isPublished) }}
Expand Down
Loading

0 comments on commit e76a52a

Please sign in to comment.