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

Fixing issue #686 #705

Merged
merged 4 commits into from
May 21, 2019
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
40 changes: 25 additions & 15 deletions lib/mail/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ public function __construct(
$this->addPersonalization($personalization);
}
}
if (!$subs = $email->getSubstitutions()) {
$this->addPersonalization($personalization);
if (isset($email)) {
if (!$subs = $email->getSubstitutions()) {
$this->addPersonalization($personalization);
}
}
}
if (isset($subject)) {
Expand Down Expand Up @@ -664,7 +666,7 @@ public function getHeaders($personalizationIndex = 0)
* objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*/
*/
public function addDynamicTemplateData(
$key,
$value = null,
Expand All @@ -683,7 +685,7 @@ public function addDynamicTemplateData(
* objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*/
*/
public function addDynamicTemplateDatas(
$datas,
$personalizationIndex = null,
Expand All @@ -694,13 +696,13 @@ public function addDynamicTemplateDatas(

/**
* Retrieve dynamic template data key/value pairs from a Personalization object
*
*
* @param int|0 $personalizationIndex Index into an array of
* existing Personalization
* objects
*
*
* @return array
*/
*/
public function getDynamicTemplateDatas($personalizationIndex = 0)
{
return $this->getSubstitutions($personalizationIndex);
Expand Down Expand Up @@ -815,7 +817,7 @@ public function getSubstitutions($personalizationIndex = 0)
/**
* Add a custom arg to a Personalization or Mail object
*
* Note that custom args added to Personalization objects
* Note that custom args added to Personalization objects
* override global custom args.
*
* @param string|CustomArg $key Key or CustomArg object
Expand Down Expand Up @@ -987,7 +989,7 @@ public function getSendAt($personalizationIndex = 0)
* @param string|null $name Sender name
*
* @throws TypeException
*/
*/
public function setFrom($email, $name = null)
{
if ($email instanceof From) {
Expand Down Expand Up @@ -1114,7 +1116,7 @@ public function addContents($contents)
*
* 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()
Expand All @@ -1130,7 +1132,9 @@ public function getContents()
break;
}
}
array_unshift($this->contents, $plain_content);
if (isset($plain_content)) {
array_unshift($this->contents, $plain_content);
}
}
}

Expand Down Expand Up @@ -1174,7 +1178,7 @@ public function addAttachment(
$disposition,
$content_id
);
}
}
$this->attachments[] = $attachment;
}

Expand Down Expand Up @@ -1580,7 +1584,7 @@ public function getIpPoolName()
* use to specify how you would
* like this email to be handled
* @throws TypeException
*/
*/
public function setMailSettings($mail_settings)
{
if (!($mail_settings instanceof MailSettings)) {
Expand Down Expand Up @@ -1721,7 +1725,7 @@ public function setSpamCheck($enable = null, $threshold = null, $post_to_url = n
* of how your recipients interact
* with your email
* @throws TypeException
*/
*/
public function setTrackingSettings($tracking_settings)
{
if (!($tracking_settings instanceof TrackingSettings)) {
Expand Down Expand Up @@ -1882,9 +1886,15 @@ public function jsonSerialize()
}
}
}

return array_filter(
[
'personalizations' => $this->getPersonalizations(),
'personalizations' => array_values(array_filter(
$this->getPersonalizations(),
function ($value) {
return null !== $value && null !== $value->jsonSerialize();
}
)),
'from' => $this->getFrom(),
'reply_to' => $this->getReplyTo(),
'subject' => $this->getGlobalSubject(),
Expand Down
68 changes: 64 additions & 4 deletions test/unit/MailHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,75 @@ public function testEmailName()
'{"name":"\\"O\'Keeffe, John \\\\\\"Billy\\\\\\"\\"","email":"test@example.com"}'
);
}

/**
* This method tests TypeException for wrong email address
*
* @expectedException \SendGrid\Mail\TypeException
*/
public function testEmailAddress()
{
$email = new EmailAddress();
$email->setEmailAddress('test@example.com@wrong');
}
$email = new EmailAddress();
$email->setEmailAddress('test@example.com@wrong');
}

public function testJsonSerializeOverPersonalizationsShouldNotReturnNull()
{
$objEmail = new \SendGrid\Mail\Mail();

$objFrom = new \SendGrid\Mail\From('my@self.com', 'my self');
$objEmail->setFrom($objFrom);

$objSubject = new \SendGrid\Mail\Subject("test subject");
$objEmail->setSubject($objSubject);

$objContent = new \SendGrid\Mail\Content("text/html", "test content");
$objEmail->addContent($objContent);


$objPersonalization = new \SendGrid\Mail\Personalization();

$objTo = new \SendGrid\Mail\To('foo@bar.com', 'foo bar');
$objPersonalization->addTo($objTo);

$objPersonalization->addSubstitution("{{firstname}}", 'foo');

$objPersonalization->addSubstitution("{{lastname}}", 'bar');

$objEmail->addPersonalization($objPersonalization);

$json = json_encode($objEmail, JSON_PRETTY_PRINT);

$expectedJson = <<<JSON
{
"personalizations": [
{
"to": [
{
"name": "foo bar",
"email": "foo@bar.com"
}
],
"substitutions": {
"{{firstname}}": "foo",
"{{lastname}}": "bar"
}
}
],
"from": {
"name": "my self",
"email": "my@self.com"
},
"subject": "test subject",
"content": [
{
"type": "text\/html",
"value": "test content"
}
]
}
JSON;

$this->assertEquals($expectedJson, $json);
}
}