send email with attachment in php

Send Attachment With Email using php

Welcome back to shortlearner.com, in our previous post we learn how to add pagination using PHP and MySQL. so today in this post we will see how to Send Attachment With Email using php.

so first of all we should take an overview of our working scenario.
in the below code we are created a form with having email, subject,message and attachment fields.

once the form is submitted our script is check that the file’s format is valid or not.
if the file format is valid than the attachment is concatenate with our message field and with the help of MIME (Multipurpose Internet Mail Extensions) version and its content type and boundary.

we will send the attachment. once the mail is send successfully our script will display the success message and if the mail isn’t sent than it will display failure message as well.

send email with attachment in php

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

<html>
    <body>
        <h2>Send Mail</h2>
        <form method="post" action="" enctype="multipart/form-data">
            <input type="text" name="email" placeholder="email"><br>
            <input type="text" name="sub" placeholder="Subject"><br>
            <textarea name="msg" placeholder="Write email message"></textarea><br>

            Attach file:<br>
            <input type="file" name="attach1"/><br><br>
            <input type="submit" value="Send Mail"/>
        </form>
    </body>
</html>
<?php
if (isset($_FILES) && (bool) $_FILES) {

    $allowedExtensions = array("pdf", "doc", "docx", "gif", "jpeg", "jpg", "png");

    $files = array();
    foreach ($_FILES as $name => $file) {
        $file_name = $file['name'];
        $temp_name = $file['tmp_name'];
        $file_type = $file['type'];
        $path_parts = pathinfo($file_name);
        $ext = $path_parts['extension'];
        if (!in_array($ext, $allowedExtensions)) {
            die("File $file_name has the extensions $ext which is not allowed");
        }
        array_push($files, $file);
    }

    // email fields: to, from, subject, and so on
    $to = $_POST['email'];
    $from = "admin@localhost";
    $subject = $_POST['sub'];
    $message = $_POST['msg'];
    $headers = "From: $from";

    // boundary 
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    // headers -for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

    // multipart boundary 
    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
    $message .= "--{$mime_boundary}\n";

    // preparing attachments
    for ($x = 0; $x < count($files); $x++) {
        $file = fopen($files[$x]['tmp_name'], "rb");
        $data = fread($file, filesize($files[$x]['tmp_name']));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $name = $files[$x]['name'];
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
                "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
                "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
    }
    // send

    $ok = mail($to, $subject, $message, $headers);
    if ($ok) {
        echo "<p>mail sent to $to!</p>";
    } else {
        echo "<p>mail could not be sent!</p>";
    }
}
?>

guys this code is not works on local server like xampp,wamp and appserv. so try it on live server.