php - instead of uploading to folder, send as email with attachment of file uploaded

566 views Asked by At
<?php
    //if there is post
if(isset($_POST) && !empty($_POST)){
        //if there is attachment
        if(!empty($_FILES['attachment']['name'])){
            //store some variables
            $file_name = $_FILES['attachment']['name'];
            $temp_name = $_FILES['attachment']['tmp_name'];
            $file_type = $_FILES['attachment']['type'];

            //get the extension of the file
            $base = basename($file_name);
            $extension = substr($base, strlen($base)-4,strlen($base));

            //only these file type will be allowed
            $allow_extensions = array(".doc","docx",".pdf",".zip",".png");

            //check if file is allowes
            if(in_array($extension,$allow_extensions)){

                //mail essentials
                $from = $_POST['email'];
                $to = "[email protected]";
                $replyto = $to;
                $subject = "email with attachment";
                $message = "this is a random message";

                //things you need
                $file = $temp_name;
                $content = chunk_split(base64_encode(file_get_contents($file)));
                $uid = md5(uniqid(time()));

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

                //declairing we hav e multiple parts of message like text
                $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
                $header .= "This is a multi-part message in MIME format. \r\n";

                //plain text part
                $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";

                //file attachment
                $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";

                //send mail 
                if(mail($to, $subject, "", $header)){
                    echo "Mail Sent";
                }
                else
                {
                    echo "Failed";
                }

            }
            else{
                echo "file type not allowed";
            }


        }
        else{
            echo "no file posted";
        }
    }
?>


<html>
    <head>

    </head>
    <body>
    <form method="post" action="index.php" enctype="multipart/form-data">
        <input type="text" name="email" value="from" />
        <br>
        <input type="file" name="attachment" />
        <br>
        <input type="submit" value="Send Mail" />
    </form>     
    </body>
</html>

Can't find the bug to my code, I am not getting the link to my attachment.. T_T please help. I am not good in boundary.. T_T I tried using phpmailer but can't get it to work, is there any document to read on how to set it up? I really just wanted to make a simple form with attach button for applicant to sent their resumes..

1

There are 1 answers

0
mike.k On

Everything after $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; should be in a $body, not a header.

I would get rid of this: $header .= "This is a multi-part message in MIME format. \r\n";

After $content you actually need to put another line with the mime separator.

$body .= $content . "\r\n" . '--' . $uid . '--';

And finally

if (mail($to, $subject, $body, $header)) {...