From 0076a251879a433a1c5b56cae8ffe1671f4bccb3 Mon Sep 17 00:00:00 2001 From: Ben Routson Date: Thu, 16 Feb 2017 10:21:55 -0800 Subject: [PATCH] Add Sendgrid email notification example --- sendgrid_email_notification/README.md | 61 ++++++++++++++++++ sendgrid_email_notification/pantheon.yml | 28 ++++++++ sendgrid_email_notification/send_email.php | 74 ++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 sendgrid_email_notification/README.md create mode 100644 sendgrid_email_notification/pantheon.yml create mode 100644 sendgrid_email_notification/send_email.php diff --git a/sendgrid_email_notification/README.md b/sendgrid_email_notification/README.md new file mode 100644 index 0000000..5ac64dd --- /dev/null +++ b/sendgrid_email_notification/README.md @@ -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 +``` + diff --git a/sendgrid_email_notification/pantheon.yml b/sendgrid_email_notification/pantheon.yml new file mode 100644 index 0000000..690fbe1 --- /dev/null +++ b/sendgrid_email_notification/pantheon.yml @@ -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 \ No newline at end of file diff --git a/sendgrid_email_notification/send_email.php b/sendgrid_email_notification/send_email.php new file mode 100644 index 0000000..9815f12 --- /dev/null +++ b/sendgrid_email_notification/send_email.php @@ -0,0 +1,74 @@ + 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; +} \ No newline at end of file