-
Notifications
You must be signed in to change notification settings - Fork 628
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
Added Box attachment example (issue #481) #743
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ This documentation provides examples for specific use cases. Please [open an iss | |
# Table of Contents | ||
- [Table of Contents](#table-of-contents) | ||
- [Attachments](#attachments) | ||
- [Attaching a File from Box](#attaching-a-file-from-box) | ||
- [Kitchen Sink - an example with all settings used](#kitchen-sink) | ||
- [Send an Email to a Single Recipient](#send-an-email-to-a-single-recipient) | ||
- [Send an Email to Multiple Recipients](#send-an-email-to-multiple-recipients) | ||
|
@@ -58,6 +59,181 @@ try { | |
} | ||
``` | ||
|
||
<a name="box-attachment-example"></a> | ||
# Attaching a File from Box | ||
|
||
You can attach a file from [Box](https://www.box.com) to your emails. | ||
Because there is no official Box SDK for PHP, this example requires | ||
[firebase/php-jwt](https://github.com/firebase/php-jwt) to generate a | ||
[JSON Web Token](https://jwt.io) assertion. Before using this code, you should | ||
set up a JWT application on [Box](https://developer.box.com/docs/setting-up-a-jwt-app). | ||
For more information about authenticating with JWT, see | ||
[this page](https://developer.box.com/docs/construct-jwt-claim-manually). | ||
|
||
After completing the setup tutorial, you will want to make sure your app’s | ||
configuration settings have at least the following options enabled: | ||
|
||
**Application Access** | ||
*Enterprise | ||
|
||
**Application Scopes** | ||
*Read all files and folders stored in Box | ||
*Read and write all files and folders stored in Box | ||
*Manage users | ||
|
||
**Advanced Features** | ||
*Perform Actions as Users | ||
|
||
Remember to reauthorize your app | ||
[here](https://app.box.com/master/settings/openbox) after making any changes to | ||
your app’s JWT scopes. | ||
|
||
```php | ||
<?php | ||
|
||
// This example assumes you're using Composer for both | ||
// the sendgrid-php library and php-jwt. | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use \Firebase\JWT\JWT; | ||
|
||
$fileOwner = '[email protected]'; // Replace with the email you use to sign in to Box | ||
$filePath = '/path/to/file.txt'; // Replace with the path on Box to the file you will attach | ||
$boxConfig = json_decode(file_get_contents('/path/to/boxConfig.json')); // Replace with the path to your Box config file. Keep it in a secure location! | ||
|
||
$path = explode('/', $filePath); | ||
if (!empty($path[0])){ | ||
array_unshift($path, ''); // Adds a blank element to beginning of array in case $filePath does not have a preceding forward slash. | ||
} | ||
|
||
$header = array( | ||
'alg' => 'RS256', | ||
'typ' => 'JWT', | ||
'kid' => $boxConfig->boxAppSettings->appAuth->publicKeyID | ||
); | ||
$claims = array( | ||
'iss' => $boxConfig->boxAppSettings->clientID, | ||
'sub' => $boxConfig->enterpriseID, | ||
'box_sub_type' => 'enterprise', | ||
'aud' => 'https://api.box.com/oauth2/token', | ||
'jti' => bin2hex(random_bytes(16)), | ||
'exp' => time() + 50 | ||
); | ||
|
||
$privateKey = openssl_get_privatekey($boxConfig->boxAppSettings->appAuth->privateKey, $boxConfig->boxAppSettings->appAuth->passphrase); | ||
|
||
$assertion = JWT::encode($claims, $privateKey, 'RS256', null, $header); | ||
|
||
// Get access token | ||
$url = 'https://api.box.com/oauth2/token'; | ||
$data = array( | ||
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', | ||
'client_id' => $boxConfig->boxAppSettings->clientID, | ||
'client_secret' => $boxConfig->boxAppSettings->clientSecret, | ||
'assertion' => $assertion | ||
); | ||
$ch = curl_init($url); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); | ||
$result = json_decode(curl_exec($ch)); | ||
curl_close($ch); | ||
$accessToken = $result->access_token; | ||
|
||
// Get user ID | ||
$url = 'https://api.box.com/2.0/users'; | ||
$ch = curl_init($url); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | ||
'Authorization: Bearer '.$accessToken | ||
)); | ||
$result = json_decode(curl_exec($ch)); | ||
curl_close($ch); | ||
foreach ($result->entries as $entry){ | ||
if ($entry->login === $fileOwner){ | ||
$userId = $entry->id; | ||
} | ||
} | ||
|
||
// Get file ID | ||
$url = 'https://api.box.com/2.0/search'; | ||
$data = array( | ||
'query' => urlencode(end($path)) | ||
); | ||
$urlEncoded = http_build_query($data); | ||
$ch = curl_init($url.'?'.$urlEncoded); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | ||
'Authorization: Bearer '.$accessToken, | ||
'As-User: '.$userId | ||
)); | ||
$result = json_decode(curl_exec($ch)); | ||
curl_close($ch); | ||
foreach ($result->entries as $entry){ | ||
if (count($entry->path_collection->entries) === count($path) -1){ | ||
if (count($path) > 2){ | ||
// File is located in a subdirectory. | ||
for ($i = 1; $i < (count($path) - 1); $i++){ | ||
if ($path[$i] === $entry->path_collection->entries[$i]->name){ | ||
$fileId = $entry->id; | ||
} | ||
} | ||
} else { | ||
// File is located in default directory. | ||
$fileId = $entry->id; | ||
} | ||
} | ||
} | ||
|
||
if (isset($fileId) && isset($userId)){ | ||
// Get file data | ||
$url = 'https://api.box.com/2.0/files/'.$fileId.'/content'; | ||
$ch = curl_init($url); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | ||
'Authorization: Bearer '.$accessToken, | ||
'As-User: '.$userId | ||
)); | ||
$result = curl_exec($ch); | ||
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); | ||
curl_close($ch); | ||
|
||
$attachmentFilename = end($path); | ||
$attachmentContent = base64_encode($result); | ||
$attachmentContentType = $contentType; | ||
|
||
$email = new \SendGrid\Mail\Mail(); | ||
$email->setFrom("[email protected]", "Example User"); | ||
$email->setSubject("Attaching a File from Box"); | ||
$email->addTo("[email protected]", "Example User"); | ||
$email->addContent("text/plain", "See attached file from Box."); | ||
$email->addContent( | ||
"text/html", "<strong>See attached file from Box.</strong>" | ||
); | ||
|
||
$attachment = new \SendGrid\Mail\Attachment(); | ||
$attachment->setContent($attachmentContent); | ||
$attachment->setType($attachmentContentType); | ||
$attachment->setFilename($attachmentFilename); | ||
$attachment->setDisposition("attachment"); | ||
$attachment->setContentId($attachmentFilename); // Only used if disposition is set to inline | ||
$email->addAttachment($attachment); | ||
|
||
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); | ||
try { | ||
$response = $sendgrid->send($email); | ||
print $response->statusCode() . "\n"; | ||
print_r($response->headers()); | ||
print $response->body() . "\n"; | ||
} catch (Exception $e) { | ||
echo 'Caught exception: '. $e->getMessage() ."\n"; | ||
} | ||
} else { | ||
echo "Error: file or owner could not be located\n"; | ||
} | ||
``` | ||
|
||
<a name="kitchen-sink"></a> | ||
# Kitchen Sink - an example with all settings used | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to official doc, the
random_bytes
function supports sincephp-7.0
version.And this package still supports the
php-5.6
version.I think this example code should add the notification message to notify someone this sample is only available for
php-7.0+
or using other ways to be compatible withphp-5.6
.