PHP mail send Body with attachment




<?php

        $to = 'satyam.gupta@test.com';
        $emailBody .= "<html><body>Blah</body></html>";
        $emailSubject = "Subject";
        $emailCSV = "\"Col1\", \"Col2\"\r\n"; // etc.
        $instructions ="Hello hi";
        $filepath = '../images/App_images/Interview Questions/Interview Questions.zip';       
        $attachment = chunk_split(base64_encode(file_get_contents($filepath)));
        $filename="test.zip";



        $boundary = md5(time());
        $eol = "\r\n";

        $header = "From: Abc <abc@test.com>\r\n";
        $header .= "MIME-Version: 1.0\r\n";
        $header .= "Content-Type: multipart/mixed;boundary=\"" . $boundary . "\"\r\n";

        $output = "--".$boundary."\r\n";
        $output .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
        $output .= "Content-Transfer-Encoding: base64" . $eol;
        $output .= "Content-Disposition: attachment" . $eol;
        $output .= $attachment . $eol;
        $output .= "--".$boundary."\r\n";
        $output .= "Content-type: text/html; charset=\"utf-8\"\r\n";
        $output .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
        $output .= $emailBody."\r\n\r\n";
        $output .= "--".$boundary."--\r\n\r\n";

        $mail_sent = @mail($to, $emailSubject, $output, $header);
        echo $mail_sent ? "Mail sent" : "Mail failed";


?>

Previous
Next Post »