PDA

Pogčedajte punu verziju : Slanje fajlova putem formulara na mail?


R. S.
24. 06. 2008., 04:38
Zamoliću za pomoć. Problem je sledeći: putem formulara sa HTML stranice treba proslediti i fajl - .doc ili .pdf na odredjenu e-mail adresu (PHP skript koji se poziva i prosledjuje vrednosti radi, ali ne i deo za slanje fajlova)... :lost:

Obzirom da sam početnik i da danima ne nalazim rešenje (koje naravno hitno treba) još jednom apelujem na neku dobru dušu da mi pomogne :please:

Kod je sledeći:

<?
include 'include/connect.db.inc.php';


$fname = $_POST['name_first'];
$lname = $_POST['name_last'];
$ad = $_POST['address'];
$email = $_POST['mail'];


$to = "my.gml.rs@gmail.com";
$subject = "Application: ".$fname." ".$lname;

$content .= "<b><u>APPLICATION</u></b><br /><br />";
$content .= "<b>First Name:</b> ".$fname." <br />";
$content .= "<b>Last Name:</b> ".$lname." <br />";
$content .= "<b>Address:</b> ".$ad." <br />";
$content .= "<b>E-mail:</b> ".$email." <br /><br />";


// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

// store the file information to variables for easier access
$tmp_name = $_FILES['filename']['tmp_name'];
$type = $_FILES['filename']['type'];
$name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];

// here we'll hard code a text message
// again, in reality, you'll normally get this from the form submission

// if the upload succeded, the file will exist
if ((file_exists($tmp_name) && $size < 5000000 ) || $$tmp_name==""){

// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){

// open the file for a binary read
$file = fopen($tmp_name,'rb');

// read the file content into a variable
$data = fread($file,filesize($tmp_name));

// close the file
fclose($file);

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));

}}



// now we'll build the message headers
$header = "From: Web Formular \r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";

// next, we'll build the message body
// note that we insert two dashes in front of the
// MIME boundary when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"utf-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content and set another boundary to
// indicate that the end of the file has been reached
$message .= "Content-Type: ".$type."\r\n" .
" name=\"".$name."\"\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-Disposition: attachment;\r\n" .
" filename=\"".$name."\"\r\n\r\n" .
$data."\r\n" .
"--".$mime_boundary."\r\n";



mail($to, $subject, $content, $message, $header);

echo("<script>document.location=\"formular.htm\";</script>");
?>

kickloop
24. 06. 2008., 10:40
Na prvi pogled čini mi se da je sve ok u tvom kod-u. Pokušaj da pošalješ mail koristeći SMTP. Neki serveri ne dozvoljavaju mail ()

R. S.
24. 06. 2008., 11:16
Nije do servera sigurno. Funkcija mail provereno radi. Ne radi deo za prenos fajla. Naime, on upload-uje fajl ali ga ne prikači na mail kao attachment nego ga embed-uje u poruku u heksadecimalnom obliku... Hvala na odgovoru iako ne rešava problem...

Nemanja Avramović
24. 06. 2008., 11:31
Ja ti preporučujem da koristiš SwiftMailer (http://swiftmailer.org/) biblioteku koja radi 99% posla oko slanja mejlova a ima i odličnu dokumentaciju. Slanje mejla sa atačmentom obično predstavlja problem, pogotovo početnicima, ali uz Swiftmailer "it's a breeze" ;)

R. S.
24. 06. 2008., 12:04
Hvala. Download-van je SwiftMailer-er. Samo da vidim gde se ubacuje ugalj da krene ovo čudo :1087:

Nemanja Avramović
24. 06. 2008., 12:36
Kreni redom: http://swiftmailer.org/wikidocs/ :)

Korisni linkovi:
http://swiftmailer.org/wikidocs/v3/tutorials/basic
http://swiftmailer.org/wikidocs/easy/swift_connection_sendmail
http://swiftmailer.org/wikidocs/v3/attachments

Ivan
24. 06. 2008., 12:47
Ili mozes da koristis moju klasu: http://security-net.biz/files/mail/mail_mine.class.php.txt, postoji i novija verzija ako ti ova bude pravila probleme ...

japan
24. 06. 2008., 14:11
sto se tice koda koji si postavio ovde, najverovatnije je problem u lose formatiranjom headeru, neka greska '\r\n' u delu za attachment, tu je najosetljiviji. pa ako iz nekog razloga ne budes hteo/mogao da koristis predlozena resenja, obrati paznju na to.