Jump to content

[SOLVED] Attach Form


ballouta

Recommended Posts

Hello

I searched on google for a contact form with an attachment option, i tried many scripts but i got error on many of them, some are restricted with a specific extension.

 

Would you please provide a simple contact form + PHP script sample link?

 

Many thanks

Link to comment
Share on other sites

Actually i deleted them all,

 

still have this one which requires two files:

  include_once('Mail.php');

  include_once('mime.php');

 

and i they were not attached with the script.

 

I prefer a simple contact form with one attachment field

 

thank you

 

<?php 
$max_allowed_file_size = "100"; // this is size in KB 
list($name_of_uploaded_file, $type_of_uploaded_file, $size_of_uploaded_file) = GetUploadedFileInfo();
if(!Validate($name_of_uploaded_file, $type_of_uploaded_file, $size_of_uploaded_file, $max_allowed_file_size)) {
   exit();
}
LoadUploadedFile($name_of_uploaded_file);
$path_of_uploaded_file = "uploads/" . $name_of_uploaded_file;

   include_once('Mail.php');
   include_once('mime.php');

ComposeMail($path_of_uploaded_file);


//////////////////// Functions ////////////////////////

function GetUploadedFileInfo() {
   $file_info[] = basename($_FILES['uploaded_file']['name']);
   $file_info[] = substr($file_info[0], strrpos($file_info[0], '.') + 1);
   $file_info[] = $_FILES["uploaded_file"]["size"]/1024;
   return $file_info;
}


function Validate($name_of_uploaded_file, $type_of_uploaded_file, $size_of_uploaded_file, $max_allowed_file_size) {
   if($size_of_uploaded_file>$max_allowed_file_size ) {
       echo "Size of file is greater than" . $max_allowed_file_size . " KB. <a href='attachment_email_form.html'>Click Here to upload a smaller sized file.</a>";
      return false;
   }
$allowed_extension = array("jpg", "jpeg", "gif", "bmp");
for($i=0; $i<sizeof($allowed_extension); $i++) { 
   $allowed_extension[$i] = strtoupper($allowed_extension[$i]); 
}
$type_of_uploaded_file = strtoupper($type_of_uploaded_file);
if(!(in_array(strtoupper($type_of_uploaded_file),$allowed_extension))) {
   echo "You have uploaded a file with an extension of " . $type_of_uploaded_file . " . This type is not allowed. Please upload a file with allowed image extensions like jpg, jpeg, bmp, gif. <a href='attachment_email_form.html'>Click Here to upload a file with allowed extension.</a>";
   return false;
}
   return true;
}


function LoadUploadedFile($name_of_uploaded_file) {
   move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "uploads/" . $name_of_uploaded_file);
   return true;
}


function ComposeMail($name_of_uploaded_file) {
   $name = $_POST['name'];
   $user_message = $_POST['message'];
   $to = "RecipietnEmail@hotmail.com";
   $subject="An email with attachement is sent";
   $from = "SenderEmail@hotmail.com";
   $text = "A user " . $name . "has sent you this message and an attachment: " . $user_message;
   $message = new Mail_mime(); 
   $message->setTXTBody($text); 
   $message->addAttachment($name_of_uploaded_file);
   $body = $message->get();
   $extraheaders = array("From"=>$from, "Subject"=>$subject);
   $headers = $message->headers($extraheaders);
   $mail = Mail::factory("mail");
   $mail->send($to, $headers, $body);
   echo "Your Email with attachment was sent.";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.