Skip to content

Instantly share code, notes, and snippets.

@michaeltwofish
Created April 18, 2010 03:17
Show Gist options
  • Save michaeltwofish/369975 to your computer and use it in GitHub Desktop.
Save michaeltwofish/369975 to your computer and use it in GitHub Desktop.
<?php
/**
* A generic form mailer, designed to be built upon by other plugins and themes
*/
class FormMailer extends Plugin
{
const X_MAILER = 'Habari Form Mailer';
public function __get($name) {
switch ( $name ) {
case 'default_email':
// If email is set in Options, use that
$default_email = Options::get('form_mailer__default_email');
if ( $default_email == NULL ) {
// Use the first users' email address
$users = Users::get();
$default_email = $users[0]->email;
}
return $default_email;
}
}
/**
* Implement the simple plugin configuration.
* @return FormUI The configuration form
*/
public function configure()
{
$form = new FormUI( 'form_mailer' );
// Add a text control for the address you want the email sent to
$form->append('static', 'help_to', '<small>Override by calling <code>$theme->form_mailer("context", array("to" => "mail@example.com"))</code></small>');
$form->append('text', 'default_to', 'option:form_mailer__', _t('Default recipient', 'form_mailer'));
if ( !$form->default_to->value ) {
$form->default_to->value = $this->default_email;
}
// Add a text control for the address you want the email sent from
$form->append('static', 'help_from', '<small>Override by calling <code>$theme->form_mailer("context", array("from" => "mail@example.com"))</code></small>');
$form->append('text', 'default_from', 'option:form_mailer__', _t('Default sender', 'form_mailer'));
if ( !$form->default_from->value ) {
$form->default_from->value = $this->default_email;
}
$form->append( 'submit', 'save', _t('Save') );
return $form->get();
}
/**
* Display a FormUI that will be mailed on submit.
*
* @param String context The context of the form, used by plugins to extend this FormUI
* @param Array params Options to be set on this FormUI
*/
public function theme_form_mailer($theme, $context, $params = array())
{
$form = new FormUI('mailer_'. Utils::slugify($context, '_'));
foreach ( $params as $param => $value ) {
$form->set_option($param, $value);
}
$form->append('textarea', 'message', 'null:null', _t('Message'));
// Create the submit button
$form->append('submit', 'submit', _t('Send'));
$form->on_success( array( $this, 'send_mail' ) );
// Return the form object
return $form->out();
}
public function send_mail($form)
{
// if to is set in the form options, use that
if ( $form->get_option('to') ) {
$to = $form->get_option('to');
}
else {
$to = $this->default_email;
}
// if from is set in the form options, use that
if ( $form->get_option('from') ) {
$from = $form->get_option('from');
}
else {
$from = $this->default_email;
}
$headers['From'] = $from;
$headers['X-Mailer'] = self::X_MAILER;
$contents = '';
foreach ( $form->get_values() as $field => $value )
{
if ( $field != 'submit' ) {
$contents .= $field.": ".$value.PHP_EOL.PHP_EOL;
}
}
Utils::mail($to, 'subject', $contents, $headers);
}
/**
* Add update beacon support
*/
public function action_update_check()
{
Update::add( __CLASS__, 'dee60755-2fae-4c70-b24c-843353581017', $this->info->version );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment