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

Add check so that getContents() always returns content with MimeType text/plain first in array of Content objects #626

Merged
merged 3 commits into from
Aug 6, 2018
Merged
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
17 changes: 17 additions & 0 deletions lib/mail/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -1093,11 +1093,28 @@ public function addContents($contents)

/**
* Retrieve the contents attached to a Mail object
*
* Will return array of Content Objects with text/plain MimeType first
* Array re-ordered before return where this is not already the case
*
* @return Content[]
*/
public function getContents()
{
if ($this->contents[0]->getType() !== 'text/plain'
&& count($this->contents) > 1
) {

foreach ($this->contents as $key => $value) {
if ($value->getType() == 'text/plain') {
$plain_content = $value;
unset($this->contents[$key]);
break;
}
}
array_unshift($this->contents, $plain_content);
}

return $this->contents;
}

Expand Down
39 changes: 39 additions & 0 deletions test/unit/MailGetContentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace SendGrid\Tests;

use PHPUnit\Framework\TestCase;
use SendGrid\Mail\Mail;
use SendGrid\Mail\Content;
use SendGrid\Mail\EmailAddress;
use SendGrid\Mail\From;

/**
* This class tests the getContents() function in SendGrid\Mail\Mail
*
* @package SendGrid\Tests
*/
class MailGetContentsTest extends TestCase
{

/**
* This method tests that array from Mail getContents() returns with
* text/plain Content object first when Mail instantiated with text/html
* content before text/plain
*
* @return null
*/
public function testWillReturnPlainContentFirst()
{
$from = new From(null, "test@example.com");
$subject = "Hello World from the SendGrid PHP Library";
$to = new EmailAddress("Test Person", "test@example.com");

$plain_content = new Content("text/plain", "some text here");
$html_content = new Content("text/html", "<p>some text here</p>");

$mail = new Mail($from, [$to], $subject, $html_content, $plain_content);

$this->assertEquals('text/plain', $mail->getContents()[0]->getType());
}
}