codegrid
codegrid

Reputation: 1007

Empty File with Mail attachment - PHP

I am using the below script to send an attachment with a mail. It sends the mail but the file attached is empty. I have gone through the script but don't seem to find where the error is coming from.

Php:

    $file_name = $_FILES['attachment']['name'];
    $temp_name = $_FILES['attachment']['tmp_name'];
    $file_type = $_FILES['attachment']['type'];

    // get file extension

    $base = basename($file_name);
    $extension = substr($base, strlen($base)-4, strlen($base));

    // allowed file types

    $allowed_extensions = array(".doc", ".docx", ".pdf");

    $from = $_POST['email'];
    $to = "[email protected]";
    $subject = "CV Upload";
    $message = "My CV";

    $file_name = $temp_name;
    $content = chunk_split(base64_encode(file_get_contents($file)));
    $uid = md5(uniqid(time()));

    $header = "From: ".$from."\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";

    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";

    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset-iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";

    $header .= "--".$uid."\r\n";
    $header .= "Content-type: ".$file_type."; name=\"".$file_name."\"\r\n";
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";

Upvotes: 0

Views: 1434

Answers (1)

Nelson Owalo
Nelson Owalo

Reputation: 2414

check your code

$content = chunk_split(base64_encode(file_get_contents($file)));

Technicaly, $file doesn't exist. Use $file_name

$content = chunk_split(base64_encode(file_get_contents($file_name)));

Upvotes: 2

Related Questions