PHP script to send mail with Attachment
You probably know how to send email with PHP, but it gets little trickier when you want to send attachment with PHP email. Here's another example you can use to send attachment with PHP mail.
Step2: PHP code that will send email
That’s it, you can capture subject and message body from HTML form and use them in the code.
Let's see step by step how to send email with attachment.
Step1: Create html form
<div class="row"> <div class="col-md-6 col-md-offset-3"> <h1 class="page-header text-center">Send PHP mail with Attachment</h1> <form class="form-horizontal" enctype="multipart/form-data" method="post" action="send.php"> <div class="form-group"> <label for="InputName" class="col-sm-2 control-label">Name</label> <div class="col-sm-10"> <input type="text" class="form-control" name="name" id="InputName" placeholder="Enter Name" required=""> </div> </div> <div class="form-group"> <label for="InputEmail" class="col-sm-2 control-label">Email</label> <div class="col-sm-10"> <input type="email" class="form-control" id="InputEmail" name="email" placeholder="Enter Email" required=""> </div> </div> <div class="form-group"> <label for="InputFile" class="col-sm-2 control-label">Attachment</label> <div class="col-sm-10"> <input type="file" class="form-control" id="InputFile" name="my_file"> </div> </div> <div class="form-group"> <label for="InputReal" class="col-sm-2 control-label">4+3?</label> <div class="col-sm-10"> <input type="text" class="form-control" name="human" id="InputReal" required=""> </div> </div> <div class="form-group"> <div class="col-sm-10 col-sm-offset-2"> <input type="submit" name="submit" id="submit" value="Submit" class="btn btn-info pull-right"> </div> </div> </form> </div> </div>
Step2: PHP code that will send email
if($_POST && isset($_FILES['my_file'])) { $from_email = 'sender_mail@example.com'; //sender email $subject = 'Test mail'; //subject of email $message = 'This is body of the message'; //message body //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); $recipient_email = $user_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("developersarea"); //header $headers = "MIME-Version: 1.0\r\n"; $headers .= "From:".$from_email."\r\n"; $headers .= "Reply-To: ".$user_email."" . "\r\n"; $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; //plain text $body = "--$boundary\r\n"; $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n"; $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; $body .= chunk_split(base64_encode($message)); //attachment $body .= "--$boundary\r\n"; $body .="Content-Type: $file_type; name=\"$file_name\"\r\n"; $body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n"; $body .="Content-Transfer-Encoding: base64\r\n"; $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; $body .= $encoded_content; $sentMail = @mail($recipient_email, $subject, $body, $headers); if($sentMail) //output success or failure messages { die('Thank you! Email sent.'); }else{ die('Could not send mail! Please check your PHP mail configuration.'); } }
That’s it, you can capture subject and message body from HTML form and use them in the code.
Please support us, Like us on Facebook.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment