Zo4inec
1/24/2018 - 1:57 PM

Обработчик формы с вложением

<?php

if ( isset( $_POST['url'] ) && $_POST['url'] == '' ) {
	if ( isset( $_POST['form'] ) ) {

		function test_input( $data ) {
			$data = trim( $data );
			$data = stripslashes( $data );
			$data = htmlspecialchars( $data );

			return $data;
		}

		$project_name = "Name";
		$admin_email  = "Email";
		$subject = "Subject";
		$email        = test_input( $_POST['email'] );
		$address      = test_input( $_POST['address'] );
		$title        = test_input( $_POST['title'] );
		$form         = test_input( $_POST['form'] );
		$bedrooms     = test_input( $_POST['bedrooms'] );
		$attachments = $_FILES['files'];

//		$my_file = "";
//		if (!empty($_FILES['file']['tmp_name']))
//		{
//			$path = $_FILES['file']['name'];
//			if (copy($_FILES['file']['tmp_name'], $path)) $my_file = $path;
//		}

		$message = "		
<tr style='background-color: #f8f8f8;'>
	<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>Форма:</b></td>
	<td style='padding: 10px; border: #e9e9e9 1px solid;'>$title</td>
</tr>
<tr>
	<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>Е-mail:</b></td>
	<td style='padding: 10px; border: #e9e9e9 1px solid;'>$email</td>
</tr>
<tr style='background-color: #f8f8f8;'>
	<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>Адрес:</b></td>
	<td style='padding: 10px; border: #e9e9e9 1px solid;'>$address</td>
</tr>
<tr>
	<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>Количество спален:</b></td>
	<td style='padding: 10px; border: #e9e9e9 1px solid;'>$bedrooms</td>
</tr>
	";
		$message = "<table style='width: 100%;'>$message</table>";

		function adopt( $text ) {
			return '=?UTF-8?B?' . base64_encode( $text ) . '?=';
		}

		if ( $form == 'form-main' ) {
			$headers = "MIME-Version: 1.0" . PHP_EOL .
			           "Content-Type: text/html; charset=utf-8" . PHP_EOL .
			           'From: ' . adopt( $project_name ) . ' <' . $admin_email . '>' . PHP_EOL .
			           'Reply-To: ' . $admin_email . '' . PHP_EOL;
			mail( $admin_email, adopt( $subject ), $message, $headers );

		} else if ( $form == 'form-calc' ) {

			$file_count = count($attachments['name']); //count total files attached
			$boundary = md5(uniqid(time()));


			//header
			$headers = "MIME-Version: 1.0\r\n";
			$headers .= "From:". adopt( $project_name ) . ' <' . $admin_email . '>' . "\r\n";
			$headers .= "Reply-To: ".$admin_email."" . "\r\n";
			$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

			//message text
			$body = "--$boundary\r\n";
			$body .= "Content-Type: text/html; charset=utf-8\r\n";
			$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
			$body .= chunk_split(base64_encode($message));

			//attachments
			for ($x = 0; $x < $file_count; $x++){
				if(!empty($attachments['name'][$x])){

					//get file info
					$file_name = $attachments['name'][$x];
					$file_size = $attachments['size'][$x];
					$file_type = $attachments['type'][$x];

					//read file
					$handle = fopen($attachments['tmp_name'][$x], "r");
					$content = fread($handle, $file_size);
					fclose($handle);
					$encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)

					$body .= "--$boundary\r\n";
					$body .="Content-Type: $file_type; name=".$file_name."\r\n";
					$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
					$body .="Content-Transfer-Encoding: base64\r\n";
					$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
					$body .= $encoded_content;
				}
			}
			mail( $admin_email, adopt( $subject ), $body, $headers);
		}
	}
}

?>