Hi in this tutorial, PHP beginners can learn how to upload a resume and send mail with attachment in PHP.


follow few simple steps to it:


Step 1

Create index.php file and make a form to upload file and some fields to enter data like to, subject & message.


index.php

<html>
    <head>
        <title>Upload resume and send mail with attachment in php</title>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    </head>
    <body>

        <div class="container">
            <div class="row">
                <div class="col-md-3"></div>
                <div class="col-md-6">
                    
                    <h3>Send mail with attachment in php</h3>
                    
                    <form action="send_mail.php" method="post" name="form1" enctype="multipart/form-data">
                        <div class="form-group">
                            <label>To</label>
                            <input name="txtTo" type="text" class="form-control">
                        </div>


                        <div class="form-group">
                            <label>Subject</label>
                            <input name="txtSubject" type="text" class="form-control">
                        </div>

                        <div class="form-group">
                            <label>Message</label>
                            <textarea name="txtDescription" cols="30" rows="4" class="form-control"></textarea>
                        </div>


                        <div class="form-group">
                            <label>Upload Document</label>
                            <input name="fileAttach" type="file">
                        </div>



                        <div class="form-group">
                            <input type="submit" name="Submit" value="Send now" class="pull-right">
                        </div>

                    </form>

                </div>
                <div class="col-md-3"></div>

            </div>
        </div>
    </form>
</body>
</html>


Step 2

Now we need to action file form action where we can write a code for uploading file and sending mail, lets create send_mail.php file and put the following code .


send_mail.php


<?php

$strTo = $_POST["txtTo"];
$strSubject = $_POST["txtSubject"];
$strMessage = nl2br($_POST["txtDescription"]);

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));


//from name you can replace it
$from_name = "Rathorji";

//from email
$from_email = 'support@rathrji.in';


$strHeader = "";
$strHeader .= "From: " . $from_name . "<" . $from_email . ">\nReply-To: " . $from_email . "";

$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"" . $strSid . "\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";

$strHeader .= "--" . $strSid . "\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage . "\n\n";

//*** Attachment ***//
if ($_FILES["fileAttach"]["name"] != "") {
    $strFilesName = $_FILES["fileAttach"]["name"];
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
    $strHeader .= "--" . $strSid . "\n";
    $strHeader .= "Content-Type: application/octet-stream; name=\"" . $strFilesName . "\"\n";
    $strHeader .= "Content-Transfer-Encoding: base64\n";
    $strHeader .= "Content-Disposition: attachment; filename=\"" . $strFilesName . "\"\n\n";
    $strHeader .= $strContent . "\n\n";
}

$flgSend = @mail($strTo, $strSubject, null, $strHeader);

 if($flgSend) {
    echo "Mail send completed.";
} else {
    echo "Cannot send mail.";
}



You can replace your "From Name" and "From Email in above code".



Step 3

Now we need to run a application it will not work on localhost so I'll suggest you to use your live server for sending mail with attachment 



Output:


Thanks, May this example will help you.