contact_attach.module in Contact Attach 6
Same filename and directory in other branches
Allows attaching files to e-mails sent using the site-wide contact form.
This module gives users the ability of attaching one or more files to e-mails sent using the site-wide contact form.
File
contact_attach.moduleView source
<?php
/**
* @file
* Allows attaching files to e-mails sent using the site-wide contact form.
*
* This module gives users the ability of attaching one or more files to
* e-mails sent using the site-wide contact form.
*/
/**
* Implementation of hook_help().
*/
function contact_attach_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/settings/contact_attach':
$output = t('This module gives users the ability of attaching one or more files to e-mails sent using the site-wide contact form.');
break;
}
return $output;
}
// End of contact_attach_help().
/**
* Implementation of hook_perm().
*/
function contact_attach_perm() {
if (module_exists('og_contact')) {
$allowed_permissions = array(
'send attachments with site-wide contact form',
'send attachments with user contact form',
'send attachments with og contact form',
);
}
else {
$allowed_permissions = array(
'send attachments with site-wide contact form',
'send attachments with user contact form',
);
}
return $allowed_permissions;
}
// End of contact_attach_perm().
/**
* Implementation of hook_menu().
*/
function contact_attach_menu() {
$items = array();
$items['admin/settings/contact_attach'] = array(
'title' => 'Contact attach',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'contact_attach_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'description' => 'Configure the Contact attach module.',
);
return $items;
}
// End of contact_attach_menu().
/**
* Administration settings form.
*
* @return
* The completed form definition.
*/
function contact_attach_admin_settings() {
$form = array();
$form['contact_attach_number'] = array(
'#type' => 'textfield',
'#title' => t('Number of attachments'),
'#default_value' => variable_get('contact_attach_number', '3'),
'#size' => 10,
'#description' => t('The number of attachments to allow on the contact form.'),
);
return system_settings_form($form);
}
// End of contact_attach_admin_settings().
/**
* Implementation of hook_form_alter().
*/
function contact_attach_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'contact_mail_user' && user_access('send attachments with user contact form') || $form_id == 'contact_mail_page' && user_access('send attachments with site-wide contact form') || $form_id == 'og_contact_mail_page' && user_access('send attachments with og contact form')) {
for ($i = 1; $i <= variable_get('contact_attach_number', '3'); $i++) {
$form['contact_attach_' . $i] = array(
'#type' => 'file',
'#title' => t('Attachment #!number', array(
'!number' => $i,
)),
'#weight' => $i,
);
}
// We do not allow anonymous users to send themselves a copy because it
// can be abused to spam people.
global $user;
if ($user->uid) {
$form['copy'] = array(
'#type' => 'checkbox',
'#title' => t('Send yourself a copy.'),
'#weight' => $i,
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send e-mail'),
'#weight' => $i + 1,
);
$form['#attributes']['enctype'] = 'multipart/form-data';
// Empty the array to make sure our function is the only one called.
if (!empty($form['#submit'])) {
unset($form['#submit']);
}
// Set the validate and submit functions to our functions.
switch ($form_id) {
case 'contact_mail_user':
$form['#validate'][] = 'contact_attach_contact_mail_user_validate';
$form['#submit'][] = 'contact_attach_contact_mail_user_submit';
break;
case 'contact_mail_page':
$form['#validate'][] = 'contact_attach_contact_mail_page_validate';
$form['#submit'][] = 'contact_attach_contact_mail_page_submit';
break;
case 'og_contact_mail_page':
$form['#validate'][] = 'contact_attach_og_contact_mail_page_validate';
$form['#submit'][] = 'contact_attach_og_contact_mail_page_submit';
break;
}
}
}
// End of contact_attach_form_alter().
/**
* Add custom validation to verify the attachments.
*
* @param form
* An associative array containing the structure of the form.
* @param form_state
* A keyed array containing the current state of the form.
*/
function contact_attach_contact_mail_page_validate($form, &$form_state) {
_contact_attach_upload_validate($form, $form_state);
}
// End of contact_attach_contact_mail_page_validate().
/**
* Override contact_mail_page_submit().
*
* @param form
* An associative array containing the structure of the form.
* @param form_state
* A keyed array containing the current state of the form.
*/
function contact_attach_contact_mail_page_submit($form, &$form_state) {
global $language;
$values = $form_state['values'];
// E-mail address of the sender: as the form field is a text field,
// all instances of \r and \n have been automatically stripped from it.
$from = $values['mail'];
// Load category properties and save form values for email composition.
$contact = contact_load($values['cid']);
$values['contact'] = $contact;
// Send the e-mail to the recipients using the site default language.
$results[0] = drupal_mail('contact_attach', 'contact_page_mail', $contact['recipients'], language_default(), $values, $from);
// If the user requests it, send a copy using the current language.
if ($values['copy']) {
$results[1] = drupal_mail('contact_attach', 'contact_page_copy', $from, $language, $values, $from);
}
// Send an auto-reply if necessary using the current language.
if ($contact['reply']) {
$results[2] = drupal_mail('contact_attach', 'contact_page_autoreply', $from, $language, $values, $contact['recipients']);
}
for ($i = 0; $i < 3; $i++) {
if (!empty($results[$i])) {
if (!$results[$i]['result']) {
watchdog('mail', '%name-from attempted to send an e-mail regarding %category, but was unsuccessful.', array(
'%name-from' => $values['name'] . " [{$from}]",
'%category' => $contact['category'],
));
drupal_set_message(t('Your message was NOT sent.'));
}
else {
flood_register_event('contact');
watchdog('mail', '%name-from sent an e-mail regarding %category.', array(
'%name-from' => $values['name'] . " [{$from}]",
'%category' => $contact['category'],
));
drupal_set_message(t('Your message has been sent.'));
}
}
}
// Jump to home page rather than back to contact page to avoid
// contradictory messages if flood control has been activated.
$form_state['redirect'] = '';
}
// End of contact_attach_contact_mail_page_submit().
/**
* Add custom validation to verify the attachments.
*
* @param form
* An associative array containing the structure of the form.
* @param form_state
* A keyed array containing the current state of the form.
*/
function contact_attach_contact_mail_user_validate($form, &$form_state) {
_contact_attach_upload_validate($form, $form_state);
}
// End of contact_attach_contact_mail_user_validate().
/**
* Override contact_mail_user_submit().
*
* @param form
* An associative array containing the structure of the form.
* @param form_state
* A keyed array containing the current state of the form.
*/
function contact_attach_contact_mail_user_submit($form, &$form_state) {
global $user, $language;
$account = $form_state['values']['recipient'];
// Send from the current user to the requested user.
$to = $account->mail;
$from = $user->mail;
// Save both users and all form values for email composition.
$values = $form_state['values'];
$values['account'] = $account;
$values['user'] = $user;
// Send the e-mail in the requested user language.
$results[0] = drupal_mail('contact_attach', 'contact_user_mail', $to, user_preferred_language($account), $values, $from);
// Send a copy if requested, using current page language.
if ($form_state['values']['copy']) {
$results[1] = drupal_mail('contact_attach', 'contact_user_copy', $from, $language, $values, $from);
}
for ($i = 0; $i < 2; $i++) {
if (!empty($results[$i])) {
if (!$results[$i]['result']) {
watchdog('mail', '%name-from attempted to send %name-to an e-mail, but was unsuccessful.', array(
'%name-from' => $user->name,
'%name-to' => $account->name,
));
drupal_set_message(t('The message has NOT been sent.'));
}
else {
flood_register_event('contact');
watchdog('mail', '%name-from sent %name-to an e-mail.', array(
'%name-from' => $user->name,
'%name-to' => $account->name,
));
drupal_set_message(t('The message has been sent.'));
}
}
}
// Back to the requested users profile page.
$form_state['redirect'] = "user/{$account->uid}";
}
// End of contact_attach_contact_mail_user_submit().
/* *
* Add custom validation to verify the attachments.
*
* @param form
* An associative array containing the structure of the form.
* @param form_state
* A keyed array containing the current state of the form.
* /
function contact_attach_og_contact_mail_page_validate($form, &$form_state) {
/** TODO: As there is not yet a 6.x compatible version of OG Contact, this function has not been tested. * /
_contact_attach_upload_validate($form, &$form_state);
} // End of contact_attach_og_contact_mail_page_validate().
*/
/* *
* Override og_contact_mail_page_submit().
*
* @param form
* An associative array containing the structure of the form.
* @param form_state
* A keyed array containing the current state of the form.
* /
function contact_attach_og_contact_mail_page_submit($form, &$form_state) {
/** TODO: As there is not yet a 6.x compatible version of OG Contact, this function has not been tested. * /
$name = og_contact_get_group_name($form_state['values']['gid']);
// E-mail address of the sender: as the form field is a text field,
// all instances of \r and \n have been automatically stripped from it.
$from = $form_state['values']['mail'];
// Load the group information:
$group = db_fetch_object(db_query("SELECT * FROM {og_contact} WHERE gid = %d", $form_state['values']['gid']));
// $contact = og_contact_get_recipients($form_state['values']['gid']);
$contact['recipients'] = og_contact_get_recipients($form_state['values']['gid']);
// $contact = contact_load($values['cid']);
$values['contact'] = $contact;
// Send the e-mail to the recipients using the site default language.
$results[0] = drupal_mail('contact_attach', 'og_contact_page_mail', $contact['recipients'], language_default(), $values, $from);
// If the user requests it, send a copy using the current language.
if ($form_state['values']['copy']) {
$results[1] = drupal_mail('contact_attach', 'og_contact_page_copy', $from, $language, $values, $from);
}
// Send an auto-reply if necessary using the current language.
if ($group->reply) {
$results[2] = drupal_mail('contact_attach', 'og_contact_page_autoreply', $from, $language, $values, $contact['recipients']);
}
for ($i = 0; $i < 3; $i++) {
if (!empty($results[$i])) {
if (!$results[$i]['result']) {
// Log the operation:
watchdog('mail', '%name-from attempted to send an e-mail regarding %category, but was unsuccessful.', array('%name-from' => $form_state['values']['name'] ." <$from>", '%category' => $name));
// Update user:
drupal_set_message(t('Your message was NOT sent.'));
}
else {
// Log the operation:
flood_register_event('og-contact');
watchdog('mail', '%name-from sent an e-mail regarding %category.', array('%name-from' => $form_state['values']['name'] ." <$from>", '%category' => $name));
// Update user:
drupal_set_message(t('Your message has been sent.'));
}
}
}
// Jump to group page rather than back to contact page to avoid
// contradictory messages if flood control has been activated.
$form_state['redirect'] = 'node/'. $group->gid;
} // End of contact_attach_og_contact_mail_page_submit().
*/
/**
* Implementation of hook_mail().
*/
function contact_attach_mail($key, &$message, $params) {
$language = $message['language'];
switch ($key) {
case 'contact_page_mail':
case 'contact_page_copy':
$contact = $params['contact'];
$message['subject'] .= t('[!category] !subject', array(
'!category' => $contact['category'],
'!subject' => $params['subject'],
), $language->language);
$message['body'][] = t("!name sent a message using the contact form at !form.", array(
'!name' => $params['name'],
'!form' => url($_GET['q'], array(
'absolute' => TRUE,
'language' => $language,
)),
), $language->language);
$message['body'][] = $params['message'];
// Attachment processing begins here.
$return_message = _contact_attach_process_attachments($message);
if (!empty($return_message)) {
$message['headers'] = $return_message['headers'];
$message['body'] = $return_message['body'];
}
// Attachment processing ends here.
break;
case 'contact_page_autoreply':
$contact = $params['contact'];
$message['subject'] .= t('[!category] !subject', array(
'!category' => $contact['category'],
'!subject' => $params['subject'],
), $language->language);
$message['body'][] = $contact['reply'];
break;
case 'contact_user_mail':
case 'contact_user_copy':
$user = $params['user'];
$account = $params['account'];
$message['subject'] .= '[' . variable_get('site_name', 'Drupal') . '] ' . $params['subject'];
$message['body'][] = "{$account->name},";
$message['body'][] = t("!name (!name-url) has sent you a message via your contact form (!form-url) at !site.", array(
'!name' => $user->name,
'!name-url' => url("user/{$user->uid}", array(
'absolute' => TRUE,
'language' => $language,
)),
'!form-url' => url($_GET['q'], array(
'absolute' => TRUE,
'language' => $language,
)),
'!site' => variable_get('site_name', 'Drupal'),
), $language->language);
$message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array(
'!url' => url("user/{$account->uid}", array(
'absolute' => TRUE,
'language' => $language,
)),
), $language->language);
$message['body'][] = t('Message:', NULL, $language->language);
$message['body'][] = $params['message'];
// Attachment processing begins here.
$return_message = _contact_attach_process_attachments($message);
if (!empty($return_message)) {
$message['headers'] = $return_message['headers'];
$message['body'] = $return_message['body'];
}
// Attachment processing ends here.
break;
}
}
/**
* Check for attachments and process them, if one or more exists.
*
* @param message
* The message, as it exists so far.
* @return
* The message, including processed attachment(s).
*/
function _contact_attach_process_attachments($message) {
$return_message = array();
// Loop through each possible attachment.
for ($i = 1; $i <= variable_get('contact_attach_number', '3'); $i++) {
if ($file = file_save_upload('contact_attach_' . $i)) {
// Check to see if the attachment exists.
if ($file->filesize > 0) {
// An attachment exists, so save it to an array for later processing.
$files[] = $file;
}
}
}
// If the array cantains something, we have one or more attachments to
// process. If it does not contain anything, we send back an empty $body,
// indicating no attachments exist.
if (!empty($files)) {
// Set initial values.
$body = '';
$return_message = array();
$boundary_id = md5(uniqid(time()));
$message['headers']['Content-Type'] = 'multipart/mixed; boundary="' . $boundary_id . '"';
$body = "\n--{$boundary_id}\n";
$body .= "Content-Type: text/plain; charset=UTF-8; format=flowed;\n\n";
// sets the mime type
// Prepare the body:
$body .= implode("\n\n", $message['body']);
$body .= "\n\n\n";
// Add the attachments.
// Loop through each possible attachment.
for ($i = 0; $i < count($files); $i++) {
// Process the attachment.
$body .= "--{$boundary_id}\n";
$body .= _contact_attach_add_attachment($files[$i]);
$body .= "\n\n";
}
$body .= "--{$boundary_id}--\n\n";
$return_message['headers'] = $message['headers'];
$return_message['body'] = $body;
}
return $return_message;
}
// End of _contact_attach_process_attachments().
/**
* Add an attachment to the message body.
*
* @param file
* An attachment to add to the message.
* @return
* The processed attachment, ready for appending to the message.
*/
function _contact_attach_add_attachment($file) {
$attachment = "Content-Type: " . $file->filemime . "; name=\"" . basename($file->filename) . "\"\n";
$attachment .= "Content-Transfer-Encoding: base64\n";
$attachment .= "Content-Disposition: attachment; filename=\"" . basename($file->filename) . "\"\n\n";
if (file_exists($file->filepath)) {
$attachment .= chunk_split(base64_encode(file_get_contents($file->filepath)));
}
else {
$attachment .= chunk_split(base64_encode(file_get_contents(file_directory_temp() . '/' . $file->filename)));
}
return $attachment;
}
// End of _contact_attach_add_attachment().
/**
* Validate the attachment(s).
*/
function _contact_attach_upload_validate() {
global $user;
// Validate file against all users roles.
// Only denies an upload when all roles prevent it.
foreach ($user->roles as $rid => $name) {
// Validate the file type, based on file extension.
$extensions = variable_get("upload_extensions_{$rid}", variable_get('upload_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'));
// Validate the upload limit, based on file size.
$file_limit = variable_get("upload_uploadsize_{$rid}", variable_get('upload_uploadsize_default', 1)) * 1024 * 1024;
// Validate the upload limit, based on the user's uploads.
$user_limit = variable_get("upload_usersize_{$rid}", variable_get('upload_usersize_default', 1)) * 1024 * 1024;
$validators = array(
'file_validate_extensions' => array(
$extensions,
),
'file_validate_name_length' => array(),
'file_validate_size' => array(
$file_limit,
$user_limit,
),
);
// Loop through each possible attachment.
for ($i = 1; $i <= variable_get('contact_attach_number', '3'); $i++) {
file_save_upload('contact_attach_' . $i, $validators);
}
}
}
// End of _contact_attach_upload_validate().
Functions
Name | Description |
---|---|
contact_attach_admin_settings | Administration settings form. |
contact_attach_contact_mail_page_submit | Override contact_mail_page_submit(). |
contact_attach_contact_mail_page_validate | Add custom validation to verify the attachments. |
contact_attach_contact_mail_user_submit | Override contact_mail_user_submit(). |
contact_attach_contact_mail_user_validate | Add custom validation to verify the attachments. |
contact_attach_form_alter | Implementation of hook_form_alter(). |
contact_attach_help | Implementation of hook_help(). |
contact_attach_mail | Implementation of hook_mail(). |
contact_attach_menu | Implementation of hook_menu(). |
contact_attach_perm | Implementation of hook_perm(). |
_contact_attach_add_attachment | Add an attachment to the message body. |
_contact_attach_process_attachments | Check for attachments and process them, if one or more exists. |
_contact_attach_upload_validate | Validate the attachment(s). |