Send File Attachment by using PHP Form


Send File Attachment by using PHP Form

PHP mail with Attachment

In this smart tutorial we are going to see about how to Send File Attachment by using simple PHP Form and we using “multipart/form-data” so it is encoded when the send the file from our server ok let’s come to our tutorial part.

Step 1 : Create a HTML Form by using index.php extension

<form name="form1" enctype="multipart/form-data" method="post" action="mail.php">
<label>Your Name
<input type="text" name="name" />
</label>
<label>Your Email
<input type="email" name="email" />
</label>
<label>Attachment
<input type="file" name="my_file" />
</label>
<label>
<input type="submit" name="button" value="Submit" />
</label>
</form>

 

Step2 : create a mail.php File.This is the Complete PHP code for File attachment send the File to the user’s or clients from the PHP form.

Copy the below PHP mail with Attachment code


<?php

if($_POST && isset($_FILES['my_file']))
{

$from_email = 'Your Mail'; //sender email
$recipient_email = $_POST['mail'];
$user_email = $_POST['email'];
$subject = 'Mail From Mskv';
$message = $_POST['msg'];

//get file details we need
$file_tmp_name = $_FILES['my_file']['tmp_name'];
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];

$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);

if($file_error>0)
{
die('upload error');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("sanwebe");
//header
$headers = "MIME-Version: 1.0rn";
$headers .= "From:".$from_email."rn";
$headers .= "Reply-To: ".$user_email."rn";
$headers .= "Content-Type: multipart/mixed; boundary = $boundaryrnrn";

//plain text
$body = "--$boundaryrn";
$body .= "Content-Type: text/plain; charset=ISO-8859-1rn";
$body .= "Content-Transfer-Encoding: base64rnrn";
$body .= chunk_split(base64_encode($message));

//attachment
$body .= "--$boundaryrn";
$body .="Content-Type: $file_type; name="$file_name"rn";
$body .="Content-Disposition: attachment; filename="$file_name"rn";
$body .="Content-Transfer-Encoding: base64rn";
$body .="X-Attachment-Id: ".rand(1000,99999)."rnrn";
$body .= $encoded_content;

$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{
die('Your File Attachment successfully Mailed');
}else{
die('Could not send mail! Please check your PHP mail configuration.');
}

}

?>

successfully we create a PHP form for sending file attachments like email.i tested this PHP code its work perfectly.This type of PHP really helpful to the online business people who need privacy for sending attachment’s and important files.

Final words

Hope this PHP form will send the file attachment to your user’s.if you are a non-coding guy please feel free to comment here I will help you to build the PHP mail attachment form.


Was this article helpful?
Thanks!

Your feedback helps us improve Allwebtuts.com



2 Shares
+11
Tweet
Share
Pin1
Stumble