Skip to content
This repository has been archived by the owner on Nov 30, 2017. It is now read-only.

Commit

Permalink
Merge pull request #22 from ruudk/patch-2
Browse files Browse the repository at this point in the history
Properly escape quotes in setFrom, addTo etc.
  • Loading branch information
miguel250 committed Sep 12, 2013
2 parents 65759c3 + 296ef34 commit 95a84ae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
25 changes: 20 additions & 5 deletions Postmark/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ public function __construct(HTTPClient $client, $from_email, $from_name = null)
public function setFrom($email, $name = null)
{
if (!empty($name)) {
$email = "{$name} <{$email}>";
$email = sprintf('"%s" <%s>',
str_replace('"', '', $name),
$email
);
}

$this->from = $email;
Expand All @@ -151,7 +154,10 @@ public function setFrom($email, $name = null)
public function addTo($email, $name = null)
{
if (!empty($name)) {
$email = "{$name} <{$email}>";
$email = sprintf('"%s" <%s>',
str_replace('"', '', $name),
$email
);
}

$this->to[] = $email;
Expand All @@ -169,7 +175,10 @@ public function addTo($email, $name = null)
public function addCC($email, $name = null)
{
if (!empty($name)) {
$email = "{$name} <{$email}>";
$email = sprintf('"%s" <%s>',
str_replace('"', '', $name),
$email
);
}

$this->cc[] = $email;
Expand All @@ -187,7 +196,10 @@ public function addCC($email, $name = null)
public function addBCC($email, $name = null)
{
if (!empty($name)) {
$email = "{$name} <{$email}>";
$email = sprintf('"%s" <%s>',
str_replace('"', '', $name),
$email
);
}

$this->bcc[] = $email;
Expand All @@ -205,7 +217,10 @@ public function addBCC($email, $name = null)
public function setReplyTo($email, $name = null)
{
if (!empty($name)) {
$email = "{$name} <{$email}>";
$email = sprintf('"%s" <%s>',
str_replace('"', '', $name),
$email
);
}

$this->replyTo = $email;
Expand Down
14 changes: 7 additions & 7 deletions Tests/Postmark/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testSendMessage()
$message->addAttachment(new File(__FILE__), 'attachment.php', 'text/plain'); // Attachment with custom filename and mimetype
$response = json_decode($message->send(), true);

$this->assertEquals('Test Test <[email protected]>', $response['To']);
$this->assertEquals('"Test Test" <[email protected]>', $response['To']);
$this->assertEquals(0, $response['ErrorCode']);
$this->assertEquals('Test job accepted', $response['Message']);
}
Expand All @@ -57,7 +57,7 @@ public function testSendMultipleMessages()
$message->addAttachment(new File(__FILE__), 'attachment.php'); // Attachment with custom filename
$response = json_decode($message->send(), true);

$this->assertEquals('Test Test <[email protected]>', $response['To']);
$this->assertEquals('"Test Test" <[email protected]>', $response['To']);
$this->assertEquals(0, $response['ErrorCode']);
$this->assertEquals('Test job accepted', $response['Message']);

Expand All @@ -68,7 +68,7 @@ public function testSendMultipleMessages()
$message->addAttachment(new File(__FILE__), 'attachment.php'); // Attachment without custom filename or mimetype
$response = json_decode($message->send(), true);

$this->assertEquals('Test Test <[email protected]>', $response['To']);
$this->assertEquals('"Test Test" <[email protected]>', $response['To']);
$this->assertEquals(0, $response['ErrorCode']);
$this->assertEquals('Test job accepted', $response['Message']);

Expand All @@ -78,7 +78,7 @@ public function testSendMultipleMessages()
$message->setHTMLMessage('<b>second email body</b>');
$response = json_decode($message->send(), true);

$this->assertEquals('Test Test <[email protected]>', $response['To']);
$this->assertEquals('"Test Test" <[email protected]>', $response['To']);
$this->assertEquals(0, $response['ErrorCode']);
$this->assertEquals('Test job accepted', $response['Message']);
}
Expand Down Expand Up @@ -115,15 +115,15 @@ public function testSendMultipleMessagesViaBatch()
$this->assertEquals(1, count($responses));
$response = json_decode($responses[0], true);

$this->assertEquals('Test Test <[email protected]>', $response[0]['To']);
$this->assertEquals('"Test Test" <[email protected]>', $response[0]['To']);
$this->assertEquals(0, $response[0]['ErrorCode']);
$this->assertEquals('Test job accepted', $response[0]['Message']);

$this->assertEquals('Test Test <[email protected]>', $response[1]['To']);
$this->assertEquals('"Test Test" <[email protected]>', $response[1]['To']);
$this->assertEquals(0, $response[1]['ErrorCode']);
$this->assertEquals('Test job accepted', $response[1]['Message']);

$this->assertEquals('Test Test <[email protected]>', $response[2]['To']);
$this->assertEquals('"Test Test" <[email protected]>', $response[2]['To']);
$this->assertEquals(0, $response[2]['ErrorCode']);
$this->assertEquals('Test job accepted', $response[2]['Message']);
}
Expand Down

0 comments on commit 95a84ae

Please sign in to comment.