html — электронная почта PHP с ATTACHMENT — fopen () ожидает, что параметр 1 будет правильным путем

Мне очень трудно приложить любой файл и отправить форму PHP по электронной почте.

Я пытаюсь добиться простой функции прикрепления файла, которая отправляет вложение на указанный адрес электронной почты. Я не хочу использовать PHPmailer, поскольку нахожу его действительно сложным для такой маленькой функции.

Я получил форму отсюда — http://forums.phpfreaks.com/topic/280373-uploading-file-and-sending-as-attachment-in-e-mail/

Пожалуйста помоги!

Я получаю эти ошибки:

**Warning: fopen() expects parameter 1 to be a valid path, resource given in /homepages/34/d565332578/htdocs/email-test/send2.php on line 36
Warning: filesize() expects parameter 1 to be a valid path, resource given in /homepages/34/d565332578/htdocs/email-test/send2.php on line 37
Warning: fread() expects parameter 1 to be resource, boolean given in /homepages/34/d565332578/htdocs/email-test/send2.php on line 37
Warning: fclose() expects parameter 1 to be resource, boolean given in /homepages/34/d565332578/htdocs/email-test/send2.php on line 38**

HTML:

<!DOCTYPE html>
<html>
<body>

<form name="contactform" method="post" action="send2.php" enctype="multipart/form-data">
<table width="100%" border="0" align="center">
<tr>
<td valign="middle" id="ta">
<label for="title">Title *</label>
</td>
<td valign="middle" id="ta">
<select name="title">
<option value="0">Title</option>
<option value="1">Mr.</option>
<option value="2">Ms.</option>
<option value="3">Mrs.</option>
</select></td></tr><tr><td id="ta">
<label for="first_name">First Name *</label>
</td>
<td valign="middle" id="ta">
<input  type="text" name="first_name" maxlength="50" size="30" required="required">
</td>
</tr>
<tr>
<td valign="middle" id="ta">
<label for="last_name">Last Name *</label>
</td>
<td valign="middle" id="ta">
<input  type="text" name="last_name" maxlength="50" size="30" required="required">
</td>
</tr>
<tr>
<td valign="middle" id="ta">
<label for="email">Email Address *</label>
</td>
<td valign="middle" id="ta">
<input  type="text" name="email" maxlength="80" size="30" required="required">
</td>
</tr>
<tr>
<td valign="middle" id="ta">
<label for="telephone">Telephone Number *</label>
</td>
<td valign="middle" id="ta">
<input  type="text" name="telephone" maxlength="30" size="30" required="required">
</td>
</tr>
<tr>
<td valign="middle" id="ta">
<label for="comments">Details</label>
</td>
<td valign="middle" id="ta">
<textarea  name="comments" maxlength="100000" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td valign="middle" id="ta">
<label for="file">Or upload a file (only word, excel or pdf)</label>
</td>
<td valign="middle" id="ta">
<input type="file" name="file">
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center" id="ta">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>

</body>
</html>

PHP-код:

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$title = array('Title', 'Mr.', 'Ms.', 'Mrs.');
$selected_key = $_POST['title'];
$selected_val = $title[$_POST['title']];

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required

if(($selected_key==0))
echo "<script> alert('Please enter your title')</script>";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message = "";
$email_message .="Title: ".$selected_val."\n";
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

$email_to = "bhavesh.k.2007@gmail.com"; // The email you are sending to (example)
//$email_from = "sendfrom@email.com"; // The email you are sending from (example)
$email_subject = "subject line"; // The Subject of the email
$email_txt = "text body of message"; // Message that the email has in it
$destination=$_FILES["file"]["name"];/**here you open the file, the $fileatt variable become a resource**/
$fileatt = fopen($_FILES["file"]["tmp_name"],'r'); // Path to the file (example)
move_uploaded_file($_FILES["file"]["name"], $destination);

$fileatt_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; // File Type
$fileatt_name = "Details.docx"; // Filename that will be used for the file as the attachment

/**here you are just read the $fileatt opened**/
$data = fread($fileatt ,filesize($fileatt));

fclose($file);

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers="From: $email_from"; // Who the email is from (example)
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part 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;
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

mail($email_to,$email_subject,$email_message,$headers);
?>

0

Решение

Ошибка здесь:

/**
here you open the file, the $fileatt variable become a resource
**/
$fileatt = fopen($_FILES["file"]["name"],'r'); // Path to the file (example)

$fileatt_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; // File Type
$fileatt_name = "Details.docx"; // Filename that will be used for the file as the attachment

/**
here you are trying open a resource but not a file,
cuz $fileatt is a resource from $_FILES['file']['name']
**/
$file = fopen($fileatt,'r');
$data = fread($file,filesize($fileatt));
fclose($file);
1

Другие решения

 move_uploaded_file($_FILES["file"]["name"], $destination);
$fileatt = fopen($_FILES["file"]["name"],'r');

должно быть что:

move_uploaded_file($_FILES["file"]["tmp_name"], $destination);
$fileatt = fopen($destination,'r');

потому что $ _FILES [«file»] [«name»] — это просто «базовое имя» файла
вам нужен загруженный файл (в вашем / tmp)

0

попробуй это:

$fileatt = fopen($_FILES['file']['tmp_name'], 'r');

потому что это оригинальный файл во временном каталоге

0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector