Jump to content

multipart emiail forms


bcarlson

Recommended Posts

Hi All,

 

I am new to the boards and I've been working on a form (which is rather massive, imo) and i've been looking online at other peoples php examples for multipart emails.  The code i am using / editing (trying to learn and understand it) is one of the web examples I found..... I have some questions:

 

1) onSubmit my action attribute is calling <someReuest.php>, is this a good idea?  I assume yes.

 

<form id="1" enctype="multipart/form-data" name="siteRequest" onSubmit="return validate()" method="post" action="someReuest.php">

 

2) in my php file i'm creating a new variable for each form field (before beinging to process anything)

  e.g.

  $headerimage = $_POST['headerimage']; 

where header image is the name of one of my form fields.  Is this a good practice or is this an unnecessary step?

 

3) I have 10 form fields of type=file thus do I need to loop over my mail attachment function?  see below.  Again, this function was found on the internet which i'm trying to edit, work with, learn, etc..

 

 

I know using someone else code isn't a good idea, but i'm trying to learn, and multipart isn't the easiest thing for a newb to learn.

 

function mail_attachment ($from , $to, $subject, $message, $attachment) {
	//posting attachment to mail

	$fileatt = $attachment; //path to file
	$fileatt_type = "image/jpeg"; //filetype
	$start = strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;  
	$fileatt_name = substr($attachment, $start, strlen($attachment)); //file name used for attachment


	$email_from = $from; //who e-mail is from
	$email_subject = $subject; //subject of e-mail
	$email_txt = $message; //message that the emal has in it
	$email_to = $to; //who the email is to

	$headers = "From: ".$email_from;

	if(!($file = fopen($fileatt,'rb'))) echo "Failed To Open File $fileatt"; //read binary bytes
	$data = fread($file,filesize($fileatt));
	fclose($file);

	$msg_txt="\n\n There is an attachment with this request, please review. The message came from: $from";
	$semi_rand = md5(time());
	$mime_boundary = "==Multipart_Boundary_x($semi_rand)x";
	$headers .= "\nMIME-Version: 1.0\n" .
				"Content-Type: multipart/mixed;\n" .
				" boundary=\"{$mime_boundary}\"";
	$email_txt .= $msg_txt;
	$email_message .= "This is a multipart message in MIME format.\n\n" .
			"--{$mime_boundary}\n" .
			"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
			"Content-Transfer-Encoding: 7bit\n\n" .
	$email_txt . "\n\n";
	$data = chunk_split(base64_encode($data));
	$email_message .= "--{$mime_boundary}\n" .
		  "Content-Type: {$fileatt_type};\n" .
		  " name=\"{$fileatt_name}\"\n" .
		  //"Content-Disposition: attachment;\n" .
		  //" filename=\"{$fileatt_name}\"\n" .
		  "Content-Transfer-Encoding: base64\n\n" .
		 $data . "\n\n" .
		  "--{$mime_boundary}--\n";


	$ok = mail($email_to, $email_subject, $email_message, $headers);


	if($ok)
	{
	echo "File Sent Successfully.";
	unlink($attachment); //delete file off server after its attached
	}
	else
	{
		die("Sorry the email could not be sent.");
	}
}

 

This is a good start I know there will be more, but i'll try to use the helpful suggestions for these first 3 questions first. 

 

Sorry if there isn't enough code here, let me know if more needs to be added.

Link to comment
https://forums.phpfreaks.com/topic/187965-multipart-emiail-forms/
Share on other sites

So after digging deeper and doing some reading I have answered/solved my own problems:

 

1) Yes, the action attribute in the form with the post method should be calling a php file.

 

2)Yes, If I want to include all those fields in the message i'll need to declare them then add them to a placeholder variable like $message.

 

3) for each form field of type=file you need to grab the file attributes out of $_FILES like so..

 

/* header upload */
$fileatt = ""; //path to file
$fileatt_type = "application/octet-stream"; //file type
$fileatt_name = ""; //file name used for attachment

so in my case I have 6 file upload fields, so i did something similar to this for each field.

 

Regarding the mail_function, I have since scrapped that, and made 6 individual attach sections like so:

 

	/*attaching the header file*/
$fileatt = ""; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type, this changes based on the file, maybe a switch?
$fileatt_name = ""; // Filename that will be used for the file as the attachment

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);


$data = chunk_split(base64_encode($data));  //encode data using base64 encoding

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);

 

obviously the type will change based on images, documents, etc.. but you get the idea.

 

 

Thus far it appears things are working the way I need them. 

 

I hope this will help someone else!!

 

8) 8) 8) 8)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.