I need some help with the mail-attach module. I modified it a bit so that anonymous user can get to it. I added a text field where users are required to input their phone number. I have a problem. The phone number is not showing up in the email when it is sent without a file attached but is showing up when a file is attached. I need to display the phone number and body of the email with and without a file attach. I think I know where the problem is, I just don't know exactly how to go about fixing the problem.

Here is my code I believe the problem is around lines 230 and 277. Any help would be appreciate it. I marked where I believe the problem is.

<?php
// $Id: mailattach.module,v 1.2 2007/05/23 21:50:18 creaziondev Exp $

/**
* @file
* Contact form with the ability to attach files.
*/

/**
* Implementation of hook_help().
*/
function mailattach_help($section) {
switch ($section) {
case 'admin/help#mailattach':
return t('Generates a contact form with the attachment-fields.');
break;
}
}

/**
* Implementation of hook_perm
*/
function mailattach_perm() {
return array('access mailattach');
}

/**
* Implementation of hook_menu().
*/
function mailattach_menu($may_cache) {
$items = array();

if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/mailattach',
'title' => t('Mailattach settings'),
'callback' => 'drupal_get_form',
'callback arguments' => array('mailattach_admin_settings'),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
$items[] = array(
'path' => 'mailattach',
'title' => module_exists('contact') ? t('Contact') : t('Contact with file attachment'),
'callback' => 'mailattach_site_page',
'access' => user_access('access mailattach'),
'type' => MENU_NORMAL_ITEM,
);
}

return $items;
}

function mailattach_admin_settings() {
global $user;

$form['general_settings'] = array(
'#type' => 'fieldset',
'#title' => t('General Settings')
);
$form['general_settings']['mailattach_recipient'] = array(
'#type' => 'textfield',
'#title' => t('Recipient'),
'#default_value' => variable_get('mailattach_recipient', $user->name),
);
$form['general_settings']['mailattach_recipient_mail'] = array(
'#type' => 'textfield',
'#title' => t("Recipient's email address"),
'#default_value' => variable_get('mailattach_recipient_mail',
$user->mail),
);
$form['general_settings']['mailattach_reply'] = array(
'#type' => 'textfield',
'#title' => t("Reply"),
'#default_value' => variable_get('mailattach_reply', t('Thank you for your message.')),
);
$form['general_settings']['mailattach_success'] = array(
'#type' => 'textfield',
'#title' => t("Success message"),
'#default_value' => variable_get('mailattach_success', 'Your message has been sent to us.'),
);
$form['general_settings']['mailattach_goto'] = array(
'#type' => 'textfield',
'#title' => t("Redirect after submit"),
'#default_value' => variable_get('mailattach_goto', ''),
);
$form['general_settings']['mailattach_attachment_fields'] = array(
'#type' => 'select',
'#title' => t("Attachment fields"),
'#default_value' => variable_get('mailattach_attachment_fields', 3),
'#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6,
7 => 7, 8 => 8, 9 => 9),
);
$form['general_settings']['mailattach_hourly_threshold'] = array(
'#type' => 'select',
'#title' => t('emails per hour'),
'#default_value' => variable_get('mailattach_hourly_threshold', 3),
'#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6,
7 => 7, 8 => 8, 9 => 9),
);

$form['error'] = array(
'#type' => 'fieldset',
'#title' => t('Error handling')
);
$form['error']['mailattach_goto_error'] = array(
'#type' => 'textfield',
'#title' => t('Error redirect'),
'#default_value' => variable_get('mailattach_goto_error', ''),
'#description' => t('url to which the user is redirected in case of an error'),
);
$form['error']['mailattach_spam_msg'] = array(
'#type' => 'textfield',
'#title' => t('Message for our spammers'),
'#default_value' => variable_get('mailattach_spam_msg', 'Stop spamming'),
);

return system_settings_form($form);
}

/**
* Generates the contact site
*/
function mailattach_site_page() {
global $user;

{
// otherwise we generate the contact form
$output = drupal_get_form('mailattach_mail_page');
}

return $output;
}

/**
* Create the contact form array
*/
function mailattach_mail_page() {

$form['name'] = array(
'#type' => 'textfield',
'#title' => t('your name'),
'#default_value' => $object['name'],
'#size' => 30,
'#maxlength' => 128,
'#required' => TRUE,
);
$form['eMail'] = array(
'#type' => 'textfield',
'#title' => t('your email address'),
'#default_value' => $object['eMail'],
'#size' => 30,
'#maxlength' => 128,
'#required' => TRUE,
);
$form['number'] = array(
'#type' => 'textfield',
'#title' => t('contact number(xxx-xxx-xxxx)'),
'#default_value' => $object['number'],
'#size' => 15,
'#maxlength' => 12,
'#required' => TRUE,
);
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('subject'),
'#default_value' => $object['subject'],
'#size' => 30,
'#maxlength' => 128,
'#required' => TRUE,
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => t('your message'),
'#default_value' => $object['message'],
'#size' => 30,
'#maxlength' => 128,
'#rows' => 7,
'#required' => TRUE,
);
$form['file1'] = array(
'#type' => 'file',
'#title' => t('attach your files here'),
);
for($i = 2; $i <= variable_get('mailattach_attachment_fields', 3); $i++){
$form['file'.$i] = array(
'#type' => 'file',
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('send email'),
);
$form['#attributes'] = array(
'enctype' => 'multipart/form-data',
'id' => 'mailattach',
);

return $form;

}

/**
* Validate the contact form submission.
*/
function mailattach_mail_page_validate($form_id, $form_values) {
// first we validate if there is an email injection
foreach($form_values as $field => $value){
if(preg_match('/(%0A|%0D|\n+|\r+|content-type:|to:|cc:|bcc:)/i', $value)){
form_set_error($field, variable_get('mailattach_spam_msg', 'Stop spamming'));
break;
}
}

// then we validate the email address
if( $error = user_validate_mail($form_values['eMail']) )
form_set_error('eMail', $error);

}

/**
* contact form submission
*/
function mailattach_mail_page_submit($form_id, $form_values) {
global $user;

$headers = array();
$mailkey = 'mailattach';
$from = check_plain($form_values['name']).'<'.$form_values['eMail'].'>';
$recipient = variable_get('mailattach_recipient_name', $user->name).'<'.variable_get('mailattach_recipient_mail', $user->mail).'>';
$subject = check_plain($form_values['subject']);
$body = wordwrap($form_values['message']);
$bodyn = wordwrap($form_values['number']); I added this variable to add a phone number field
$reply = variable_get('mailattach_reply', t('Thank you for your message.'));
$fileFields = 1;

// we check for attachments
do{
$files = file_check_upload('file'.$fileFields++) ? TRUE : FALSE;
}while(!$files && $fileFields <= 3);

if($files){
$trenner = md5(uniqid(time()));
$headers = array('Content-Type' => "multipart/mixed; boundary=$trenner");
$message = "\n--$trenner\n";
$message .= "Content-Type: text/plain; charset=UTF-8; format=flowed;"."\n\n"; // sets the mime type
$message .= $body."\n";
$message .= $bodyn."\n";I wasn't to sure about how to display this
$message .= "\n\n";
for($i=1;$i<=3;$i++){
$file = file_check_upload('file'.$i);
if($file->filename){
$file->filepath = str_replace("\\","\\\\",$file->filepath);
$message .= "--$trenner"."\n";
$messagen .= "--$trenner"."\n";
$message .= "Content-Type:$file->filemime;\n\tname=$file->filename\n";
$message .= "Content-Transfer-Encoding: base64\n";I'm not sure if I need to add another one
$message .= "Content-Disposition: attachment;\n\tfilename=$file->filename\n\n";
$filedata = fread(fopen($file->filepath, "rb"), $file->filesize);
$message .= chunk_split(base64_encode($filedata));
$message .= "\n\n";
}
}
$message .= "--$trenner--";
}else{
$message = $body;I try added $message = $bodyn; to this as well but it did not work. It would display the phone number but not the message in that case. This is where I believe the problem is. I need to call upon $bodyn and display it in the message as well, but I'm not sure how.
}

// send mail
$mailError = !drupal_mail($mailkey, $recipient, $subject, $message, $messagen, $from, $headers);

// Reply
$mailError = !drupal_mail($mailkey, $from, $subject, wordwrap($reply), $recipient);

if(!$mailError){
// Log the operation:
flood_register_event('mailattach');
watchdog('mail', t('%name-from used the contact form', array('%name-from'=> theme('placeholder', $form_values['name'] ." <$from>"),)));

drupal_set_message(variable_get('mailattach_success', 'Your message has been sent to us.'));
drupal_goto(variable_get('mailattach_goto', ''));
}
else{
drupal_set_message('An error occurred. Please contact your administrator.', 'error');
drupal_goto(variable_get('mailattach_goto_error', ''));
}

}

Comments

jody lynn’s picture

You really shouldn't hack modules. I don't see any reason you couldn't use hook_form_alter to modify the form to do what you want.

--Zivtech--