Categories
PHP

PHP send e-mail with attachment using mail()

I am making a form where user would fill out information about himself and than he would attach a resume which would than be send to me in an e-mail. The e-mail arrives but attachment is always empty. I used this as the basis for the php https://eureka.ykyuen.info/2010/02/16/php-send-attachmemt-with-php-mail/

PHP code

$name = HTMLSpecialChars($_POST["name"]);
$surname = HTMLSpecialChars($_POST["surname"]);
$phone = HTMLSpecialChars($_POST["phone"]);
$email = HTMLSpecialChars($_POST["email"]);
if (!empty($_POST["name"]) && !empty($_POST["surname"]) && !empty($_POST["phone"]) && !empty($_POST["email"])):
    $atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]'; 
    $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; 
    if (eregi("^$atom+(\\.$atom+)*@($domain?\\.)+$domain\$", $email)):
        $body .="Jmeno: $name\n";
        $body .="Prijemni: $surname\n";
        $body .="Telefon: $phone\n";
        $body .="E-mail: $email\n\n";
        $body .="\n\n";
        $subjekt="Zivotopis z webu";
        $mail="xxxx@xxxx.xx";

        $headers = "From: ".$email."\n";
        $headers .= "MIME-Version: 1.0\n"."Content-Type: text/plain; charset=\"iso-8859-2\"\n"."Content-Transfer-Encoding: base64\n";

        $file_name = basename($_FILES['uploaded_file']['name']);
        $path = $_FILES["uploaded_file"]["tmp_name"];

        $file = $path.$file_name;
        $file_size = filesize($file);
        $handle = fopen($file, "r");
        $content = fread($handle, $file_size);
        fclose($handle);
        $content = chunk_split(base64_encode($content));

        $boundary = md5(uniqid(time()));

        $headers .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\"".PHP_EOL;
        $headers .= "This is a multi-part message in MIME format.".PHP_EOL;
        $headers .= "--".$boundary.PHP_EOL;

        $headers .= "Content-type:text/plain; charset=iso-8859-1".PHP_EOL;
        $headers .= "Content-Transfer-Encoding: 7bit".PHP_EOL.PHP_EOL;
        $headers .= "$body".PHP_EOL;
        $headers .= "--".$boundary.PHP_EOL;

        $headers .= "Content-Type: application/xml; name=\"".$file_name."\"".PHP_EOL;
        $headers .= "Content-Transfer-Encoding: base64".PHP_EOL;
        $headers .= "Content-Disposition: attachment; filename=\"".$file_name."\"".PHP_EOL.PHP_EOL;
        $headers .= $content.PHP_EOL;
        $headers .= "--".$boundary."--";

        if (Mail ($mail,$subjekt,$body,$headers)):

HTML code

<form class="zivotopis" action="cz.php?txt=kariera" method="post" enctype="multipart/form-data">
<input type="file" name="upload_file"> 

Leave a comment