Page 1 of 1

Email attachments

Posted: Sun Jan 06, 2013 10:45 pm
by Fidbeck
I have a simple contact form that allows me to send attachments but I'm having trouble sending the right file
Can anyone help me out?

This is the code
http://pastebin.com/qtGyJdjD

Re: Email attachments

Posted: Mon Jan 07, 2013 12:09 am
by ExtremeGaming
Other than there being no variables called $filename and $fileContent?

Re: Email attachments

Posted: Mon Jan 07, 2013 12:10 am
by Fidbeck
I'm nearly done with this but curiously this is putting my body(message) inside the file that i'm uploading (in this case an image) thus making it inaccessible.

http://pastebin.com/AkbLcMsi

@ExtremeGaming i was writting my message whe you replied.

Re: Email attachments

Posted: Mon Jan 07, 2013 12:35 am
by Helx
Why are you using pastebin? You can put the code in the code tags :P

Re: Email attachments

Posted: Mon Jan 07, 2013 12:45 am
by Fidbeck
I used pastebin for another forum and just copied and pasted :P
I haven't figured it out :S

Re: Email attachments

Posted: Mon Jan 07, 2013 12:54 am
by Helx
I would just upload the file to the server and put a link in the email somewhere.
Much easier :P

Re: Email attachments

Posted: Mon Jan 07, 2013 1:01 am
by Fidbeck
easy != fun :P but it does equals no headaches xD
I want to figure how to do this that way

Re: Email attachments

Posted: Mon Jan 07, 2013 10:39 pm
by Fidbeck
I just found out like 2 minutes ago that Jacek has a How ro send email attachments tutorial :S

Re: Email attachments

Posted: Tue Jan 08, 2013 12:26 am
by Fidbeck
I've applied Jacek's tutorial to what I have and this is the final result
<?php
 
if(empty($_POST) === false){
	$errors = array();
   
	$name = $_POST['name'];
	$email = $_POST['email'];
	$message = $_POST['message'];
	$file = $_FILES['filename'];
   
	if(empty($name) === true || empty($email) === true || empty($message) === true){
		$errors[] = 'Name, email and message are required!';
	} else {
		if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
			$errors[] ='Not a valid email';
		}
		if(ctype_alpha($name) === false){
			$errors[] ='Name must only contain letters';
		}
	}

	if(empty($errors) === true){ 
		$boundary = md5(rand());

		$headers = array(
			'MIME-Version: 1.0',
			"Content-type: multipart/mixed; boundary=\"{$boundary}\"",
			"From: {$email}"
		);
		$message = array(
			"--{$boundary}",
			'Content-type: text/html',
			'Content-Transfer-Encoding: 7bit',
			'',
			chunk_split($message),
			"--{$boundary}",
			"Content-type: {$file['type']}; name=\"{$file['filename']}\"",
			"Content-Disposition: attachment; filename=\"{$file['filename']}\"",
			'Content-Transfer-Encoding: base64',
			'',
			chunk_split(base64_encode(file_get_contents($file['tmp_name']))),
			"--{$boundary}--"
		);

		//send email
		mail($email, 'Contact form', implode("\r\n", $message), implode("\r\n", $headers));

		//redirect user
		header('Location: index.php?sent');
		exit();
	}    
}
 
?>
but i'm having a problem. I need to get the name of the file and the extension to send the to the user.
If I send a jpg or a pdf file they send fine and If I send a php file for instance it doesn't show a thing. It simply shows a "noname" file.
What I need to do next, after fixing this, is to make a little verification for file types and do a size limit verification.
I also need to find a way to if the user doesn't want to send any attachment it just sends the mail without any problem. Now its gving me a Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in /home/a3876128/public_html/testcontact.php on line 41

Re: Email attachments

Posted: Tue Jan 08, 2013 11:12 pm
by Fidbeck
Thanks everybody but I already have the problem solved :)