Created
May 17, 2012 21:03
-
-
Save kevinchampion/2721584 to your computer and use it in GitHub Desktop.
Figure out Drupal 7 mail formatting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param $form | |
* @param $form_state | |
*/ | |
function lsadataclassify_keyad_route_submit($form, &$form_state) { | |
global $base_url; | |
// For each pm checked | |
for ($i = 0; $i < $form_state['values']['num_compliance']; $i++){ | |
if ($form_state['values']['row-' . $i]['route'] == 1){ | |
// Add admin_route to compliance | |
$node = node_load($form_state['values']['row-' . $i]['nid']); | |
$node->field_compliance_admin_route[$node->language][] = array('value' => $form_state['values']['routee']); | |
if ($node = node_submit($node)) { // Prepare node for saving | |
node_save($node); | |
} | |
$routed_nodes[] = $node; | |
} | |
} | |
// Send email collating all checked pms | |
// Get admin active setting boolean | |
if (variable_get('route_email', 1)){ | |
$message = decode_entities(t('@message', array('@message' => variable_get('route_message', 'A researcher/scholar needs your help complying with one or more protection measures in the researcher/scholar\'s new submission.')))); | |
$research_data_node = node_load($routed_nodes[0]->field_compliance_data[$routed_nodes[0]->language][0]['nid']); | |
$submitter = user_load($research_data_node->uid); | |
if (empty($research_data_node->field_research_affiliation_ref[$research_data_node->language][0]['tid'])) { | |
$affiliation = $research_data_node->field_research_affiliation_other[$research_data_node->language][0]['value']; | |
} | |
else { | |
$affiliation = taxonomy_term_load($research_data_node->field_research_affiliation_ref[$research_data_node->language][0]['tid']); | |
$affiliation = $affiliation->name; | |
} | |
$data_types = ''; | |
$dts = $research_data_node->field_research_data_type_ref[$research_data_node->language]; | |
$dts_size = count($dts); | |
for ($i = 0; $i < $dts_size; $i++){ | |
$dt_node = node_load($dts[$i]['nid']); | |
$data_types .= $dt_node->title; | |
// Format array to sentence structure | |
if ($dts_size == 2 && ($dts_size - 1) > $i) { | |
$data_types .= ' and '; | |
} | |
elseif ($dts_size > 2 && ($dts_size - 2) > $i) { | |
$data_types .= ', '; | |
} | |
elseif ($dts_size > 2 && ($dts_size - 1) > $i) { | |
$data_types .= ', and '; | |
} | |
else { | |
// $data_types .= '.'; | |
} | |
} | |
$replacements = array( | |
'@field_of_research' => '<b>' . $research_data_node->title . '</b>', | |
'@submitter' => '<b>' . $submitter->name . '</b>', | |
'@sensitivity' => '<b>' . $research_data_node->field_research_sensitivity[$research_data_node->language][0]['value'] . '</b>', | |
'@principal_investigator' => '<b>' . $research_data_node->field_research_pi[$research_data_node->language][0]['value'] . '</b>', | |
'@affiliation' => '<b>' . $affiliation . '</b>', | |
'@key_admin' => '<b>' . $research_data_node->field_research_key_admin[$research_data_node->language][0]['value'] . '</b>', | |
'@research_focus' => $research_data_node->body[$research_data_node->language][0]['value'], | |
'@data_types' => '<b>' . $data_types . '</b>', | |
); | |
$message = format_string($message, $replacements); | |
// $message .= '<br /><br />'; | |
$message .= "\n\n" . '<p>' . t('@pi needs help complying with the following protection measures:', array('@pi' => $research_data_node->field_research_pi[$research_data_node->language][0]['value'])) . "\n\n"; | |
foreach ($routed_nodes as $routed_node) { | |
$pm_node = node_load($routed_node->field_compliance_pm[$routed_node->language][0]['nid']); | |
$message .= '- ' . $pm_node->title . "\n"; | |
} | |
$message .= '</p>' . "\n\n"; | |
$message .= '<p>' . t('Once compliant with all these protection measures, please return to this Research and Scholarship Data profile to confirm it is complete:') . '</p>'; | |
$path_to_confirmation = 'admin/data/' . $research_data_node->nid . '/route/' . check_url($form_state['values']['routee']) . '/confirm'; | |
$message .= '<p>' . l($base_url . $path_to_confirmation, $path_to_confirmation) . '</p>'; | |
watchdog('lsadataclassify', 'message before: !notification', array('!notification' => $message), WATCHDOG_NOTICE); | |
$message = drupal_html_to_text($message); | |
watchdog('lsadataclassify', 'message after: !notification', array('!notification' => $message), WATCHDOG_NOTICE); | |
lsadataclassify_mail_send(array('email' => $form_state['values']['routee'] . '@umich.edu', 'message' => $message)); | |
} | |
} | |
function lsadataclassify_mail_send($form_values) { | |
// All system mails need to specify the module and template key (mirrored from | |
// hook_mail()) that the message they want to send comes from. | |
$module = 'lsadataclassify'; | |
$key = 'contact_message'; | |
// Specify 'to' and 'from' addresses. | |
$to = $form_values['email']; | |
$from = variable_get('site_mail', '[email protected]'); | |
// "params" loads in additional context for email content completion in | |
// hook_mail(). In this case, we want to pass in the values the user entered | |
// into the form, which include the message body in $form_values['message']. | |
$params = $form_values; | |
// The language of the e-mail. This will one of three values: | |
// * user_preferred_language(): Used for sending mail to a particular website | |
// user, so that the mail appears in their preferred language. | |
// * global $language: Used when sending a mail back to the user currently | |
// viewing the site. This will send it in the language they're currently | |
// using. | |
// * language_default(): Used when sending mail to a pre-existing, 'neutral' | |
// address, such as the system e-mail address, or when you're unsure of the | |
// language preferences of the intended recipient. | |
// | |
// Since in our case, we are sending a message to a random e-mail address that | |
// is not necessarily tied to a user account, we will use the site's default | |
// language. | |
$language = language_default(); | |
// Whether or not to automatically send the mail when drupal_mail() is | |
// called. This defaults to TRUE, and is normally what you want unless you | |
// need to do additional processing before drupal_mail_send() is called. | |
$send = TRUE; | |
// Send the mail, and check for success. Note that this does not guarantee | |
// message delivery; only that there were no PHP-related issues encountered | |
// while sending. | |
$result = drupal_mail($module, $key, $to, $language, $params, $from, $send); | |
if ($result['result'] == TRUE) { | |
drupal_set_message(t('These protection measures have been routed and an email has been sent.')); | |
} | |
else { | |
drupal_set_message(t('There was a problem routing these protection measures and the email was not sent.'), 'error'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment