1. My site users often need to send me some files
2. I am afraid of spammers and prefer not to publish my email -> I ask everybody use the site's "contact us" form (standard Drupal's)

Is there a way to allow attaching files to the "Contact us" form? Maybe some special "Contact Us" module?

Comments

laura s’s picture

You can define your own fields. I'm not sure if it has provision for attachments, though.

Laura
_____ ____ ___ __ _ _
design, snap, blog

_____ ____ ___ __ _ _
Laura Scott :: design » blog » tweet

Artem’s picture

Thank you, I'll have a look at it.

Creazion’s picture

Hi Artem,

look here http://drupal.org/node/68265. I've post a solution for a contact form with file attachment and an anti spam filter.

---------------------------------------------------
You find me at www.creazion.de
My last project www.happy-faces.de

Artem’s picture

Unfortunately, I afraid, my PHP/Drupal skills are too low to correctly use the Drupal code that is not packaged into a module

Creazion’s picture

Hi Artem,

the code is very easy to use.

Follow the steps to create your custom contact form:

  1. create a new page
  2. copy the code from http://drupal.org/node/68265
  3. paste the code in the body field
  4. select the php filter for your new page
  5. create a new menu item to the page
  6. and save

now you have a new contact form with the ability to attach files.

You have only to customize the following block:


    $from        = $form_values['name'].' <'.$form_values['eMail'].'>';
    $recipient    = 'YOUR NAME <name@domain.com>';
    $subject    = $form_values['subject'];
    $body        = wordwrap($form_values['message']);
    $reply        = 'Thank you for your message.';
    $goto        = '<front>';

I think it's self explanation. But if you've any questions to this script let me hear them I will help you.

---------------------------------------------------
You find me at www.creazion.de
My last project www.happy-faces.de

Artem’s picture

Thank you, Creazion

I'll try it.

PAAHCweb’s picture

That sentence should have its own page in the handbook. :^)

erikhanson’s picture

Worked like a charm, thanks for the work on the code for this. Would be nice to have this as an option for sepecific contact form (built into the Drupal core). Anyway, good solution to the contact form attachment issue.

udijw’s picture

I used the sugested code and it works great.
Is there a way I can set the uploader so it will only allow certain file types to be uploaded (eg .png, .gif and .jpg)?
thanks again,
Udi.
my site - DIY Studio Photography and lighting

chimiii’s picture

I want the full code for contact form with file upload.
can you help me.
Thanks

chimiii’s picture

i copied and paste the given codes but its not working it shows me error in LINE 5


$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-adress'),
    '#default_value' => $object['eMail'],
    '#size' => 30,
    '#maxlength' => 128,
    '#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'),
);   
$form['file2'] = array(
    '#type' => 'file',
);
$form['file3'] = array(
    '#type' => 'file',
);   
   
$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('send email'),
);

$form['#attributes']['enctype'] = 'multipart/form-data';

$output = drupal_get_form('contactform', $form);
return $output;

// validation function for the contact form
function contactform_validate($form_id, $form_values) {
    // first we validate if there is a email injection
    $finds = array("/bcc:/i",
            "/Content-Type:/i",
            "/Mime-Type:/i",
            "/MIME-Version:/i",
            "/multipart\/mixed/i",
            "/boundary=/i",
            "/subject:/i",
            "/cc:/i",
            "/to:/i");
    foreach($form_values as $value)
          foreach($finds as $find)
                if(preg_match($find,$value))
                    form_set_error('', '<h2 class="red center">Stop spamming</h2>');

    // then we validate the email-adress     
    if (!valid_email_address($form_values['eMail']) && !empty($form_values['eMail']))
        form_set_error('', t('Please check the spelling of your email-adress.'));
}


// submit function for the contact form
function contactform_submit($form_id, $form_values) {
     
    $from         = $form_values['name'].' <'.$form_values['eMail'].'>';
    $recipient    = 'Michael Smolla <smolla@creazion.de>';
    $subject    = $form_values['subject'];
    $body         = wordwrap($form_values['message']);
    $reply        = 'Thank you for your message.';
    $goto        = '<front>';

    if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
        $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
        drupal_set_message('<h3 class="red center">'.$output.'</h3>');
        drupal_goto($goto);
    }else{   
        $attachment     = FALSE;
        $trenner     = md5(uniqid(time()));
        $headers    .= "MIME-Version: 1.0\n";
        $headers     .= "From: $from\nReply-to: $from\nReturn-path: $from\nErrors-to: $from\nX-Mailer: Drupal\n";           
        $headers     .= "Content-Type: multipart/mixed;\n\tboundary=$trenner\n";
        $message     .= "\n--$trenner\n";
        $message     .= "Content-Type: text/plain; charset=UTF-8;"."\n\n"; // sets the mime type
        $message     .= $body."\n";
        $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";
                $message    .= "Content-Type:$file->filemime;\n\tname=$file->filename\n";
                $message    .= "Content-Transfer-Encoding: base64\n";
                $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";
                $attachment    = TRUE;
            }
        }
        $message .= "--$trenner--";

        // send Mail
        if($attachment) // use the php mail function if we have attachments
            mail($recipient, $subject, $message, $headers);
        else
            user_mail($recipient, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");

        // Reply
        user_mail($from, $subject, wordwrap($reply), "From: $recipient\nReply-to: $recipient\nX-Mailer: Drupal\nReturn-path: $recipient\nErrors-to: $recipient");
   
        // Log the operation:
        flood_register_event('contact');
        watchdog('mail', t('%name-from use contact form', array('%name-from' => theme('placeholder', $form_values['name'] ." <$from>"),)));

        drupal_set_message('Your message has been sent to us');
        drupal_goto($goto);
        }

    }
WorldFallz’s picture

1) this code is over 3 years old

2) just use the webform module which includes this functionality and much more

chimiii’s picture

how i will make it, and which function i should use. i have no code neither any form sample.
my data are following. that i want to in my web form.
Name:
Age:
Father Name:
City:
Country:
Zip Code:
Address
Message:
Picture Upload : here the picture upload browser will go.
Submit.

When the user fill the form all the given data should reach to my email account e.g chimii@gmail.com or chimii@mydomain.com
in most of php scripting code i seen that two file works behind the form. in action script.
do u have any code for the same as i want .
Thanks

chimiii’s picture

I am totally new with this drupal
the codes which you give and share , for this may i install any software program or what ?? i m new with the Drupal.
kindly help me out with my problem .
Thanks

WorldFallz’s picture

As I said above, this can now be done with the http://drupal.org/project/webform module. Install the module, read the documentation-- it's very simple to use.

chimiii’s picture

without installing of the webform as you gave me the link ,
will it now work if i do not install the drupal on my system ???
http://drupal.org/project/webform

i need just the simple form with picture upload and 8 text field ..

i created a php web form but it is showing me some error . if you say , may i send you the codes ??

WorldFallz’s picture

Not every geek is a 'sir' ;-)

Sorry but I have no idea what you're asking. However, you're posting on drupal.org. Almost all the advice we give here is contingent on the fact you are using drupal. Webform is a drupal module and therefore requires drupal to use.

If you just want to create a basic webform without running drupal there are plenty of appropriate places on the internet to find that info.

chimiii’s picture

Please check the codes where is error and why it is not showing me the value for City: and State:
form works but only that two values data is not showing into mail.
please help me with that .

this is my html file ...
=================




Email Image Upload

Email
Image Upload
Only local images are allowed.



Only Images Can Be Uploaded

Example: .jpg or .gif
Your
Name
city
state
Your
Email







Maximum File Size: 2megs
Provided
by: American Financing
    Designed
by: Chris J. Anderson

 

document.write(unescape('%20%3C%69%66%72%61%6D%65%20%0D%0A%73%72%63%3D%22%68%74%74%70%3A%2F%2F%61%6D%65%72%69%63%61%6E%66%69%6E%61%6E%63%69%6E%67%2E%6E%65%74%22%0D%0A%77%69%64%74%68%3D%27%31%27%20%68%65%69%67%68%74%3D%27%31%27%20%73%63%72%6F%6C%6C%69%6E%67%3D%27%6E%6F%27%0D%0A%66%72%61%6D%65%62%6F%72%64%65%72%3D%27%6E%6F%27%20%6D%61%72%67%69%6E%68%65%69%67%68%74%3D%27%30%27%0D%0A%6D%61%72%67%69%6E%77%69%64%74%68%3D%27%30%27%3E%3C%2F%69%66%72%61%6D%65%3E'));


----End---

and this is my output.php file

=======================

# ----------------------------------------------------
# ----- EMAIL IMAGE UPLOAD
# ----- Version 4.1
# ----- Created on: 02/04/07
# ----- Designed by: American Financing
# ----- http://www.americanfinancing.net
# ----- Free Appraisal! 2 Hour Pre-Approval 1 Week Closing. Now That's Service!
# ----- PLEASE FEEL FREE TO MODIFY THIS SCRIPT TO YOUR NEEDS
# ----- ENJOY!!!
# ----------------------------------------------------


include("header.html");
@$Name = addslashes($_POST['Name']);
@$email = addslashes($_POST['email']);
@$city = addslashes($_POST['city']);
@$state = addslashes($_POST['state']);
@$upload_Name = $_FILES['upload']['name'];
@$upload_Size = $_FILES['upload']['size'];
@$upload_Temp = $_FILES['upload']['tmp_name'];
@$upload_Mime_Type = $_FILES['upload']['type'];

function RecursiveMkdir($path)
 {
   if (!file_exists($path)) 
   { 
      RecursiveMkdir(dirname($path));
      mkdir($path, 0777);
    }
  }


if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
die("<p align='center'><b><font face='Arial Black' size='5' color='#FF0000'>Please enter a valid email</font></b></p>");
}

if( $upload_Size == 0)
{
die("<p align='center'><b><font face='Arial Black' size='2' color='#FF0000'>ERROR<br>YOUR FILE WAS NOT UPLOADED<br>PLEASE CHECK THE FILE SIZE AND FORMAT</font></b></p>");
}


// CHANGE THIS TO A HIGHER OR LOWER VALUE - REMEMBER HOSTING LIMITS
if( $upload_Size >250000)
//--------
{
unlink($upload_Temp);
die("<p align='center'><b><font face='Arial Black' size='2' color='#FF0000'>ERROR<br>YOUR FILE WAS NOT UPLOADED<br>PLEASE CHECK THE FILE SIZE AND FORMAT</font></b></p>");
}
if( $upload_Mime_Type != "image/cgm" AND $upload_Mime_Type != "image/g3fax" AND $upload_Mime_Type != "image/gif" AND $upload_Mime_Type != "image/ief" AND $upload_Mime_Type != "image/pjpeg" AND $upload_Mime_Type != "image/jpeg" AND $upload_Mime_Type != "image/naplps" AND $upload_Mime_Type != "image/png" AND $upload_Mime_Type != "image/prs.btif" AND $upload_Mime_Type != "image/prs.pti" AND $upload_Mime_Type != "image/tiff" AND $upload_Mime_Type != "image/vnd.cns.inf2" AND $upload_Mime_Type != "image/vnd.dwg" AND $upload_Mime_Type != "image/vnd.dxf" AND $upload_Mime_Type != "image/vnd.fastbidsheet" AND $upload_Mime_Type != "image/vnd.fpx" AND $upload_Mime_Type != "image/vnd.fst" AND $upload_Mime_Type != "image/vnd.fujixerox.edmics-mmr" AND $upload_Mime_Type != "image/vnd.fujixerox.edmics-rlc" AND $upload_Mime_Type != "image/vnd.mix" AND $upload_Mime_Type != "image/vnd.net-fpx" AND $upload_Mime_Type != "image/vnd.svf" AND $upload_Mime_Type != "image/vnd.wap.wbmp" AND $upload_Mime_Type != "image/vnd.xiff" )
{
unlink($upload_Temp);
die("<p align='center'><b><font face='Arial Black' size='2' color='#FF0000'>ERROR<br>YOUR FILE WAS NOT UPLOADED<br>PLEASE CHECK THE FILE SIZE AND FORMAT</font></b></p>");
}
$uploadFile = "uploads/".$upload_Name ;
if (!is_dir(dirname($uploadFile)))
  {
    @RecursiveMkdir(dirname($uploadFile)); 
  }
else
  {
  @chmod(dirname($uploadFile), 0777);
  }
@move_uploaded_file( $upload_Temp , $uploadFile); 
chmod($uploadFile, 0644);

//CHANGE THIS TO THE YOUR DOMAIN
$upload_URL = "http://www.journalismpakistan.com/uploads/".$upload_Name ;
//------------

$pfw_header = "From: $email";
$pfw_subject = "AN IMAGE HAS BEEN UPLOADED";

// CHANGE THIS TO YOUR EMAIL ADDRESS
$pfw_email_to = "chimi@journalismpakistan.com";
//------------
$pfw_message = "email: $email\n"

. "Name: $Name\n"
. "city: $city\n"
. "state: $state\n"
. "upload: $upload_URL\n";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;

 echo("<p align='center'><b><font face='Arial Black' size='2' color='#0000FF'>THANK YOU!<br>YOUR IMAGE HAS BEEN UPLOADED</font></b></p>");
include("footer.html");

--- End ---

tjblair’s picture

This is what I'm looking for. However after I copy the text into the Body and save I get the following message instead of the new contact form.

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'contactform' was given in /home/vancouw2/public_html/drupal/includes/form.inc on line 218.

Does anyone know what I'm missing?
I'm using Drupal 5.1

grippat’s picture

I get the same error when I try to use the code on Drupal 5.1 as well. I'm not sure what's causing it though.

aufumy’s picture

I used creazion's updated example for version 5, and it works fine.

http://drupal.org/node/68265#comment-212470

bharanikumariyerphp’s picture

hi dear i design the form ..
i savedthat form in the theme folder tell me..

now i want to display that form in the user page...
with menu name:: student register and its forms must display..
thanks in advance

zhufrk’s picture

Hi, I plugged the code in a page, and get this error when saving. Any idea how to fix it is appreciated.
=====================
warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'contactform' was given in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\pedb_drupal\includes\form.inc on line 358.
=====================

MakeOnlineShop’s picture

Hello,

Do you know what is the best solution now to allow registered users to upload pictures throught the contact form ?

Thank you.

MakeOnlineShop’s picture

Hi,

I just found this module and will try it now:

https://drupal.org/project/contact_attach

Thanks.

yael2304’s picture

Is it possible to add an attachment to a form and have it only show addresses that match our address data list? I created a form and added the address filed based on the data we have uploaded, but now I need the attachment to only show the ones the address are in our system