Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent empty text part when only html part is assigned #19

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('[email protected]')->
setFromName('John Doe')->
setSubject('foobar subject')->
setHtml('foobar html')->
addTo('[email protected]');

$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('[email protected]')->
setFromName('John Doe')->
setSubject('foobar subject')->
setText('foobar text')->
addTo('[email protected]');

$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('[email protected]')->
setSubject('foobar subject')->
setText('foobar text')->
setHtml('foobar html')->
addTo('[email protected]')->
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' => '[email protected]',
'to' => '[email protected]',
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