Skip to content

Commit

Permalink
Prevent empty text part when only html part is assigned
Browse files Browse the repository at this point in the history
Added tests and fixed same bug in WebAPI

Closes #19
Closes #17
Closes #16
  • Loading branch information
tenitski authored and theycallmeswift committed Apr 2, 2013
1 parent fa0c2c2 commit f978cad
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
12 changes: 10 additions & 2 deletions SendGrid/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,19 @@ protected function _mapToSwift(Mail $mail)
*/
$message->setTo($mail->getFrom());
$message->setFrom($mail->getFrom(true));
$message->setBody($mail->getHtml(), 'text/html');
$message->addPart($mail->getText(), 'text/plain');
$message->setCc($mail->getCcs());
$message->setBcc($mail->getBccs());

if ($mail->getHtml())
{
$message->setBody($mail->getHtml(), 'text/html');
if ($mail->getText()) $message->addPart($mail->getText(), 'text/plain');
}
else
{
$message->setBody($mail->getText(), 'text/plain');
}

if(($replyto = $mail->getReplyTo())) {
$message->setReplyTo($replyto);
}
Expand Down
10 changes: 8 additions & 2 deletions SendGrid/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ protected function _prepMessageData(Mail $mail)
'api_user' => $this->username,
'api_key' => $this->password,
'subject' => $mail->getSubject(),
'html' => $mail->getHtml(),
'text' => $mail->getText(),
'from' => $mail->getFrom(),
'to' => $mail->getFrom(),
'x-smtpapi' => $mail->getHeadersJson()
);

if($mail->getHtml()) {
$params['html'] = $mail->getHtml();
}

if($mail->getText()) {
$params['text'] = $mail->getText();
}

if(($fromname = $mail->getFromName())) {
$params['fromname'] = $fromname;
}
Expand Down
45 changes: 44 additions & 1 deletion Test/SendGrid/SmtpTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php

class SmtpTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -45,4 +45,47 @@ public function testPorts()
$mock->setPort('52');
$this->assertEquals('52', $mock->getPort());
}

public function testEmailBodyAttachments()
{
$_mapToSwift = new ReflectionMethod('SendGrid\Smtp', '_mapToSwift');
$_mapToSwift->setAccessible(true);

$sendgrid = new SendGrid("foo", "bar");
$message = new SendGrid\Mail();
$message->
setFrom('bar@foo.com')->
setFromName('John Doe')->
setSubject('foobar subject')->
setHtml('foobar html')->
addTo('foo@bar.com');

$swift_message = $_mapToSwift->invoke($sendgrid->smtp, $message);
$this->assertEquals(count($swift_message->getChildren()), 0);

$message->setText('foobar text');

$swift_message = $_mapToSwift->invoke($sendgrid->smtp, $message);
$this->assertEquals(count($swift_message->getChildren()), 1);
$body_attachments = $swift_message->getChildren();
$this->assertEquals($body_attachments[0]->getContentType(), 'text/plain');
}

public function testEmailTextBodyAttachments()
{
$_mapToSwift = new ReflectionMethod('SendGrid\Smtp', '_mapToSwift');
$_mapToSwift->setAccessible(true);

$sendgrid = new SendGrid("foo", "bar");
$message = new SendGrid\Mail();
$message->
setFrom('bar@foo.com')->
setFromName('John Doe')->
setSubject('foobar subject')->
setText('foobar text')->
addTo('foo@bar.com');

$swift_message = $_mapToSwift->invoke($sendgrid->smtp, $message);
$this->assertEquals(count($swift_message->getChildren()), 0);
}
}
5 changes: 4 additions & 1 deletion Test/SendGrid/WebTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function testMockFunctions()
setFrom('bar@foo.com')->
setSubject('foobar subject')->
setText('foobar text')->
setHtml('foobar html')->
addTo('foo@bar.com')->
addAttachment("mynewattachment.jpg");

Expand All @@ -31,7 +32,7 @@ public function testMockFunctions()
'api_user' => 'foo',
'api_key' => 'bar',
'subject' => 'foobar subject',
'html' => null,
'html' => 'foobar html',
'text' => 'foobar text',
'from' => 'bar@foo.com',
'to' => 'bar@foo.com',
Expand Down Expand Up @@ -63,6 +64,8 @@ public function testOptionalParamters()
// Default Values
$actual_without_optional_params = $mock->testPrepMessageData($message);

$this->assertArrayNotHasKey('html', $actual_without_optional_params);
$this->assertArrayNotHasKey('text', $actual_without_optional_params);
$this->assertArrayNotHasKey('fromname', $actual_without_optional_params);
$this->assertArrayNotHasKey('replyto', $actual_without_optional_params);

Expand Down

0 comments on commit f978cad

Please sign in to comment.