View source
<?php
function regcode_mailer_action_info() {
return array(
'regcode_mailer_mail_action' => array(
'type' => 'regcode',
'description' => t('Mail codes'),
'configurable' => TRUE,
'hooks' => array(
'any' => TRUE,
),
),
);
}
function regcode_mailer_mail_action_form() {
$form = array();
$form['regcode_mailer_subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#required' => TRUE,
);
$form['regcode_mailer_message'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#description' => t('Enter the plaintext email to be sent to the user.'),
'#rows' => 8,
'#required' => TRUE,
);
$form['regcode_mailer_tokens'] = array(
'#title' => t('Replacement patterns'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['regcode_mailer_tokens']['tokens'] = array(
'#value' => theme('token_help', 'site'),
);
$form['regcode_mailer_emails'] = array(
'#type' => 'textarea',
'#title' => t('Email addresses'),
'#description' => t('List the email addresses to send the registration code to. Separate each email with a new line.'),
'#rows' => 8,
'#required' => TRUE,
);
$form['regcode_mailer_tags'] = array(
'#type' => 'checkboxes',
'#title' => t('Tags'),
'#options' => regcode_get_vocab_terms(),
'#description' => t('You may assign tags to track which codes have been sent.'),
);
return $form;
}
function regcode_mailer_mail_action_validate($form, $form_state) {
if (strpos($form_state['values']['regcode_mailer_message'], '[regcode]') === FALSE) {
form_set_error('regcode_mailer_message', t('Cannot send message without registration code - you should insert the [regcode] token in the message body.'));
}
}
function regcode_mailer_mail_action_submit($form, $form_state) {
$emails = array_filter(explode("\n", $form_state['values']['regcode_mailer_emails']));
array_map('trim', $emails);
return array(
'subject' => $form_state['values']['regcode_mailer_subject'],
'message' => $form_state['values']['regcode_mailer_message'],
'emails' => $emails,
'terms' => array_filter($form_state['values']['regcode_mailer_tags']),
);
}
function regcode_mailer_mail_action(&$object, &$context = array()) {
static $i = 0;
if (!isset($context['emails'][$i])) {
drupal_set_message(t('Ran out of emails, unable to send code (%code).', array(
'%code' => $object->code,
)));
return FALSE;
}
$params = array(
'regcode' => $object,
'message' => $context['message'],
'subject' => $context['subject'],
);
$email = $context['emails'][$i];
$message = drupal_mail('regcode_mailer', 'regcode', $email, language_default(), $params);
drupal_set_message(t("Sent message with code %code to %email", array(
'%code' => $object->code,
'%email' => $email,
)));
db_query('INSERT INTO {regcode_mailer} (rid, mailed, recipient) VALUES (%d, %d, "%s")', $object->rid, time(), $email);
foreach ($context['terms'] as $term_id) {
db_query('REPLACE INTO {regcode_term} (rid, tid) VALUES (%d, %d)', $object->rid, $term_id);
}
$i++;
}
function regcode_mailer_mail($key, &$message, $params) {
switch ($key) {
case 'regcode':
$message['subject'] = token_replace($params['subject'], 'regcode', $params['regcode']);
$message['body'][] = token_replace($params['message'], 'regcode', $params['regcode']);
break;
}
}
function regcode_mailer_token_list($type = 'site') {
if (arg(2) === 'regcode') {
$tokens['user']['regcode'] = t('The next available registration code.');
return $tokens;
}
}
function regcode_mailer_token_values($type, $object = NULL, $options = array()) {
if ($type == 'regcode') {
$tokens['regcode'] = $object->code;
return $tokens;
}
}