phprockers-logo

How to send the email with attachment in php?

Very simple to send the email with attachment with php validation.

First we have to write html form for upload the attchement.

<form name="uploadRes" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF'];" >
<input type="file" name="resume" id="resume" />
<input type="submit" name="res-submit" id="res-submit" value="Send Resume"  />
</form>

Validation for attaching the file. Here we have to find the file type, then send the email to particular function.

if($_REQUEST['res-submit'] != '') {
    if(!empty($_FILES)) {
        function getExt($file) {
            $dot = strrpos($file, '.') + 1;
            return substr($file, $dot);
        }


        $resume = $_FILES['resume']['name'];
        if($resume != '')
            $resume = $resume;
        else
            $resume = 'Not Uploaded!';
       
        $extension = getExt($resume);
       
        if( $resume== 'Not Uploaded!')
            $error_flag = 1;
        else {
            if($extension != 'doc' || $extension != 'docx' || $extension != 'pdf' || $extension != 'odt' || $extension != 'txt') {
                $to = 'php.com';
                $from    = "candidate@dckap.com";
                $subject = 'Resume from candidate';
   
                $fileatt      = $_FILES['resume']['tmp_name'];
                $fileatt_type = $_FILES['resume']['type'];
                $fileatt_name = $_FILES['resume']['name'];
                $fileatt_size = $_FILES['resume']['size'];
                if($fileatt_size < 300000) {
                    $headers = "From: $from";
                    $message="<p> Hi, <br/><br/>Please find the attached resume. </p>";
                    $email_message .= $message;
                    $semi_rand = md5(time());
                    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
                    $headers .= "\nMIME-Version: 1.0\n" .
                    "Content-Type: multipart/mixed;\n" .
                    " boundary=\"{$mime_boundary}\"";

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

                    $data = chunk_split(base64_encode(file_get_contents($fileatt)));
                    $email_message .= "--{$mime_boundary}\n" .
                    "Content-Type: {$fileatt_type};\n" .
                    " name=\"{$fileatt_name}\"\n" .
                    //"Content-Disposition: attachment;\n" .
                    //" filename=\"{$fileatt_name}\"\n" .
                    "Content-Transfer-Encoding: base64\n\n" .
                    $data .= "\n\n" .
                    "--{$mime_boundary}--\n";
                    $ok = mail($to, $subject, $email_message, $headers);  // Mail sending to the requested Email Id
                } else {
                   
                    $error_flag_filesize = 3;
                }

            } else {
                $error_flag = 2;
            }   
        }
    }
}
?>

Here we are showing the validation errors.

 
<?php if(!empty($_FILES)) {  if($error_flag == 1) { ?>
<div id="result-resume" name="result-resume" >
<span style="color:red; margin-left:-245px;">please upload the file</span>
</div>
<?php } elseif ($error_flag == 2) { ?>
<div id="result-resume" name="result-resume" >
<span style="color:red; margin-left:-245px;">Please Upload Valid file Extension!</span>
</div>
<?php } elseif ($error_flag_filesize == 3) { ?>
<div id="result-resume" name="result-resume" >
<span style="color:red; margin-left:-245px;">Please upload the file less than 3MB</span>
</div>
<?php } else { ?>
<div id="result-resume" name="result-resume" >
<span style="color:red; margin-left:-245px;">Sent the resume successful</span>
</div>
<?php }?>
<script>hideResult=document.getElementById("result-resume");
setTimeout('hideResult.innerHTML=\'\'',7000); </script>
<?php }?>

0 comments:

Post a Comment