Skip to content

Commit

Permalink
Merge pull request mautic#4158 from escopecz/bug-email-address-apostr…
Browse files Browse the repository at this point in the history
…ophe

Bug email address apostrophe
  • Loading branch information
virlatinus authored May 30, 2017
2 parents b49f194 + ee599fb commit 13d78a3
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 13 deletions.
15 changes: 8 additions & 7 deletions app/bundles/EmailBundle/Helper/MailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1252,17 +1252,18 @@ public function setFrom($address, $name = null)
*/
public static function validateEmail($address)
{
static $grammer;
$invalidChar = strpbrk($address, '\'^&*%');

if ($grammer === null) {
$grammer = new \Swift_Mime_Grammar();
if ($invalidChar !== false) {
throw new \Swift_RfcComplianceException(
'Email address ['.$address.
'] contains this invalid character: '.substr($invalidChar, 0, 1)
);
}

if (!preg_match('/^'.$grammer->getDefinition('addr-spec').'$/D',
$address)) {
if (!filter_var($address, FILTER_VALIDATE_EMAIL)) {
throw new \Swift_RfcComplianceException(
'Address in mailbox given ['.$address.
'] does not comply with RFC 2822, 3.6.2.'
'Email address ['.$address.'] is invalid'
);
}
}
Expand Down
95 changes: 89 additions & 6 deletions app/bundles/EmailBundle/Tests/Helper/MailHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\EmailBundle\Entity\Email;
use Mautic\EmailBundle\Helper\MailHelper;
use Mautic\EmailBundle\MonitoredEmail\Mailbox;
use Mautic\EmailBundle\Swiftmailer\Exception\BatchQueueMaxException;
use Mautic\EmailBundle\Tests\Helper\Transport\BatchTransport;
Expand Down Expand Up @@ -79,7 +80,7 @@ public function testQueueModeThrowsExceptionWhenBatchLimitHit()

$swiftMailer = new \Swift_Mailer(new BatchTransport());

$mailer = new \Mautic\EmailBundle\Helper\MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']);
$mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']);

// Enable queue mode
$mailer->enableQueue();
Expand Down Expand Up @@ -107,7 +108,7 @@ public function testQueueModeDisabledDoesNotThrowsExceptionWhenBatchLimitHit()

$swiftMailer = new \Swift_Mailer(new BatchTransport());

$mailer = new \Mautic\EmailBundle\Helper\MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']);
$mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']);

// Enable queue mode
try {
Expand All @@ -128,7 +129,7 @@ public function testQueuedEmailFromOverride()
$transport = new BatchTransport();
$swiftMailer = new \Swift_Mailer($transport);

$mailer = new \Mautic\EmailBundle\Helper\MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']);
$mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']);
$mailer->enableQueue();

$email = new Email();
Expand Down Expand Up @@ -169,7 +170,7 @@ public function testBatchMode()
$transport = new BatchTransport(true);
$swiftMailer = new \Swift_Mailer($transport);

$mailer = new \Mautic\EmailBundle\Helper\MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']);
$mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']);
$mailer->enableQueue();

$email = new Email();
Expand Down Expand Up @@ -200,7 +201,7 @@ public function testQueuedOwnerAsMailer()
$transport = new BatchTransport();
$swiftMailer = new \Swift_Mailer($transport);

$mailer = new \Mautic\EmailBundle\Helper\MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']);
$mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']);
$mailer->enableQueue();

$mailer->setSubject('Hello');
Expand Down Expand Up @@ -255,7 +256,7 @@ public function testStandardOwnerAsMailer()
$transport = new SmtpTransport();
$swiftMailer = new \Swift_Mailer($transport);

$mailer = new \Mautic\EmailBundle\Helper\MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']);
$mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']);
$mailer->setBody('{signature}');

foreach ($this->contacts as $key => $contact) {
Expand All @@ -276,6 +277,88 @@ public function testStandardOwnerAsMailer()
}
}

public function testValidateValidEmails()
{
$helper = $this->mockEmptyMailHelper();
$addresses = [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
];

foreach ($addresses as $address) {
// will throw Swift_RfcComplianceException if it will find the address invalid
$helper::validateEmail($address);
}
}

public function testValidateEmailWithoutTld()
{
$heper = $this->mockEmptyMailHelper();
$this->setExpectedException(\Swift_RfcComplianceException::class);
$heper::validateEmail('john@doe');
}

public function testValidateEmailWithSpaceInIt()
{
$heper = $this->mockEmptyMailHelper();
$this->setExpectedException(\Swift_RfcComplianceException::class);
$heper::validateEmail('jo [email protected]');
}

public function testValidateEmailWithCaretInIt()
{
$heper = $this->mockEmptyMailHelper();
$this->setExpectedException(\Swift_RfcComplianceException::class);
$heper::validateEmail('jo^[email protected]');
}

public function testValidateEmailWithApostropheInIt()
{
$heper = $this->mockEmptyMailHelper();
$this->setExpectedException(\Swift_RfcComplianceException::class);
$heper::validateEmail('jo\'[email protected]');
}

public function testValidateEmailWithSemicolonInIt()
{
$heper = $this->mockEmptyMailHelper();
$this->setExpectedException(\Swift_RfcComplianceException::class);
$heper::validateEmail('jo;[email protected]');
}

public function testValidateEmailWithAmpersandInIt()
{
$heper = $this->mockEmptyMailHelper();
$this->setExpectedException(\Swift_RfcComplianceException::class);
$heper::validateEmail('jo&[email protected]');
}

public function testValidateEmailWithStarInIt()
{
$heper = $this->mockEmptyMailHelper();
$this->setExpectedException(\Swift_RfcComplianceException::class);
$heper::validateEmail('jo*[email protected]');
}

public function testValidateEmailWithPercentInIt()
{
$heper = $this->mockEmptyMailHelper();
$this->setExpectedException(\Swift_RfcComplianceException::class);
$heper::validateEmail('jo%[email protected]');
}

protected function mockEmptyMailHelper()
{
$mockFactory = $this->getMockFactory();
$transport = new SmtpTransport();
$swiftMailer = new \Swift_Mailer($transport);

return new MailHelper($mockFactory, $swiftMailer);
}

protected function getMockFactory($mailIsOwner = true)
{
$mockLeadRepository = $this->getMockBuilder(LeadRepository::class)
Expand Down

0 comments on commit 13d78a3

Please sign in to comment.