problem in sending email with attached file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krishan123456
    New Member
    • Apr 2007
    • 1

    problem in sending email with attached file

    i have tried to send email with attached doc file but when i receive the mail i find the attached file in encoded text instead of actuall attachment.the code that i used to send an email is given below.


    function SendMail123($Fr om,$FromName,$T o,$ToName,$Subj ect,$Text,$Html ,$AttmFiles){
    $OB="----=_OuterBoundary _000";
    $IB="----=_InnerBoundery _001";

    $Html=$Html?$Ht ml:preg_replace ("/\n/","{br}",$T ext)
    or die("neither text nor html part present.");
    $Text=$Text?$Te xt:"Sorry, but you need an html mailer to read this mail.";
    $From or die("sender address missing");
    $To or die("recipient address missing");
    $Html=stripslas hes($Html);
    $headers ="MIME-Version: 1.0\r\n";
    $headers.="From : ".$FromName ." <".$From.">\n ";
    $headers.="To: ".$ToName." <".$To.">\n" ;
    $headers.="Repl y-To: ".$FromName ." <".$From.">\n ";
    $headers.="X-Priority: 1\n";
    $headers.="X-MSMail-Priority: High\n";
    $headers.="X-Mailer: My PHP Mailer\n";
    $headers.="Cont ent-Type: multipart/mixed;\n\tbound ary=\"".$OB."\" \n";
    //Messages start with text/html alternatives in OB
    $Msg ="This is a multi-part message in MIME format.\n";
    $Msg.="\n--".$OB."\n";
    $Msg.="Content-Type: multipart/alternative;\n\ tboundary=\"".$ IB."\"\n\n";
    //plaintext section
    $Msg.="\n--".$IB."\n";
    $Msg.="Content-Type: text/plain;\n\tchars et=\"iso-8859-1\"\n";
    $Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
    // plaintext goes here
    $Msg.=$Text."\n \n";
    // html section
    $Msg.="\n--".$IB."\n";
    $Msg.="Content-Type: text/html;\n\tcharse t=\"iso-8859-1\"\n";
    $Msg.="Content-Transfer-Encoding: base64\n\n";
    // html goes here
    $Msg.=chunk_spl it(base64_encod e($Html))."\n\n ";
    // end of IB
    $Msg.="\n--".$IB."--\n";
    // attachments
    if(count($AttmF iles) != 0){

    foreach($AttmFi les as $AttmFile){
    $patharray = explode ("/", $AttmFile);

    $FileName=$path array[count($patharra y)-1];
    $Msg.= "\n--".$OB."\n";
    $Msg.="Content-Type: application/octetstream;\n\ tname=\"".$File Name."\"\n";
    $Msg.="Content-Transfer-Encoding: base64\n";
    $Msg.="Content-Disposition: attachment;\n\t filename=\"".$F ileName."\"\n\n ";

    //file goes here
    $fd= @fopen ($AttmFile, "r");
    $FileContent= @fread($fd,file size($AttmFile) );
    @fclose ($fd);
    $FileContent=ch unk_split(base6 4_encode($FileC ontent));
    $Msg.=$FileCont ent;
    $Msg.="\n\n";
    }
    }
    //message ends
    $Msg.="\n--".$OB."--\n";
    mail($To,$Subje ct,$Msg,$header s);

    //syslog(LOG_INFO ,"Mail: Message sent to $ToName <$To>");
    }

    if(isset($_POST['submit']) && $_POST['submit'] == 'Submit'){
    //die("hhhhhhhh") ;
    $ATTM=array();
    @chmod('uploads/',777);

    $target_path = 'uploads/'.$_FILES['textDoc']['name'];

    //system("chmod -R 777 ".$target_path) ;
    //@chmod($target_ path,777);
    if(move_uploade d_file($_FILES['textDoc']['tmp_name'], $target_path)) {

    $ATTM[]=$target_path;

    }
    $message = $_POST["Comments"];
    $TEXT=$message;
    $HTML=$message ;
    $sender_email=$ _POST['txtEmail'];
    $ret = SendMail123(
    $sender_email,$ sender_emails, //sender
    "aaaaa@gmail.co m","Recipien ts Name", //recipient
    "PC - Free Assessment Form", //subject
    $TEXT,$HTML,$AT TM); //body and attachment(s)

    }




    below is the email that i got....

    From: <TEST123@TEST12 3.COM>
    To: Recipients Name <aaaaa@gmail.co m>
    Reply-To: <TEST123@TEST12 3.COM>
    X-Priority: 1
    X-MSMail-Priority: High
    X-Mailer: My PHP Mailer
    Content-Type: multipart/mixed;
    boundary="----=_OuterBoundary _000"


    This is a multi-part message in MIME format.

    ------=_OuterBoundary _000
    Content-Type: multipart/alternative;
    boundary="----=_InnerBoundery _001"


    ------=_InnerBoundery _001
    Content-Type: text/plain;
    charset="iso-8859-1"
    Content-Transfer-Encoding: quoted-printable

    test123


    ------=_InnerBoundery _001
    Content-Type: text/html;
    charset="iso-8859-1"
    Content-Transfer-Encoding: base64

    dGVzdDEyMw==




    ------=_InnerBoundery _001--

    ------=_OuterBoundary _000
    Content-Type: application/octetstream;
    name="val1.txt"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;
    filename="val1. txt"

    MTE2MDY3Mzg1NQ= =




    ------=_OuterBoundary _000--




    please help to get the actual file with attachment.
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    I always used PHPMailer to send email attachment. See if this can help you.

    Comment

    Working...