Jump to content

Recommended Posts

I have a contact form with multiple attachings using a single input type file. (It uses multiples, but they disappear and a new one gets made)

A test of it can be seen here http://www.developers-community.com/support.php

Just use the browse option.

 

I'm wondering how to send it with attachments coming up shown in the email when recieved on the other end.

This is my form. (Echo'd)

echo "<table width=\"36%\" border=\"0\">
    <form action=\"\" method=\"post\" onSubmit=\"rememberinput('forename', 'surname', 'email', 'subject', 'message')\">    
    <tr>
      <td>
  <div id=\"files_list\"></div>
  <div style=\"margin-right:250px;\"><span style=\"color: #F00\">*</span>Your Full Name ".$error['name']."<br />
  <input type=\"text\" name=\"forename\" id=\"name\" value=\"forename\"
      onfocus=\"if(this.value=='forename'){this.value='';}\" 
      onblur=\"if(this.value==''){this.value='forename';}\" class=\"textboxsup\" size=\"22\"/> 
      <input type=\"text\" name=\"surname\" id=\"surname\" value=\"surname\"
      onfocus=\"if(this.value=='surname'){this.value='';}\" 
      onblur=\"if(this.value==''){this.value='surname';}\" class=\"textboxsup\" size=\"22\"/><br />
      
      <span style=\"color: #F00\">*</span>Your Email Address ".$error['email']."<br />
      <input name=\"email\" type=\"text\" id=\"email\" value=\"".$_POST['email']."\" size=\"50\" class=\"textboxsup\"/><br />

  <span style=\"color: #F00\">*</span>Subject ".$error['subject']."<br />
      <input name=\"subject\" type=\"text\" id=\"subject\" value=\"".$_POST['subject']."\" size=\"50\" class=\"textboxsup\"/><br />
  
  Add An Attachment (optional)<br />
  <input id=\"my_file_element\" type=\"file\" name=\"file_1\" size=\"45\" class=\"textboxsup\"></div>
  <script>
  var multi_selector = new MultiSelector(document.getElementById('files_list'), 6);
  multi_selector.addElement(document.getElementById('my_file_element'));
  </script>
  </td></tr>

I have the file element, with a div which is floated right which displays the files.

 

Here is the javascript function behind it.

function MultiSelector( list_target, max ){
this.list_target = list_target;
this.count = 0;
this.id = 0;
if( max ){
	this.max = max;
} else {
	this.max = -1;
};

this.addElement = function( element ){
	if( element.tagName == 'INPUT' && element.type == 'file' ){
		element.name = 'file_' + this.id++;
		element.multi_selector = this;
		element.onchange = function(){

			var new_element = document.createElement( 'input' );
			new_element.type = 'file';
			new_element.size = '45';
			new_element.setAttribute('style', 'font:100% Verdana, Arial, Helvetica, sans-serif;');

			this.parentNode.insertBefore( new_element, this );
			this.multi_selector.addElement( new_element );
			this.multi_selector.addListRow(this);

			this.style.position = 'absolute';
			this.style.display = 'none';
		};
		if( this.max != -1 && this.count >= this.max ){
			element.disabled = true;
		};

		this.count++;
		this.current_element = element;	

	} else {
		alert( 'Error: not a file input element' );
	};
};

this.addListRow = function( element ){

	var new_row = document.createElement( 'div' );
	new_row.element = element;
	new_row.setAttribute('style', 'padding:2px 10px 3px 10px; margin-bottom:1px; background:#ffc; font:100% Verdana, Arial, Helvetica, sans-serif;');

	var deletelink = document.createElement('a');
	var t = document.createTextNode("X");
	deletelink.setAttribute('href', '#');
	deletelink.setAttribute('style', 'float:right;');
	deletelink.onclick= function(){

		this.parentNode.element.parentNode.removeChild( this.parentNode.element );
		this.parentNode.parentNode.removeChild( this.parentNode );
		this.parentNode.element.multi_selector.count--;
		this.parentNode.element.multi_selector.current_element.disabled = false;

		return false;
	};

	var reduced;
	if (element.value.length > 25) {
		reduced = element.value.substr(0,25)+'...';
	} else {
		reduced = element.value;
	}

	new_row.innerHTML = reduced;

	deletelink.appendChild(t);
	new_row.appendChild(deletelink);

	this.list_target.appendChild( new_row );
};
};

The input bit to look at in the javascript file is

this.addElement = function( element ){
	if( element.tagName == 'INPUT' && element.type == 'file' ){
		element.name = 'file_' + this.id++;
		element.multi_selector = this;
		element.onchange = function(){

			var new_element = document.createElement( 'input' );
			new_element.type = 'file';
			new_element.size = '45';
			new_element.setAttribute('style', 'font:100% Verdana, Arial, Helvetica, sans-serif;');

			this.parentNode.insertBefore( new_element, this );
			this.multi_selector.addElement( new_element );
			this.multi_selector.addListRow(this);

			this.style.position = 'absolute';
			this.style.display = 'none';

This creates a new file input field, and deletes the hides the old one.

 

So far, the email sender without attachments is this

$to = "admin@developers-community.com";
$name = "$forename $surname - Guest"; 
$body = "$message\n\n $name";
$header = "from: $name <$email>"; 
mail($to, $subject, $body, $header);

$to = $email;
$name = "$forename $surname"; 
$email = "no-reply@developers-community.com";
$body = "Dear $name,\nThank you for your mail to us. Please wait patiently while we review it and get back to you. \nMeanwhile, You can use any of the other features within http://www.developers-community.com \n\n
Your's Truly\nAdmin\nDeveloper's Community\n\nIn any case, do not reply to this email!";
$header = "from: Developers Community - Admin <$email>";
mail($to, $subject, $body, $header);

echo '<span style="color:red";>Your mail has been sent.<br />
You will recieve a reply within up to 48 hours. This may take longer on busy days.<br />
You should have recieved an email stating we have recieved your mail, this may be in your Junk Folder.</span>';

 

Can anyone point me in the right right direction to sending the attachments?

Thanks.

http://www.geekology.co.za/blog/2009/06/sending-emails-with-attachments-using-php-mail-function/

I looked at this, but this is sending attachments with:

$attachment = chunk_split(base64_encode(file_get_contents("geekology.zip")));

 

Though I'm not sure what that'd be for my php, as it uses multiple attachments, with multiple file fields, along with the ID's of each one.

I found this: $files = array("file_1.ext","file_2.ext","file_3.ext",......);

On http://www.emanueleferonato.com/2008/07/22/sending-email-with-multiple-attachments-with-php/

What it'd be.. is getting $files from the File inputs id's (6 id's in total)

 

I'm not sure on the ID's of them.. or what to do.

I'm not bumping, I'm just posting updates to how far I've got. I actually have something now.

What i've got, is a full script, I have a maximum of 6 fields. The javascript for the form is shown in the first post.

When I send a mail, it sends perfectly fine. Though it doesn't get an attachment at all.

This is what i've got.

	echo'<center>';
if(isset($_POST['submit'])){
$forename = $_POST['forename'];
$surname = $_POST['surname'];
$regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{1,})*\.([a-z]{2,}){1}$"; 
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$user_code = $_POST['captcha'];

if(!$forename || !$surname || $forename == 'forenames' || $surname == 'surname'){
	$error['name'] = '- <span style="color:red;">Required!</span>';
}

if(!$email || !eregi($regex,($email))){
	$error['email'] = '- <span style="color:red;">Required!</span>';
}

if(!$subject || strlen($subject = trim($subject)) == 0){
	$error['subject'] = '- <span style="color:red;">Required!</span>';
}

if(!$message || strlen($message = trim($message)) == 0){
$error['message'] = '- <span style="color:red;">Required!</span>';
    }
elseif(strlen($message) < 10){
	$error['message'] = '- <span style="color:red;">Below 10 characters!</span>';
}

if (!$user_code){
$error['captcha'] = '- <span style="color:red;">Required!</span>';
}
elseif ($user_code != $_SESSION['ckey']){
$error['captcha'] = '- <span style="color:red;">Incorrect try again!</span>';
}

if(!isset($error)){

$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$to = "admin@developers-community.com";
$name = "$forename $surname - Guest"; 
$from = stripslashes($name)."<".stripslashes($email).">";

$header = "From: $from\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\"";
$body = "This is a multi-part message in MIME format.\n\n" .
      "--{$mime_boundary}\n" .
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n$message\n\n $name\n\n";

   foreach($_FILES as $userfile){
      $tmp_name = $userfile['tmp_name'];
      $type = $userfile['type'];
      $name = $userfile['name'];
      $size = $userfile['size'];

      if (file_exists($tmp_name)){
         if(is_uploaded_file($tmp_name)){
            $file = fopen($tmp_name,'rb');
            $data = fread($file,filesize($tmp_name));
            fclose($file);
     
            $data = chunk_split(base64_encode($data));
         }

   $body .= "--{$mime_boundary}\n" . "Content-Type: {$type};\n" . " name=\"{$name}\"\n" . "Content-Disposition: attachment;\n" .
            " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
      }
   }
   $body .="--{$mime_boundary}--\n";
  
   mail($to, $subject, $body, $header);

$to = $email;
$name = "$forename $surname"; 
$email = "no-reply@developers-community.com";
$body = "Dear $name,\nThank you for your mail to us. Please wait patiently while we review it and get back to you. \nMeanwhile, You can use any of the other features within http://www.developers-community.com \n\n
Your's Truly\nAdmin\nDeveloper's Community\n\nIn any case, do not reply to this email!";
$header = "from: Developers Community - Admin <$email>";
mail($to, $subject, $body, $header);

echo '<span style="color:red";>Your mail has been sent.<br />
You will recieve a reply within up to 48 hours. This may take longer on busy days.<br />
You should have recieved an email stating we have recieved your mail, this may be in your Junk Folder.</span>';
   }
}
echo "<table width=\"36%\" border=\"0\">
    <form action=\"\" method=\"post\" onSubmit=\"rememberinput('forename', 'surname', 'email', 'subject', 'message')\">    
    <tr>
      <td>
  <div id=\"files_list\"></div>
  <div style=\"margin-right:250px;\"><span style=\"color: #F00\">*</span>Your Full Name ".$error['name']."<br />
  <input type=\"text\" name=\"forename\" id=\"name\" value=\"forename\"
      onfocus=\"if(this.value=='forename'){this.value='';}\" 
      onblur=\"if(this.value==''){this.value='forename';}\" class=\"textboxsup\" size=\"22\"/> 
      <input type=\"text\" name=\"surname\" id=\"surname\" value=\"surname\"
      onfocus=\"if(this.value=='surname'){this.value='';}\" 
      onblur=\"if(this.value==''){this.value='surname';}\" class=\"textboxsup\" size=\"22\"/><br />
      
      <span style=\"color: #F00\">*</span>Your Email Address ".$error['email']."<br />
      <input name=\"email\" type=\"text\" id=\"email\" value=\"".$_POST['email']."\" size=\"50\" class=\"textboxsup\"/><br />

  <span style=\"color: #F00\">*</span>Subject ".$error['subject']."<br />
      <input name=\"subject\" type=\"text\" id=\"subject\" value=\"".$_POST['subject']."\" size=\"50\" class=\"textboxsup\"/><br />
  
  Add An Attachment (optional)<br />
  <input id=\"my_file_element\" type=\"file\" name=\"file_1\" size=\"45\" class=\"textboxsup\"></div>
  <script>
  var multi_selector = new MultiSelector(document.getElementById('files_list'), 6);
  multi_selector.addElement(document.getElementById('my_file_element'));
  </script>
  </td></tr>
  
  <tr><td><span style=\"color: #F00\">*</span>Message ".$error['message']."</td></tr>
      <tr>
        <td><textarea name=\"message\" cols=\"90\" rows=\"12\" class=\"textboxsup\" id=\"message\"
       value=\"".$_POST['message']."\"></textarea></td>
      </tr>
      <tr>
        <td><span style=\"color: #F00\">*</span>Verfication<span style=\"width:149px;\"> (Are you human?) ".$error['captcha']."</span></td>
      </tr>
      <tr>
        <td>
        <div style=\"width:160px; float:right; margin-right:265px; margin-top:6px;\"><span style=\"width:149px;\">The answer is:
            <input name=\"captcha\" class=\"textboxsup\" id=\"captcha\" size=\"5\" maxlength=\"2\" />
        </span></div>
        <div style=\"width:149px; margin-right:300px;\">
	<span style=\"text-align: center\">
	<img src=\"inc/pngimg.php\" alt=\"\" width=\"148\" height=\"30\" style=\"border:1px solid #448DC4;\" name=\"captcha\"/>
	</span>
	<span style=\"text-align: center\">
	<a href=\"#\" onclick=\"Start();\">Refresh</a> <strong>/</strong> <a href=\"#\"
	onmouseover=\"document.getElementById('tip').style.display='';\" 
	onmouseout=\"document.getElementById('tip').style.display='none';\">Whats this?</a></span><br />
        </div>
        <span id=\"tip\" style=\"margin-left:120px; margin-top:3px; display:none\">
      A captcha is to check if you are human or not. It's required that you fill it in!
      </span></td>
      </tr>
      <tr>
        <td><input name=\"submit\" type=\"submit\" value=\"submit\"/></td>
      </tr>
    </form>
    </table>";
 echo'<center>'; } ?>

 

Someone please help me :(

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.