Tuesday, November 4, 2014

Learning PHP (Sending email with attachement) - part 2

Hellow there

Lets see a script to send email with attachments. For this we will need to create a simple form in html which will allow user to upload files. Here is the html code for the form



form tag is used to submit some data to server by client. It has the attributes of action, where we specify the file to send the form data to, the method attricbute specifies the type used to submit data for e.g., get, post, put

get method is used to submit data which is okay for the client to see, and also we cannot send too much information using get method. Post methhod is used to submit information after encryption, we can submit a lot of information using post, put method is not commonly used in fact most servers and web hosts have blocked this method. This method allows users to upload their own files from any where without permission to server and there is more to put script than this which we will discuss later hopefully.

name attribute will simply allow us to refer to this form. This tag serves well when we do the form validation using java script which is missing here.

<!DOCTYPE html>
<html>
<head>
<title>sending email</title>
<body>
<form action="send_email.php"  method="post" name="info" >
                            <table>                          
                            <tr>
                                <td><label class="" skinpart="label_file">Upload file 1</label></td>
                                <td>
                                        <input name="selfpic" type="file">                   
                                </td>
                            </tr>
                            <tr>
                                <td><label class="" skinpart="label_file">Upload ID&#8217s picture</label></td>
                                <td>
                                        <input type="file" name="IDpic">
                                </td>
                            </tr>
                            <tr>
                                <td><label class="" skinpart="label_file">Upload your picture while holding ID</label></td>
                                <td>
                                      <input type="file" name="sidpic">
                                </td>                 
                            </tr>
                            <tr>                           
                                <td></td>
                                <td><input type="submit" name="submit" value="submit"></td>
                            </tr>
                            </table>
                        </form>
</body>
</html>

just save this html code in an html file.

The php script to send email  is given below
You can simply save this code in a php file and make sure the form action attribute contains the correct path to this file and you will be able to send emails successfully. I wanted to talk about variables and functions of php but I think we will talk about it in part 3 for now enjoy sending emails with this php script :)
                             
<?php


$success_message= 'Your Application has been submitted successfully. Click <a href="index.html">Here</a> to go back';
$failure_message="Sorry we couldn't submit your application due to system error";
$to = 'receiver@receiver.com';
$fromEmail = 'sender@send.com';
$fromName = 'SENDER NAME';
$subject = 'EMAIL SUBJECT';
$message = '<table><tr><td>Name:</td><td> '.$fromName.'</td></tr><tr><td> Email:</td><td>'.$fromEmail.'</td></tr></table>';

/* GET File Variables you can add as many files as you want here. You can make your form in such a way that you can loop through all files as well*/
$selfpic_tmpName = $_FILES['selfpic']['tmp_name'];
$selfpic_fileType = $_FILES['selfpic']['type'];
$selfpic_fileName = $_FILES['selfpic']['name'];

$IDpic_tmpName = $_FILES['IDpic']['tmp_name'];
$IDpic_fileType = $_FILES['IDpic']['type'];
$IDpic_fileName = $_FILES['IDpic']['name'];

$sidpic_tmpName = $_FILES['sidpic']['tmp_name'];
$sidpic_fileType = $_FILES['sidpic']['type'];
$sidpic_fileName = $_FILES['sidpic']['name'];
/* Start of headers */
$headers = "From: $fromName";


/* a boundary string */
  $randomVal = md5(time());
  $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";

  /* Header for File Attachment */
  $headers .= "\nMIME-Version: 1.0\n";
  $headers .= "Content-Type: multipart/mixed;\n" ;
  $headers .= " boundary=\"{$mimeBoundary}\"";
  /* Multipart Boundary above message */
  $message = "This is a multi-part message in MIME format.\n\n"."--{$mimeBoundary}\n"."Content-Type: text/html; charset=\"iso-8859-1\"\n"."Content-Transfer-Encoding: 7bit\n\n".$message . "\n\n";


if (file($selfpic_tmpName)) {
  /* Reading file ('rb' = read binary)  */
  $selfpic_file = fopen($selfpic_tmpName,'rb');
  $selfpic_data = fread($selfpic_file,filesize($selfpic_tmpName));
  fclose($selfpic_file);

  /* Encoding file data */
  $selfpic_data = chunk_split(base64_encode($selfpic_data));

  /* Adding attchment-file to message*/
  $message .= "--{$mimeBoundary}\n" ."Content-Type: {$selfpic_fileType};\n" ." name=\"{$selfpic_fileName}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$selfpic_data . "\n\n" ."{$mimeBoundary}\n";
}
if (file($IDpic_tmpName)) {
  /* Reading file ('rb' = read binary)  */
  $IDpic_file = fopen($IDpic_tmpName,'rb');
  $IDpic_data = fread($IDpic_file,filesize($IDpic_tmpName));
  fclose($IDpic_file);

  /* Encoding file data */
  $IDpic_data = chunk_split(base64_encode($IDpic_data));

  /* Adding attchment-file to message*/
  $message .= "--{$mimeBoundary}\n" ."Content-Type: {$IDpic_fileType};\n" ." name=\"{$IDpic_fileName}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$IDpic_data . "\n\n" ."{$mimeBoundary}\n";
}
if (file($sidpic_tmpName)) {
  /* Reading file ('rb' = read binary)  */
  $sidpic_file = fopen($sidpic_tmpName,'rb');
  $sidpic_data = fread($sidpic_file,filesize($sidpic_tmpName));
  fclose($sidpic_file);

  /* Encoding file data */
  $sidpic_data = chunk_split(base64_encode($sidpic_data));

  /* Adding attchment-file to message*/
  $message .= "--{$mimeBoundary}\n" ."Content-Type: {$sidpic_fileType};\n" ." name=\"{$sidpic_fileName}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$sidpic_data . "\n\n" ."--{$mimeBoundary}--\n";
}

$flgchk = mail ("$to", "$subject", "$message", "$headers");

if($flgchk){
  echo $success_message;
 }
else{
  echo $failure_message;
}
?>                  

Feel free to comment in case of any trouble or in case I missed something here, after all I am a human being ;) See you next time with another post soon.
                           
                           
                           

No comments:

Post a Comment