Skip to content

Commit

Permalink
Add names support in the emails, fix sending plain text emails with a…
Browse files Browse the repository at this point in the history
…ttachement
  • Loading branch information
Ivan Kerin committed Sep 12, 2013
1 parent 6bc3746 commit 8f38454
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
28 changes: 20 additions & 8 deletions src/Openbuildings/Postmark/Swift/Transport/PostmarkTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ public function stop()
{
return FALSE;
}

public function convert_email_array(array $emails)
{
$converted = array();

foreach ($emails as $email => $name)
{
$converted []= $name ? "{$name} <{$email}>" : $email;
}

return $converted;
}

/**
* @param Swift_Mime_Message $message
Expand Down Expand Up @@ -78,34 +90,34 @@ public function send(\Swift_Mime_Message $message, & $failed_recipients = NULL)
}

$data = array(
'From' => join(',', array_keys($message->getFrom())),
'To' => join(',', array_keys($message->getTo())),
'From' => join(',', self::convert_email_array($message->getFrom())),
'To' => join(',', self::convert_email_array($message->getTo())),
'Subject' => $message->getSubject(),
);

if ($cc = $message->getCc())
{
$data['Cc'] = join(',', array_keys($cc));
$data['Cc'] = join(',', self::convert_email_array($cc));
}

if ($reply_to = $message->getReplyTo())
{
$data['ReplyTo'] = join(',', array_keys($reply_to));
$data['ReplyTo'] = join(',', self::convert_email_array($reply_to));
}

if ($bcc = $message->getBcc())
{
$data['Bcc'] = join(',', array_keys($bcc));
$data['Bcc'] = join(',', self::convert_email_array($bcc));
}

switch ($message->getContentType())
{
case 'text/plain':
$data['TextBody'] = $message->getBody();
break;
case 'text/html':
$data['HtmlBody'] = $message->getBody();
break;
default:
$data['TextBody'] = $message->getBody();
break;
}

if ($plain = $this->getMIMEPart($message, 'text/plain'))
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public function test_send()
$api = new Api('POSTMARK_API_TEST');

$response = $api->send(array(
'From' => '[email protected]',
'From' => 'Mark Smith <[email protected]>',
'To' => '[email protected],[email protected],[email protected]',
'Subject' => 'Test',
'HtmlBody' => '<b>Hello</b>',
'TextBody' => 'Hello',
'ReplyTo' => '[email protected]',
'Cc' => '[email protected],[email protected]',
'Cc' => '[email protected],Tom Smith <[email protected]>',
'Bcc' => '[email protected],[email protected]',
'Attachments' => array(
array(
Expand Down
46 changes: 36 additions & 10 deletions tests/tests/Swift/PostmarkTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public function test_send()
$api->expects($this->at(2))
->method('send')
->with($this->equalTo(array(
'From' => '[email protected]',
'To' => '[email protected]',
'Cc' => '[email protected],[email protected]',
'Bcc' => '[email protected],[email protected]',
'ReplyTo' => '[email protected]',
'From' => 'John Smith <[email protected]>',
'To' => 'Paul Smith <[email protected]>',
'Cc' => 'Jane Smith <[email protected]>,[email protected]',
'Bcc' => 'Gale Smith <[email protected]>,Mark Smith <[email protected]>',
'ReplyTo' => 'Tom Smith <[email protected]>',
'Subject' => 'Test Big',
'TextBody' => 'Text Part',
'HtmlBody' => 'HTML Part',
Expand All @@ -54,6 +54,22 @@ public function test_send()
)
)));

$api->expects($this->at(3))
->method('send')
->with($this->equalTo(array(
'From' => '[email protected]',
'To' => '[email protected]',
'Subject' => 'Test Big',
'TextBody' => 'Text Body',
'Attachments' => array(
array(
'Name' => 'logo_black.png',
'Content' => 'iVBORw0KGgoAAAANSUhEUgAAAHsAAAASCAYAAAB7PKHtAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAABR0RVh0Q3JlYXRpb24gVGltZQAyLzcvMTOGU1gaAAAAHHRFWHRTb2Z0d2FyZQBBZG9iZSBGaXJld29ya3MgQ1M26LyyjAAAA+ZJREFUaIHtmeF12zYQx3/W83drA6kTRJkg7ATWBqUniDJB6AmiTlB6gsoThNmA2YDeQJmg/XDAI3S8A0G7TZ22//fwLIL3B+6Aw90RvsJGDeyBHbAJfV+BATgBrZLfAcfkuTVkLOR4dWgeBqAP+gzq3TGM7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5KBJxBwwr1dkAPyGX6tZAT4ghO8r+X/1fRUn+jv+2vMvIfkU2dcs0ksQC06q+Ix6RCr0FKVZy2DLmgZ7Xe+Hyb0EV/p7x7wxy2CH52uT/Cao6KpIl3Ep0AAAAAElFTkSuQmCC',
'ContentType' => 'image/png'
),
)
)));

$message = Swift_Message::newInstance();

$message->setFrom('[email protected]');
Expand All @@ -74,18 +90,28 @@ public function test_send()

$message = Swift_Message::newInstance();

$message->setFrom('[email protected]');
$message->setTo('[email protected]');
$message->setReplyTo('[email protected]');
$message->setFrom('[email protected]', 'John Smith');
$message->setTo('[email protected]', 'Paul Smith');
$message->setReplyTo('[email protected]', 'Tom Smith');
$message->setSubject('Test Big');
$message->setCc(array('[email protected]', '[email protected]'));
$message->setBcc(array('[email protected]', '[email protected]'));
$message->setCc(array('[email protected]' => 'Jane Smith', '[email protected]'));
$message->setBcc(array('[email protected]' => 'Gale Smith', '[email protected]' => 'Mark Smith'));
$message->addPart('HTML Part', 'text/html');
$message->addPart('Text Part', 'text/plain');
$message->attach(Swift_Attachment::fromPath(__DIR__.'/../../test_data/logo_black.png'));

$mailer->send($message);

$message = Swift_Message::newInstance();

$message->setFrom('[email protected]');
$message->setTo('[email protected]');
$message->setSubject('Test Big');
$message->setBody('Text Body');
$message->attach(Swift_Attachment::fromPath(__DIR__.'/../../test_data/logo_black.png'));

$mailer->send($message);

$transport->stop();

$transport->registerPlugin($this->getMock('Swift_Events_EventListener'));
Expand Down

0 comments on commit 8f38454

Please sign in to comment.