file attachment in php mail

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    file attachment in php mail

    Hii Guys,
    i need to make a php and html form and in tht form it should attach a file and send it to the recepient . how to do this function of attaching a file (any kind) ..



    Thanks,
    pradeep
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    have a look at this.

    Dont forget: google is your friend! ;)

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #3
      Originally posted by markusn00b
      have a look at this.

      Dont forget: google is your friend! ;)


      Hey markusn00b,
      I looked at that exapmle...it works...but i have more problems...i have a preview button tht previews all the entries and then in preview there is a subbmit button and also i need to attach 2 files..how to do this.

      Thanks,
      Pradeep

      Comment

      • pradeepjain
        Contributor
        • Jul 2007
        • 563

        #4
        This is the code ....please tell me how to add 2nd file in here..

        [PHP]<?php
        // Read POST request params into global vars
        $to = $_POST['to'];
        $from = "webmaster@iiap .res.in";
        $subject = $_POST['subject'];
        $message = $_POST['message'];

        // Obtain file upload vars
        $fileatt = $_FILES['fileatt']['tmp_name'];
        $fileatt_type = $_FILES['fileatt']['type'];
        $fileatt_name = $_FILES['fileatt']['name'];

        $fileatt1 = $_FILES['fileatt1']['tmp_name'];
        $fileatt1_type = $_FILES['fileatt1']['type'];
        $fileatt1_name = $_FILES['fileatt1']['name'];

        $headers = "From: $from";

        // Read the file to be attached ('rb' = read binary)
        $file = fopen($fileatt, 'rb');
        $data = fread($file,fil esize($fileatt) );
        fclose($file);
        $file1 = fopen($fileatt1 ,'rb');
        $data1 = fread($file1,fi lesize($fileatt 1));
        fclose($file1);
        // Generate a boundary string
        $semi_rand = md5(time());
        $mime_boundary = "==Multipart_Bo undary_x{$semi_ rand}x";

        // Add the headers for a file attachment
        $headers .= "\nMIME-Version: 1.0\n" .
        "Content-Type: multipart/mixed;\n" .
        " boundary=\"{$mi me_boundary}\"" ;

        // Add a multipart boundary above the plain message
        $message = "This is a multi-part message in MIME format.\n\n" .
        "--{$mime_boundary }\n" .
        "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
        $message . "\n\n";
        // Base64 encode the file data
        $data = chunk_split(bas e64_encode($dat a));

        // Add file attachment to the message

        $message .= "--{$mime_boundary }\n" .
        "Content-Type: {$fileatt_type} ;\n" .
        " name=\"{$fileat t_name}\"\n" .
        //"Content-Disposition: attachment;\n" .
        //" filename=\"{$fi leatt_name}\"\n " .
        "Content-Transfer-Encoding: base64\n\n" .
        $data . "\n\n" .
        "--{$mime_boundary }--\n";

        /*how to add 2nd file details here*/

        // Send the message
        $ok = @mail($to, $subject, $message, $headers);
        if ($ok) {
        echo "<p>Mail sent! Yay PHP!</p>";
        } else {
        echo "<p>Mail could not be sent. Sorry!</p>";
        }
        ?>
        [/PHP]

        Comment

        • pradeepjain
          Contributor
          • Jul 2007
          • 563

          #5
          another problem is ....as u cab see i am using smarty engine..

          and the $_FILES['variable']['type'] is not working here...is there any solution ....is this a problem with smarty engine or wht..


          Thanks,
          Pradeep

          Comment

          • pradeepjain
            Contributor
            • Jul 2007
            • 563

            #6
            Hii,
            I have solved all the problems i have given . i have tried the code to send 2 attachments also it worked as a singel file...but when i used the code in smarty engine its not recognizing the headers.its printing as it is in the mail.


            the code for it is

            [PHP]<?

            require_once('l ibs/Smarty.class.ph p');
            $smarty = new Smarty;
            //require_once('d b.inc');
            $smarty->compile_dir = 'templates_c/';

            $smarty->assign("TPL_Na me",$_POST['Name']);
            $smarty->assign("TPL_DO B",$_POST['DOB']);
            $smarty->assign("TPL_Ge nder",$_POST['Gender']);
            $smarty->assign("TPL_Na tionality",$_PO ST['Nationality']);
            $smarty->assign("TPL_Af filiation",$_PO ST['Affiliation']);
            $smarty->assign("TPL_Em ail",$_POST['Email']);
            $smarty->assign("TPL_Ph one",$_POST['Phone']);
            $smarty->assign("TPL_Fa x",$_POST['Fax']);
            $smarty->assign("TPL_Po stalAddress",$_ POST['PostalAddress']);
            $smarty->assign("TPL_Pe rmanentAddress" ,$_POST['PermanentAddre ss']);

            $smarty->assign("TPL_De gree1",$_POST['Degree1']);
            $smarty->assign("TPL_In stitute1",$_POS T['Institute1']);
            $smarty->assign("TPL_Un iversity1",$_PO ST['University1']);
            $smarty->assign("TPL_Ye ar1",$_POST['Year1']);
            $smarty->assign("TPL_Cl ass1",$_POST['Class1']);
            $smarty->assign("TPL_Ra nk1",$_POST['Rank1']);
            $smarty->assign("TPL_Ma jor1",$_POST['Major1']);

            $smarty->assign("TPL_De gree2",$_POST['Degree2']);
            $smarty->assign("TPL_In stitute2",$_POS T['Institute2']);
            $smarty->assign("TPL_Un iversity2",$_PO ST['University2']);
            $smarty->assign("TPL_Ye ar2",$_POST['Year2']);
            $smarty->assign("TPL_Cl ass2",$_POST['Class2']);
            $smarty->assign("TPL_Ra nk2",$_POST['Rank2']);
            $smarty->assign("TPL_Ma jor2",$_POST['Major2']);

            $smarty->assign("TPL_De gree3",$_POST['Degree3']);
            $smarty->assign("TPL_In stitute3",$_POS T['Institute3']);
            $smarty->assign("TPL_Un iversity3",$_PO ST['University3']);
            $smarty->assign("TPL_Ye ar3",$_POST['Year3']);
            $smarty->assign("TPL_Cl ass3",$_POST['Class3']);
            $smarty->assign("TPL_Ra nk3",$_POST['Rank3']);
            $smarty->assign("TPL_Ma jor3",$_POST['Major3']);

            $smarty->assign("TPL_De gree4",$_POST['Degree4']);
            $smarty->assign("TPL_In stitute4",$_POS T['Institute4']);
            $smarty->assign("TPL_Un iversity4",$_PO ST['University4']);
            $smarty->assign("TPL_Ye ar4",$_POST['Year4']);
            $smarty->assign("TPL_Cl ass4",$_POST['Class4']);
            $smarty->assign("TPL_Ra nk4",$_POST['Rank4']);
            $smarty->assign("TPL_Ma jor4",$_POST['Major4']);

            $smarty->assign("TPL_Ph DTopic",$_POST['PhDTopic']);
            $smarty->assign("TPL_Re feree1",$_POST['Referee1']);
            $smarty->assign("TPL_Re feree2",$_POST['Referee2']);
            $smarty->assign("TPL_Ju stify",$_POST['Justify']);
            $smarty->assign("TPL_Tr avelSupport",$_ POST['TravelSupport']);
            $smarty->assign("TPL_Tr avelSupportNeed ",$_POST['TravelSupportN eed']);


            $smarty->assign("TPL_ci ty",$_POST['city']);
            $smarty->assign("TPL_po st",$_POST['post']);
            $smarty->assign("TPL_co untry",$_POST['country']);

            $smarty->assign("TPL_pc ity",$_POST['pcity']);
            $smarty->assign("TPL_pp ost",$_POST['ppost']);
            $smarty->assign("TPL_pc ountry",$_POST['pcountry']);

            $smarty->assign("TPL_sa mple",$_POST['sample']);
            $smarty->assign("TPL_TS D",$_POST['TSD']);
            $smarty->assign("TPL_cv ",$_FILES['cv']['name']);
            $smarty->assign("TPL_pu blication",$_FI LES['publication']['name']);


            $fileatt = $_FILES['cv']['tmp_name'];
            $fileatt_type = $_FILES['cv']['type'];
            $fileatt_name = $_FILES['cv']['name'];
            print_r($_FILES['cv']);
            print_r($_FILES['publication']);

            $fileatt1 = $_FILES['publication']['tmp_name'];
            $fileatt1_type = $_FILES['publication']['type'];
            $fileatt1_name = $_FILES['publication']['name'];

            // Read the file to be attached ('rb' = read binary)
            $file = fopen($fileatt, 'rb');
            $data = fread($file,fil esize($fileatt) );
            fclose($file);
            $file1 = fopen($fileatt1 ,'rb');
            $data1 = fread($file1,fi lesize($fileatt 1));
            fclose($file1);



            if($_POST['TPL_Flag']!="Mail"){
            $smarty->assign("TPL_Fl ag",'Preview' );
            $smarty->display('appne w.tpl');
            }else{
            $smarty->assign("TPL_Fl ag",'Mail');
            $msg=$smarty->fetch('appnew. tpl');

            // Generate a boundary string
            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Bo undary_x{$semi_ rand}x";

            // Add the headers for a file attachment
            $headers .= "\r\nMIME-Version: 1.0\r\n" .
            "Content-Type: multipart/mixed;" .
            " boundary=\"{$mi me_boundary}\"\ r\n";
            // Add a multipart boundary above the plain message
            $message1 .= "This is a multi-part message in MIME format" . $mime_boundary . "\r\n" .
            "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" .
            "Content-Transfer-Encoding: 7bit\r\n" .
            $msg . "\r\n";
            // Base64 encode the file data
            $data = chunk_split(bas e64_encode($dat a));
            $data1 = chunk_split(bas e64_encode($dat a1));
            // Add file attachment to the message
            $message1 .= "--{$mime_boundary }\r\n" .
            "Content-Type: {$fileatt_type} ;\n" .
            " name=\"{$fileat t_name}\"\r\n" .
            "Content-Transfer-Encoding: base64\r\n" .
            $data . "\r\n";
            $message1 .= "--{$mime_boundary }\r\n" .
            "Content-Type: {$fileatt1_type };\r\n" .
            " name=\"{$fileat t1_name}\"\r\n" .
            "Content-Transfer-Encoding: base64\r\n" .
            $data1 . "\r\n" .
            "--{$mime_boundary }--\r\n";




            $sub = "Astrostat School Application from " . $_POST['Name'] . "," .$_POST['country'];

            //$headers = "From: ";
            $headers = "From: \r\nContent-Type: text/html; charset=\"iso-8859-1\"\r\n";

            $to = "";
            $mail_ret =@mail($to, $sub, $message1, $headers);

            if($mail_ret){
            $message ="Your application for the 2nd IIA-PennState Astrostatistics school has been successfully submitted.";
            }else{
            $message ="Registrati on failed. Please try after some time.";
            }


            //Display message in a template
            $smarty->assign("TPL_Me ssage",$message );
            $smarty->Display("newme ssage.tpl");

            }

            ?>[/PHP]


            and in the mail at begging its printing as This is a multi-part message in MIME format==Multipa rt_Boundary_x96 a252f89aecb7b63 748437f2ce20e62 x Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit

            and at the end
            --==Multipart_Bou ndary_x96a252f8 9aecb7b63748437 f2ce20e62x Content-Type: ; name="" Content-Transfer-Encoding: base64 --==Multipart_Bou ndary_x96a252f8 9aecb7b63748437 f2ce20e62x Content-Type: ; name="" Content-Transfer-Encoding: base64 --==Multipart_Bou ndary_x96a252f8 9aecb7b63748437 f2ce20e62x--


            Thanks,
            Pradeep

            Comment

            Working...