Email attachments

Ask about a PHP problem here.
Post Reply
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Email attachments

Post 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
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Email attachments

Post by ExtremeGaming »

Other than there being no variables called $filename and $fileContent?
<?php while(!$succeed = try()); ?>
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Email attachments

Post 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.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Email attachments

Post by Helx »

Why are you using pastebin? You can put the code in the code tags :P
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Email attachments

Post by Fidbeck »

I used pastebin for another forum and just copied and pasted :P
I haven't figured it out :S
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Email attachments

Post by Helx »

I would just upload the file to the server and put a link in the email somewhere.
Much easier :P
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Email attachments

Post by Fidbeck »

easy != fun :P but it does equals no headaches xD
I want to figure how to do this that way
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Email attachments

Post by Fidbeck »

I just found out like 2 minutes ago that Jacek has a How ro send email attachments tutorial :S
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Email attachments

Post 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
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Email attachments

Post by Fidbeck »

Thanks everybody but I already have the problem solved :)
Post Reply