-1

Why my script don't work?
I tried with xampp and with online host.
I tried with $headers = "From: $from \r\n";
I got "Sent successfully!" but I don't recieve any mail.
$to = "********"; $from = $_POST['email']; $nume = $_POST['nume']; $prenume = $_POST['prenume']; $phone = $_POST['telefon']; $oras = $_POST['oras']; $adresa = $_POST['adresa']; $facultate = $_POST['facultate']; $titlu1 = $_POST['titlu1']; $desc1 = $_POST['desc1']; $titlu2 = $_POST['titlu2']; $desc2 = $_POST['desc2']; $titlu3 = $_POST['titlu3']; $desc3 = $_POST['desc3']; $subject = "Luminile Iernii - Inscriere: $nume $prenume"; $message = " Nume si Prenume: $nume $prenume \n Email: $from \n Nr. Telefon: $phone \n Oras: $oras \n Adresa: $adresa \n Institutia de invatamant: $facultate \n Titlu Fotografie 1: $titlu1 \n Descriere Fotografie 1: $desc1 \n Titlu Fotografie 2: $titlu2 \n Descriere Fotografie 2: $desc2 \n Titlu Fotografie 3: $titlu3 \n Descriere Fotografie 3: $desc3 \n ";

    // Temporary paths of selected files
    $file1 = $_FILES['file1']['tmp_name'];
    $file2 = $_FILES['file2']['tmp_name'];
    $file3 = $_FILES['file3']['tmp_name'];

    // File names of selected files
    $filename1 = "Fotografie 1";
    $filename2 = "Fotografie 2";
    $filename3 = "Fotografie 3";

    // array of filenames to be as attachments
    $files = array($file1, $file2, $file3);
    $filenames = array($filename1, $filename2, $filename3);

    // include the from email in the headers
    $headers = "From: $from";

    // boundary
    $time = md5(time());
    $boundary = "==Multipart_Boundary_x{$time}x";

</code>

Is boundary necessary with attachment?

<code>
    // headers used for send attachment with email
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\"";

    // multipart boundary
    $message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$boundary}\n";

    // attach the attachments to the message
    foreach( $files as $key => $value )
    {
        if( empty( $value ) )
        {
            unset( $files[$key] );
            unset( $filenames[$key] );
        }
    }

    for($x = 0; $x < count($files); $x++) {
        $file = fopen($files[$x], "r");
        $content = fread($file,filesize($files[$x]));
        fclose($file);
        $content = chunk_split(base64_encode($content));
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . "Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" . "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";
        $message .= "--{$boundary}\n";
    }

    // sending mail
    $sendmail = mail($to, $subject, $message, $headers);

    // verify if mail is sent or not
    if ($sendmail) {
        echo "Sent successfully!";
    } else {
        echo "Error occurred. Try again!";
    }

</code>

Someone can exaplin to me?
I don't get it... what I did wrong?

2 Answers2

2

Try using http://github.com/PHPMailer/PHPMailer

This is a PHP plugin that handles all the stressful tasks of sending mail without writing all the code.

Download the PHPMailer Script from http://github.com/PHPMailer/PHPMailer and upload it to your server.

Include that file like so: <?php require_once('/path/to/class.phpmailer.php'); ?>

Your end result would look something like this:

require_once('/path/to/class.phpmailer.php');

$email = new PHPMailer();
$email->From      = 'you@example.com';
$email->FromName  = 'Your Name';
$email->Subject   = 'Message Subject';
$email->Body      = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';

$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

$email->Send();

If you were to try and send attachments without this class file, you would be writing stacks of code that just isn't needed. This plugin makes sending email much much easier and ultimately sending email with attachments much easier as well.

I hope this helps.

Jason Bassett
  • 1,281
  • 8
  • 19
0

Does your from has the enctype set for sending files?

<form action="upload.php" method="post" enctype="multipart/form-data">

This might help you out: http://www.w3schools.com/php/php_file_upload.asp

bobbybouwmann
  • 983
  • 3
  • 12
  • 24