Skip to content

Instantly share code, notes, and snippets.

@frankmullenger
Created November 26, 2015 22:39
Show Gist options
  • Save frankmullenger/5bf3e539bb6727c0d99f to your computer and use it in GitHub Desktop.
Save frankmullenger/5bf3e539bb6727c0d99f to your computer and use it in GitHub Desktop.
Simple GPG encryption test
---
Only:
environment: 'dev'
---
GPGMailer:
options:
- homedir: '/var/www/public/assets/'
relative_homedir: 'assets/'
debug: false
EmailPage_Controller:
encryptKey: 'frank+to@silverstripe.com'
signKey: 'frank+from@silverstripe.com'
signKeyPassPhrase: 'K3yPa55Phr4s3'
<?php
class EmailPage extends Page {
}
class EmailPage_Controller extends Page_Controller {
private static $allowed_actions = array(
'Form'
);
public function Form() {
$currentTest = date('Y-m-d h:i:s');
$fields = FieldList::create(
HeaderField::create('EmailHeader', 'Contact Us', 3),
CheckboxField::create('Encrypt', 'Encrypt this email'),
TextareaField::create('Body', 'Email content', 'Email content here ' . $currentTest)
);
$actions = FieldList::create(
FormAction::create('send', 'Send')
);
$required = RequiredFields::create(
'Body'
);
return Form::create($this, 'Form', $fields, $actions, $required);
}
public function send(array $data, Form $form) {
// If encrypting email change subject and Mailer
$encrypt = isset($data['Encrypt']);
$subject = ($encrypt) ? 'Encrypted email' : 'Non encrypted email';
// Create email, subject line is NOT encrypted
$email = new Email(
'frank+from@silverstripe.com',
'frank+to@silverstripe.com',
$subject,
$data['Body']
);
// Set up encryption mailer
if($encrypt) {
$mailer = new GPGMailer(
$this->config()->encryptKey,
$this->config()->signKey,
$this->config()->signKeyPassPhrase
);
$email->set_mailer($mailer);
}
$result = $email->sendPlain();
$message = ($result) ? 'Email was sent' : 'Email was NOT sent';
return array(
'Content' => "<h2>$message</h2>",
'Form' => $this->Form
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment