Skip to content
Open
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
61 changes: 61 additions & 0 deletions sendgrid_email_notification/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Sendgrid Email notification #

This script shows how easy it is to integrate Sendgrid emails from your Pantheon project using Quicksilver. As a bonus, we also show you how to manage API keys outside of your site repository.

## Instructions ##

1. Copy the secret Webhook URL into a file called `secrets.json` and store it in the [private files](https://pantheon.io/docs/articles/sites/private-files/) directory of every environment where you want to trigger Slack notifications.

```shell
$> echo '{"sg_username": "YOUR_SENDGRID)USERNAME","sg_password": "YOUR_SENDGRID_PASSWORD"}' > secrets.json
# Note, you'll need to copy the secrets into each environment where you want to trigger Sendgrid email notifications.
$> `terminus site connection-info --env=dev --site=your-site --field=sftp_command`
Connected to appserver.dev.d1ef01f8-364c-4b91-a8e4-f2a46f14237e.drush.in.
sftp> cd files
sftp> mkdir private
sftp> cd private
sftp> put secrets.json
sftp> quit
```

3. Add, and update as needed, the example `send_email.php` script to the `private` directory in the root of your site's codebase, that is under version control. Note this is a different `private` directory than where the secrets.json is stored.
4. Add Quicksilver operations to your `pantheon.yml`
5. Test a deploy out!

Optionally, you may want to use the `terminus workflows watch` command to get immediate debugging feedback. You may also want to customize your notifications further.

### Example `pantheon.yml` ###

Here's an example of what your `pantheon.yml` would look like if this were the only Quicksilver operation you wanted to use. Pick and choose the exact workflows that you would like to see notifications for.

```yaml
api_version: 1

workflows:
deploy_product:
after:
- type: webphp
description: Send email after site creation
script: private/scripts/send_email.php
create_cloud_development_environment:
after:
- type: webphp
description: Send email after Multidev creation
script: private/scripts/send_email.php
deploy:
after:
- type: webphp
description: Send email after deploy
script: private/scripts/send_email.php
sync_code:
after:
- type: webphp
description: Send email after code commit
script: private/scripts/send_email.php
clear_cache:
after:
- type: webphp
description: Someone is clearing the cache again
script: private/scripts/send_email.php
```

28 changes: 28 additions & 0 deletions sendgrid_email_notification/pantheon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
api_version: 1

workflows:
deploy_product:
after:
- type: webphp
description: Send email after site creation
script: private/scripts/send_email.php
create_cloud_development_environment:
after:
- type: webphp
description: Send email after Multidev creation
script: private/scripts/send_email.php
deploy:
after:
- type: webphp
description: Send email after deploy
script: private/scripts/send_email.php
sync_code:
after:
- type: webphp
description: Send email after code commit
script: private/scripts/send_email.php
clear_cache:
after:
- type: webphp
description: Someone is clearing the cache again
script: private/scripts/send_email.php
74 changes: 74 additions & 0 deletions sendgrid_email_notification/send_email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

$secrets = _get_secrets(array('sg_username', 'sg_password'), array());

$url = 'https://api.sendgrid.com/';
$user = $secrets['sg_username'];
$pass = $secrets['sg_password'];


$json_string = array(

'to' => array(
'youremail@yourdomain.com'
),
'category' => 'test_category'
);


$params = array(
'api_user' => $user,
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'to' => 'youremail@yourdomain.com',
'subject' => 'testing from curl',
'html' => 'testing body',
'text' => 'testing body',
'from' => 'youremail@yourdomain.com',
);


$request = $url.'api/mail.send.json';

// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// obtain response
$response = curl_exec($session);
curl_close($session);

// print everything out
print_r($response);

/**
* Get secrets from secrets file.
*
* @param array $requiredKeys List of keys in secrets file that must exist.
*/
function _get_secrets($requiredKeys, $defaults)
{
$secretsFile = $_SERVER['HOME'] . '/files/private/secrets.json';
if (!file_exists($secretsFile)) {
die('No secrets file found. Aborting!');
}
$secretsContents = file_get_contents($secretsFile);
$secrets = json_decode($secretsContents, 1);
if ($secrets == FALSE) {
die('Could not parse json in secrets file. Aborting!');
}
$secrets += $defaults;
$missing = array_diff($requiredKeys, array_keys($secrets));
if (!empty($missing)) {
die('Missing required keys in json secrets file: ' . implode(',', $missing) . '. Aborting!');
}
return $secrets;
}