Skip to content
This repository has been archived by the owner on Aug 18, 2019. It is now read-only.

Commit

Permalink
Add slack webhook option for contact form
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Carlevato committed Feb 20, 2017
1 parent 4894f88 commit 6d610bb
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 17 deletions.
7 changes: 6 additions & 1 deletion _tests/unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ public function testConfigFileStructure()
require('include/config.inc.php');

// verify we have the correct number of config array elements
$this->assertEquals(19, count($config), 'Incorrect number of settings in $config array.');
$this->assertEquals(23, count($config), 'Incorrect number of settings in $config array.');

// verify config array keys are named as expected
$this->assertTrue(array_key_exists('site_version', $config), '$config element version missing.');
$this->assertTrue(array_key_exists('site_domain', $config), '$config element site_domain missing.');
$this->assertTrue(array_key_exists('site_root', $config), '$config element site_root missing.');
$this->assertTrue(array_key_exists('compress', $config), '$config element compress missing.');
$this->assertTrue(array_key_exists('contact_method', $config), '$config element contact_method missing.');
$this->assertTrue(array_key_exists('send_to_address', $config), '$config element send_to_address missing.');

$this->assertTrue(array_key_exists('slack_channel', $config), '$config element slack_channel missing.');
$this->assertTrue(array_key_exists('slack_icon', $config), '$config element slack_icon missing.');
$this->assertTrue(array_key_exists('slack_url', $config), '$config element slack_url missing.');
$this->assertTrue(array_key_exists('optimizely_id', $config), '$config element optimizely_id missing.');
$this->assertTrue(array_key_exists('a_b_testing', $config), '$config element a_b_testing missing.');
$this->assertTrue(array_key_exists('google_analytics_id', $config), '$config element google_analytics_id missing.');
Expand Down
6 changes: 3 additions & 3 deletions ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
// package form data up form submission data
$form = package_form_submission($_POST['data']);

// send email with cleaned form data
send_contact_email($form, $response);
// send message with cleaned form data
send_contact_message($form, $response);

break;
}
}
Expand Down
13 changes: 8 additions & 5 deletions contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@

if ($config['recaptcha_active'] && $config['recaptcha_site_key'] && $config['recaptcha_secret_key']) {
$captcha_challenge = '<div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.$config['recaptcha_site_key'].'"></div>';
$captcha_scripts = '<script src="https://www.google.com/recaptcha/api.js"></script>';
}

$captcha_scripts = <<<HTML
<script src='https://www.google.com/recaptcha/api.js'></script>
$contact_javascript = <<<HTML
<script>
(function($){
/*
Expand All @@ -71,7 +72,10 @@
.done(function(data) {
// handle success response, reset form and recaptcha
$('#contact_christopherl').trigger('reset');
grecaptcha.reset();
if (typeof grecaptcha !== 'undefined') {
grecaptcha.reset();
}
// update feedback
$('#feedback').show();
Expand Down Expand Up @@ -100,11 +104,10 @@
}(jQuery));
</script>
HTML;
}

$smarty->assign('head_extras', '');
$smarty->assign('body_header_extras', '');
$smarty->assign('body_footer_extras', smarty_content($captcha_scripts));
$smarty->assign('body_footer_extras', smarty_content($captcha_scripts . $contact_javascript));

$footer_cta = newsletter_subscribe();

Expand Down
15 changes: 13 additions & 2 deletions include/config.inc.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,19 @@
'compress' => 0,


// Email address that should recieve contact form submissions
'send_to_address' => '',
// 0 = old timey email
// 1 = slack webhook
'contact_method' => '0',


// Email address that should recieve contact form submissions, can be left blank if using slack
'send_to_address' => 'chris@christopherl.com',


// slack webhook settings
'slack_channel' => '#',
'slack_icon' => ':memo:',
'slack_url' => '',


// Optimizely Project ID, if blank JavaScript test code will not be enabled.
Expand Down
37 changes: 31 additions & 6 deletions include/email.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @param array $form submitted form data ($key = form input id, $value = submitted value)
* @param $response reference to ajax.php $response array
*/
function send_contact_email($form=array(), &$response) {
function send_contact_message($form=array(), &$response) {
global $config;

if (!$config['send_to_address']) {
Expand Down Expand Up @@ -52,12 +52,37 @@ function send_contact_email($form=array(), &$response) {
else if (!$form['message']) {
set_response_error("How can we reply if you don't say anything?", 'message', $response);
}
// looks good, send that email
// looks good, send message
else {
$send_result = mail("{$config['send_to_address']}",
"Greetings from ChristopherL",
"{$form['message']}\n\n--------------\n{$form['fullname']}\n{$form['email']}",
"From: {$form['email']}");
if (intval($config['contact_method']) == 0) {
$send_result = mail("{$config['send_to_address']}",
"Greetings from ChristopherL",
"{$form['message']}\n\n--------------\n{$form['fullname']}\n{$form['email']}",
"From: {$form['email']}");
}
else {
$data = "payload=" . json_encode(array(
"channel" => "{$config['slack_channel']}",
"text" => sprintf("%s Contact Message:\n*Name:* %s\n*Email:* %s\n> %s",
$config['site_domain'],
$form['fullname'],
$form['email'],
$form['message']
),
"icon_emoji" => $config['slack_icon']
));

$ch = curl_init($config['slack_url']);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

if (curl_exec($ch) == 'ok') {
$send_result = true;
}

curl_close($ch);
}

// didn't work, have them try again
if (!$send_result) {
Expand Down

0 comments on commit 6d610bb

Please sign in to comment.