Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
MxFlorin authored Jun 6, 2024
2 parents 8ff944f + 6700d2c commit 7297775
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ jobs:
steps:
- uses: amannn/action-semantic-pull-request@v4
with:
types: chore docs fix feat test
types: chore docs fix feat test misc
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,37 @@ All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](http://semver.org/).

[2024-04-18] Version 8.1.2
--------------------------
**Library - Chore**
- [PR #1107](https://github.com/sendgrid/sendgrid-php/pull/1107): updating php-http-client version to dynamic. Thanks to [@tiwarishubham635](https://github.com/tiwarishubham635)!


[2023-12-06] Version 8.1.1
--------------------------
**Library - Chore**
- [PR #1105](https://github.com/sendgrid/sendgrid-php/pull/1105): updated php-http-client version to enable setHost. Thanks to [@tiwarishubham635](https://github.com/tiwarishubham635)!


[2023-12-01] Version 8.1.0
--------------------------
**Library - Feature**
- [PR #1104](https://github.com/sendgrid/sendgrid-php/pull/1104): Added Data residency for eu and global region. Thanks to [@tiwarishubham635](https://github.com/tiwarishubham635)!


[2022-08-10] Version 8.0.1
--------------------------
**Library - Docs**
- [PR #1097](https://github.com/sendgrid/sendgrid-php/pull/1097): rewrite function description. Thanks to [@mohamed-foly](https://github.com/mohamed-foly)!
- [PR #1095](https://github.com/sendgrid/sendgrid-php/pull/1095): Modify README in alignment with SendGrid Support. Thanks to [@garethpaul](https://github.com/garethpaul)!

**Library - Fix**
- [PR #1081](https://github.com/sendgrid/sendgrid-php/pull/1081): get rid of deprecation warnings regarding the first parameter of mb_convert_encoding. Thanks to [@cclark61](https://github.com/cclark61)!

**Library - Test**
- [PR #1099](https://github.com/sendgrid/sendgrid-php/pull/1099): Adding misc as PR type. Thanks to [@rakatyal](https://github.com/rakatyal)!


[2022-05-04] Version 8.0.0
--------------------------
**Note:** This release contains breaking changes, check our [upgrade guide](./UPGRADE.md#2022-05-04-7xx-to-8xx) for detailed migration notes.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (C) 2022, Twilio SendGrid, Inc. <[email protected]>
Copyright (C) 2023, Twilio SendGrid, Inc. <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ci-install: clean
test:
vendor/bin/phpunit test/unit --filter test*
vendor/bin/phpcs lib/*/*
vendor/bin/phpstan analyse --no-progress --level 1 lib test
vendor/bin/phpstan analyse --memory-limit 1G --no-progress --level 1 lib test

test-integ: test
vendor/bin/phpunit test --filter test*
Expand Down
2 changes: 1 addition & 1 deletion PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ We appreciate the effort for this pull request but before that please make sure
Please format the PR title appropriately based on the type of change:
<type>[!]: <description>
Where <type> is one of: docs, chore, feat, fix, test.
Where <type> is one of: docs, chore, feat, fix, test, misc.
Add a '!' after the type for breaking changes (e.g. feat!: new breaking feature).
**All third-party contributors acknowledge that any contributions they provide will be made under the same open-source license that the open-source project is provided under.**
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ Add Twilio SendGrid to your `composer.json` file. If you are not using [Composer

### Alternative: Install package from zip

If you are not using Composer, simply download and install the **[latest packaged release of the library as a zip](https://github.com/sendgrid/sendgrid-php/releases/download/8.0.0/sendgrid-php.zip)**.
If you are not using Composer, simply download and install the **[latest packaged release of the library as a zip](https://github.com/sendgrid/sendgrid-php/releases/download/8.1.2/sendgrid-php.zip)**.

[**⬇︎ Download Packaged Library ⬇︎**](https://github.com/sendgrid/sendgrid-php/releases/download/8.0.0/sendgrid-php.zip)
[**⬇︎ Download Packaged Library ⬇︎**](https://github.com/sendgrid/sendgrid-php/releases/download/8.1.2/sendgrid-php.zip)

Previous versions of the library can be downloaded directly from [GitHub](https://github.com/sendgrid/sendgrid-php/releases).

Expand Down
18 changes: 18 additions & 0 deletions USE_CASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This documentation provides examples for specific use cases. Please [open an iss
- [Send an Email to Multiple Recipients](#send-an-email-to-multiple-recipients)
- [Send Multiple Emails to Multiple Recipients](#send-multiple-emails-to-multiple-recipients)
- [Send Multiple Emails with Personalizations](#multiple-email-personalizations)
- [Set Region](#set-region)
- [Transactional Templates](#transactional-templates)
- [Legacy Templates](#legacy-templates)
- [Send an Email With Twilio Email (Pilot)](#send-an-email-with-twilio-email-pilot)
Expand Down Expand Up @@ -1172,6 +1173,23 @@ try {
}
```

<a name="set-region"></a>
# Set Region
The SendGrid object can also be used to set the region to "eu", which will send the request to https://api.eu.sendgrid.com/. By default, it is set to https://api.sendgrid.com/, e.g.

```php
<?php
// Uncomment next line if you're not using a dependency loader (such as Composer)
// require_once '<PATH TO>/sendgrid-php.php';

$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$sendgrid->setDataResidency("eu");

OR

$sendgrid->setDataResidency("global");
```

<a name="transactional-templates"></a>
# Transactional Templates

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"require": {
"php": ">=7.3",
"sendgrid/php-http-client": "~3.10",
"sendgrid/php-http-client": "~4.1",
"starkbank/ecdsa": "0.*",
"ext-curl": "*",
"ext-json": "*",
Expand Down
75 changes: 75 additions & 0 deletions examples/dataresidency/setregion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
// index.php
require_once __DIR__ . '/../../sendgrid-php.php';

use SendGrid\Mail\Mail;
use SendGrid\Mail\Personalization;
use SendGrid\Mail\To;
$exceptionMessage = 'Caught exception: ';

////////////////////////////////////////////////////
// Set data residency to navigate to a region/edge. #
// sending to global data residency

$email = buildHelloEmail();
$sendgrid = buildSendgridObject("global");

try {
$response = $sendgrid->client->mail()->send()->post($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo $exceptionMessage, $e->getMessage(), "\n";
}

////////////////////////////////////////////////////
// sending to EU data residency

$sendgrid_eu = buildSendgridObject("eu");

try {
$response = $sendgrid_eu->client->mail()->send()->post($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo $exceptionMessage, $e->getMessage(), "\n";
}

////////////////////////////////////////////////////
// not configuring any region defaults to global
$sendgrid_default = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
$response = $sendgrid_default->client->mail()->send()->post($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo $exceptionMessage, $e->getMessage(), "\n";
}

function buildHelloEmail(): Mail
{
$email = new Mail();
$email->setFrom("[email protected]", "test");
$email->setSubject("Sending with Twilio SendGrid is Fun");
$email->addTo("[email protected]", "test");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$objPersonalization = new Personalization();

$objTo = new To('[email protected]', 'foo bar');
$objPersonalization->addTo($objTo);
$email->addPersonalization($objPersonalization);
return $email;
}

function buildSendgridObject($region): SendGrid
{
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$sendgrid->setDataResidency($region);
return $sendgrid;
}
27 changes: 26 additions & 1 deletion lib/BaseSendGridClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
abstract class BaseSendGridClientInterface
{
/** @var string SendGrid API library version */
const VERSION = '8.0.0';
const VERSION = '8.1.2';

/** @var Client SendGrid HTTP Client library */
public $client;

/** @var string SendGrid version */
public $version = self::VERSION;

/** @var allowedRegionsHostMap regions specific hosts */
public $allowedRegionsHostMap = [
"eu" => "https://api.eu.sendgrid.com",
"global" => "https://api.sendgrid.com",
];

/**
* Set up the HTTP Client.
*
Expand Down Expand Up @@ -62,4 +68,23 @@ public function send(Mail $email)
{
return $this->client->mail()->send()->post($email);
}

/*
* Client libraries contain setters for specifying region/edge.
* This allows support global and eu regions only. This set will likely expand in the future.
* Global should be the default
* Global region means the message should be sent through:
* HTTP: api.sendgrid.com
* EU region means the message should be sent through:
* HTTP: api.eu.sendgrid.com
*/
public function setDataResidency($region): void
{
if (array_key_exists($region, $this->allowedRegionsHostMap)) {
$this->client->setHost($this->allowedRegionsHostMap[$region]);
} else {
throw new InvalidArgumentException("region can only be \"eu\" or \"global\"");
}
}

}
2 changes: 1 addition & 1 deletion lib/mail/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function setValue($value)
{
Assert::minLength($value, 'value', 1);

$this->value = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
$this->value = mb_convert_encoding((string)$value, 'UTF-8', 'UTF-8');
}

/**
Expand Down
3 changes: 1 addition & 2 deletions lib/mail/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -1810,8 +1810,7 @@ public function enableSandBoxMode()
/**
* Disable sandbox mode on a MailSettings object
*
* This allows you to send a test email to ensure that your request
* body is valid and formatted correctly.
* This to ensure that your request is not in sandbox mode.
*
* @throws TypeException
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/mail/Subject.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function setSubject($subject)
*/
public function getSubject()
{
return mb_convert_encoding($this->subject, 'UTF-8', 'UTF-8');
return mb_convert_encoding((string)$this->subject, 'UTF-8', 'UTF-8');
}

/**
Expand Down
72 changes: 72 additions & 0 deletions test/unit/DataResidencyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* This file tests email address encoding.
*/

namespace SendGrid\Tests\Unit;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;


/**
* This class tests Data residency
*
* @package SendGrid\Tests
*/
class DataResidencyTest extends TestCase
{
public function testSetResidencyEu()
{
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$sendgrid->setDataResidency("eu");
self::assertEquals("https://api.eu.sendgrid.com", $sendgrid->client->getHost());
}

public function testSetResidencyGlobal()
{
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$sendgrid->setDataResidency("global");
self::assertEquals("https://api.sendgrid.com", $sendgrid->client->getHost());
}

public function testSetResidencyOverrideHost()
{
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$sendgrid->client->setHost("https://test.api.com");
$sendgrid->setDataResidency("eu");
self::assertEquals("https://api.eu.sendgrid.com", $sendgrid->client->getHost());
}

public function testSetResidencyOverrideDataResidency()
{
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$sendgrid->setDataResidency("eu");
$sendgrid->client->setHost("https://test.api.com");
self::assertEquals("https://test.api.com", $sendgrid->client->getHost());
}

public function testSetResidencyIncorrectRegion()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("region can only be \"eu\" or \"global\"");

$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$sendgrid->setDataResidency("foo");
}

public function testSetResidencyNullRegion()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("region can only be \"eu\" or \"global\"");

$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$sendgrid->setDataResidency("");
}

public function testSetResidencyDefaultRegion()
{
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
self::assertEquals("https://api.sendgrid.com", $sendgrid->client->getHost());
}
}

0 comments on commit 7297775

Please sign in to comment.