Send file as E-mail attachment using php mail() script
Send file as E-mail attachment using php mail() script
Hi friends,
Today
I am going to explain you, how to send a file as attachment using php mail()
function. You can use this functionality for recruitments or career page of
your website.
Below is the UI of form for which we are
writing mail script.
When user fills the form with attachment
and submits, the script automatically sends e-mail to specific e-mail
address(it may be your site admin email address ) with attachment.(See below image)
To get this thing done we use
mail|_attachment () and pass temp file
name as a parameter like:
mail_attachment($from,
"info@ur-domain.com",
"New Resume Submitted", $message,
("temp/".$_FILES["file"]["name"]));
Replace ‘info@ur-domain.com’ with
your email address.
And attached file moves to
temp folder of your sites root directory.
move_uploaded_file($_FILES["file"]["tmp_name"],'temp/'.basename($_FILES['file']['name']));
Complete Script(Modify script according to your
form ):
<?php
$msg = "";
if(isset($_POST['submit']))
{
// Variable declaration
$name = $_POST['name'];
$email_from =
$_POST['email'];
$file=$_FILES["file"]["name"];
$mess =$_POST['message'];
// How mail will look like at receiver’s end
$message=
"<html><head></head><body><table width='700'
height='200' bgcolor='#EFEEE8' style='color:#000;border:none;
text-align:justify; padding:20px;'>
<tr>
<td style='border:none'>Name:
$name<br> </td>
</tr>
<tr>
<td style='border:none'>Email
ID: $email_from </td>
</tr>
<tr>
<td
style='border:none'>Message: $mess <br> </td>
</tr>
<tr>
<td
style='border:none'>File:Find details of applicant below: <br>
</td>
</tr>
</table>
</body></html>";
if ($file)
{
//Function deliration
function mail_attachment
($from , $to, $subject, $message, $attachment){
$fileatt = $attachment; // Path to the file
$fileatt_type = "application/octet-stream";
// File Type
$start= strrpos($attachment, '/') == -1 ?
strrpos($attachment, '//') : strrpos($attachment, '/')+1;
$fileatt_name = substr($attachment, $start,
strlen($attachment)); // Filename that will be used for the file as the
attachment
$from = $email_from; // Who the email is from
$subject = "New Attachment Message";
$email_subject =
$subject; // The Subject of the email
$email_txt = $message; // Message that the email has
in it
$email_to = $to; // Who the email is to
$headers = "From: ".$_POST['email'];
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$msg_txt="\n\n You have recieved a new
attachment message from ".$from;
$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_txt .= $msg_txt;
$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_txt . "\n\n";
$data = chunk_split(base64_encode($data));
$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($email_to, $email_subject,
$email_message,$headers);
if($ok)
{
$msg = "File Sent Successfully.";
unlink($attachment); // delete a file after
attachment sent.
}
else
{
die("Sorry but the email could not be sent.
Please go back and try again!");
}
}
//Move uploaded file to temp folder of root directory of your site
move_uploaded_file($_FILES["file"]["tmp_name"],'temp/'.basename($_FILES['file']['name']));
// calling function
mail_attachment($from,
"info@ur-domain.com", "New Resume Submitted", $message,
("temp/".$_FILES["file"]["name"]));
}
$msg = "Your mail
successfully sent.";//message
}
?>
HTML which supports above script:
<form
class="contact-form clearfix" action="#"
method="post"
enctype="multipart/form-data">
<div class="row">
<div class="col-md-4 col-sm-4
col-xs-4">
<p
class="input-block">
<input type="text"
value=""
id="comment_name" name="name" class="valid"
required>
</p>
</div>
<!--
col-md-3 -->
<div
class="col-md-8 col-sm-8 col-xs-8">
<div
class="input-label">
<p>name <span>*</span></p>
<p>Your full name please.</p>
</div>
</div>
<!--
col-md-9 -->
</div>
<!-- row
-->
<div
class="row">
<div class="col-md-4
col-sm-4 col-xs-4">
<p
class="input-block">
<input type="email" value="" id="comment_email"
name="email" class="valid" required>
</p>
</div>
<!--
col-md-3 -->
<div
class="col-md-8 col-sm-8 col-xs-8">
<div
class="input-label">
<p>Email Address <span>*</span></p>
<p>Used for gravatar.</p>
</div>
</div>
<!--
col-md-9 -->
</div>
<!-- row
-->
<div class="row">
<div
class="col-md-4 col-sm-4 col-xs-4">
<p
class="input-block">
<input type="file" value="" id="comment_email"
name="file" class="valid">
</p>
</div>
<!--
col-md-3 -->
<div
class="col-md-8 col-sm-8 col-xs-8">
<div
class="input-label">
<p>Browse Resume <span>*</span></p>
<p>Attach your resume.</p>
</div>
</div>
<!--
col-md-9 -->
</div>
<!-- row
-->
<div
class="row">
<div
class="col-md-12 col-sm-12 col-xs-12">
<p
class="textarea-label">Something About You
span>(required):</span></p>
<p
class="textarea-block">
<textarea name="message"
id="contact_message"
cols="88" rows="10" required></textarea>
</p>
</div>
<!--
col-md-12 -->
</div>
<!-- row
-->
<div
class="row">
<div
class="col-md-12 col-sm-12 col-xs-12">
<p
class="contact-button clearfix">
<span><input type="submit" value="Send
Resume" id="submit-contact"
name="submit"></span>
</p>
</div>
<!--
col-md-12 -->
</div>
<!-- row -->
</form>
Include above form html on your page attach
script with this and enjoy the functionality simpleJ
!...............................................................
Happy Coding................................................................. !
0 comments