Skip to content

Instantly share code, notes, and snippets.

@planeth44
Last active July 1, 2016 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save planeth44/8adac26e879ce3fc3b98558342a741b9 to your computer and use it in GitHub Desktop.
Save planeth44/8adac26e879ce3fc3b98558342a741b9 to your computer and use it in GitHub Desktop.
zem_contact_reborn plugin. Send goodies to your users.
<?php
// This is a PLUGIN TEMPLATE for Textpattern CMS.
// Copy this file to a new name like abc_myplugin.php. Edit the code, then
// run this file at the command line to produce a plugin for distribution:
// $ php abc_myplugin.php > abc_myplugin-0.1.txt
// Plugin name is optional. If unset, it will be extracted from the current
// file name. Plugin names should start with a three letter prefix which is
// unique and reserved for each plugin author ("abc" is just an example).
// Uncomment and edit this line to override:
$plugin['name'] = 'zcr_sendGoodies';
// Allow raw HTML help, as opposed to Textile.
// 0 = Plugin help is in Textile format, no raw HTML allowed (default).
// 1 = Plugin help is in raw HTML. Not recommended.
# $plugin['allow_html_help'] = 1;
$plugin['version'] = '0.10';
$plugin['author'] = 'Aleth Gueguen';
$plugin['author_uri'] = 'http://smarttleads.com';
$plugin['description'] = 'Textpattern CMS sending module for zem_contact_reborn';
// Plugin load order:
// The default value of 5 would fit most plugins, while for instance comment
// spam evaluators or URL redirectors would probably want to run earlier
// (1...4) to prepare the environment for everything else that follows.
// Values 6...9 should be considered for plugins which would work late.
// This order is user-overrideable.
$plugin['order'] = '5';
// Plugin 'type' defines where the plugin is loaded
// 0 = public : only on the public side of the website (default)
// 1 = public+admin : on both the public and admin side
// 2 = library : only when include_plugin() or require_plugin() is called
// 3 = admin : only on the admin side (no AJAX)
// 4 = admin+ajax : only on the admin side (AJAX supported)
// 5 = public+admin+ajax : on both the public and admin side (AJAX supported)
$plugin['type'] = '0';
// Plugin "flags" signal the presence of optional capabilities to the core plugin loader.
// Use an appropriately OR-ed combination of these flags.
// The four high-order bits 0xf000 are available for this plugin's private use
if (!defined('PLUGIN_HAS_PREFS')) define('PLUGIN_HAS_PREFS', 0x0001); // This plugin wants to receive "plugin_prefs.{$plugin['name']}" events
if (!defined('PLUGIN_LIFECYCLE_NOTIFY')) define('PLUGIN_LIFECYCLE_NOTIFY', 0x0002); // This plugin wants to receive "plugin_lifecycle.{$plugin['name']}" events
$plugin['flags'] = '0';
// Plugin 'textpack' is optional. It provides i18n strings to be used in conjunction with gTxt().
// Syntax:
// ## arbitrary comment
// #@event
// #@language ISO-LANGUAGE-CODE
// abc_string_name => Localized String
/** Uncomment me, if you need a textpack
$plugin['textpack'] = <<< EOT
#@admin
#@language en-gb
abc_sample_string => Sample String
abc_one_more => One more
#@language de-de
abc_sample_string => Beispieltext
abc_one_more => Noch einer
EOT;
**/
// End of textpack
if (!defined('txpinterface'))
@include_once('zem_tpl.php');
# --- BEGIN PLUGIN CODE ---
// template to create email text
if (!defined('SMRTT_LEADMAGNET_EMAILTXT')) {
define("SMRTT_LEADMAGNET_EMAILTXT", 'leadMagnet_email_text');
}
if (txpinterface === 'public') {
register_callback('smrtt_sendGoodies', 'zemcontact.deliver');
}
/**
* Callback hook for zem_contact_reborn to handle delivery.
*/
function smrtt_sendGoodies($evt, $stp, &$payload)
{
// If using copysender, it's the 2nd time the plugin has been called so no need
// to do anything. ZCR will continue as normal and mail out the email.
if ($stp === 'copysender') {
return '';
}
$zcrConfig = array('return_action' => 'skip');
foreach ($payload['fields'] as $key => $value) {
if (strpos($key, 'zcr_') === 0) {
$param = substr($key, 4); // Strip off zcr_ prefix
$zcrConfig[$param] = $value;
}
}
// this is a zcr_contact_secret
if ($zcrConfig['smrtt_action'] !== 'sendGoodies') {
// Not for us.
return '';
}
$payload['headers']['from'] = filter_var($payload ['to'],FILTER_SANITIZE_EMAIL);
$payload['to'] = filter_var($payload['fields']['Email'], FILTER_SANITIZE_EMAIL);
// Set as many fields from you zcr form as needed
$payload['fields']['Prénom'] = filter_var($payload['fields']['Prénom'], FILTER_SANITIZE_STRING);
$payload['fields']['Nom'] = filter_var($payload['fields']['Nom'], FILTER_SANITIZE_STRING);
$payload['body'] = output_form(array('form' => SMRTT_LEADMAGNET_EMAILTXT), $payload['fields']['Prénom']);
// var_dump($payload); die;
if (!smrtt_smtp_sender($payload)) {
sleep(1);
return 'zemcontact.fail';
}
else {
smrtt_storeData($payload);
return 'zemcontact.skip';
}
if ($zcrConfig['return_action']) {
return 'zemcontact.' . $zcrConfig['return_action'];
}
}
// a small helper function to store the collected contact
function smrtt_storeData($payload) {
$res = safe_insert('contact_list',
"email='" . doSlash($payload['fields']['Email'])
. "', first_name='" . doSlash($payload['fields']['Prénom'])
. "', last_name='" . doSlash($payload['fields']['Nom'])
. "', company='" . doSlash($payload['fields']['Société']) ."'"
);
}
/**
* Send emails over SMTP.
*
* This function replaces the default mail handler. Basic example
* that uses PHPMailer for sending emails over Gmail SMTP server.
*
* Expects that PHPMailer is installed to 'phpmailer' directory located
* next to '/textpattern' directory, the same directory where Textpattern
* was installed to.
*
* @param array $payload The raw arguments to construct the mail
* @return bool TRUE on success, or FALSE on failure
*/
function smrtt_smtp_sender($payload)
{
require_once dirname(txpath) . '/vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
global $txpcfg;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = $txpcfg['smtp_encryption'];
$mail->Host = $txpcfg['smtp_host'];
$mail->Port = $txpcfg['smtp_port'];
$mail->Username = $txpcfg['smtp_username'];
$mail->Password = $txpcfg['smtp_password'];
$mail->XMailer = " ";
$mail->CharSet = "utf-8";
$mail->Encoding = "quoted-printable";
// $mail->SMTPDebug = 2;
$mail->isHTML(false);
$mail->SetFrom($payload['headers']['from'], $payload['fields']['smrtt_fromName']);
$mail->AddAddress($payload['to']);
/*
You can also add reply to
if ($reply_address)
{
$mail->AddReplyTo($reply_address, $reply_name);
}
*/
$mail->Subject = $payload['subject'];
$mail->Body = $payload['body'];
return $mail->Send();
}
<?php
$txpcfg['db'] = 'db_name';
$txpcfg['user'] = 'db_user';
$txpcfg['pass'] = 'db_password';
$txpcfg['host'] = 'db_host';
$txpcfg['table_prefix'] = '';
$txpcfg['txpath'] = '/path/to//textpattern';
$txpcfg['dbcharset'] = 'utf8';
/**
* smtp server config
*/
$txpcfg['smtp_host'] = 'your_smtp_host';
$txpcfg['smtp_port'] = 'port_number';
$txpcfg['smtp_username'] = 'your_smtp_user';
$txpcfg['smtp_password'] = 'your_smtp_password';
$txpcfg['smtp_encryption'] = 'tsl or ssl';
?>
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
-- --------------------------------------------------------
--
-- Table structure for table `contact_list`
--
CREATE TABLE IF NOT EXISTS `contact_list` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`email` varchar(256) NOT NULL DEFAULT '''''',
`first_name` varchar(128) NOT NULL DEFAULT '''''',
`last_name` varchar(128) NOT NULL DEFAULT '''''',
`company` varchar(256) NOT NULL DEFAULT '''''',
`details` varchar(1024) NOT NULL DEFAULT '''''',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment