Send an Email with Multiple Attachments in PHP

The below PHP example helps you to send an email with multiple attachment file.

sendmail.html



<table width="90%" border="0" cellpadding="0" cellspacing="0" >
  <tr><td align="center" valign="top">&nbsp;</td></tr>
<tr>
    <td align="center" valign="top"><form action="sendmail.php" method="post" name="rfqfrm" id="rfqfrm" enctype="multipart/form-data">
        <table width="500" border="0" cellspacing="0" cellpadding="6">
          <tr>
            <td width="150" align="right" valign="middle">* First Name:</td>
            <td align="left" valign="middle"><input name="name" type="text" id="name" size="25"></td>
          </tr>
          <tr>
            <td width="150" align="right" valign="middle">* Last Name: </td>
            <td align="left" valign="middle"><input name="lastname" type="text" id="lastname" size="25"></td>
          </tr>
          <tr>
            <td width="150" align="right" valign="middle">* Email:</td>
            <td align="left" valign="middle"><input name="email" type="text" id="email" size="30"></td>
          </tr>
          <tr>
            <td width="150" align="right" valign="middle">Company Name: </td>
            <td align="left" valign="middle"><input name="cmname" type="text" id="cmname" size="15"></td>
          </tr>
          <tr>
            <td width="150" align="right" valign="middle">Address:</td>
            <td align="left" valign="middle"><input name="add" type="text" id="add" size="35"></td>
          </tr>
          <tr>
            <td width="150" align="right" valign="middle">City:</td>
            <td align="left" valign="middle"><input name="city" type="text" id="city" size="30"></td>
          </tr>
          <tr>
            <td width="150" align="right" valign="middle">State:</td>
            <td align="left" valign="middle"><table width="300" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="50"><input name="state" type="text" id="state" size="2"></td>
                  <td width="50" align="right">Zip: &nbsp;&nbsp;&nbsp;</td>
                  <td width="200"><input name="zip" type="text" id="zip" size="10"></td>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td width="150" align="right" valign="middle">Phone:</td>
            <td align="left" valign="middle"><input name="phone" type="text" id="phone"></td>
          </tr>
          <tr>
            <td width="150" align="right" valign="middle">Fax:</td>
            <td align="left" valign="middle"><input name="fax" type="text" id="fax"></td>
          </tr>
          <tr>
            <td width="150" align="right" valign="middle">Attach File1:</td>
            <td align="left" valign="middle"><input type="file" name="attachFile"></td>
          </tr>
          <tr>
            <td width="150" align="right" valign="middle">Attach File2:</td>
            <td align="left" valign="middle"><input type="file" name="attachFile2"></td>
          </tr>
          <tr>
            <td width="150" align="right" valign="middle">Attach File3:</td>
            <td align="left" valign="middle"><input type="file" name="attachFile3"></td>
          </tr>
          <tr>
            <td width="150" align="right" valign="middle">Attach File4:</td>
            <td align="left" valign="middle"><input type="file" name="attachFile4"></td>
          </tr>
          <tr>
            <td align="right" valign="middle">Attach File5:</td>
            <td align="left" valign="middle"><input type="file" name="attachFile5"></td>
          </tr>
          <tr>
            <td width="150" align="right" valign="top">* Please describe your project needs: </td>
            <td align="left" valign="middle"><textarea name="comments" cols="30" rows="7" id="comments"></textarea></td>
          </tr>
          <tr>
            <td align="right" valign="middle">&nbsp;</td>
            <td align="left" valign="middle"><input type="reset" name="Reset" value="Clear">
              <input type="submit" name="Submit2" value="Submit"></td>
          </tr>
        </table>
      </form></td>
  </tr>
</table>

sendmail.php

<?php
$to = "emailaddress@gmail.com";
$subject= "Request For Quote from Latva";
$todayis = date("l, F j, Y, g:i a") ;

$subject= "Request For Quote from WWW.JEEVANMUKTI.INF0";
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$cmname = $_POST['cmname'];
$add = $_POST['add'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$comments = $_POST['comments'];
$message = "
Date ------- $todayis
Name ------ $name
Last Name ------- $lastname
Email ----------- $email
Company --------- $cmname
Address --------- $add
City ------------ $city
State ----------- $state
Zip ------------- $zip
Phone ----------- $phone
Fax ------------- $fax
Message --------- $comments
";
  $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
         $headers = "From: $email\r\n" .
         "MIME-Version: 1.0\r\n" .
            "Content-Type: multipart/mixed;\r\n" .
            " boundary=\"{$mime_boundary}\"";
         $message = "This is a multi-part message in MIME format.\n\n" .
            "--{$mime_boundary}\n" .
            "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";
         foreach($_FILES as $userfile)
         {
            $tmp_name = $userfile['tmp_name'];
            $type = $userfile['type'];
            $name = $userfile['name'];
            $size = $userfile['size'];
            if (file_exists($tmp_name))
            {
               if(is_uploaded_file($tmp_name))
               {
                  $file = fopen($tmp_name,'rb');
                  $data = fread($file,filesize($tmp_name));
                  fclose($file);
                  $data = chunk_split(base64_encode($data));
               }
               $message .= "--{$mime_boundary}\n" .
                  "Content-Type: {$type};\n" .
                  " name=\"{$name}\"\n" .
                  "Content-Disposition: attachment;\n" .
                  " filename=\"{$fileatt_name}\"\n" .
                  "Content-Transfer-Encoding: base64\n\n" .
               $data . "\n\n";
            }
         }
         $message.="--{$mime_boundary}--\n";
if (mail($to, $subject, $message, $headers))
   echo "Mail sent successfully.";
else
   echo "Error in mail";
?>

21 comments:

  1. hello brother
    please add little bit code it's working fine
    --------------------------- adding code -------------
    $to = "ravi.city@gmail.com";
    $subject= "Request For Quote from WWW.JEEVANMUKTI.INF0";
    $name = $_POST['name'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $cmname = $_POST['cmname'];
    $add = $_POST['add'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zip = $_POST['zip'];
    $phone = $_POST['phone'];
    $fax = $_POST['fax'];
    $comments = $_POST['comments'];

    - - - - - - - - - - - - - - end ------------
    ------------full code ------it's working nice ------please check it


    enjoy

    ReplyDelete
    Replies
    1. Hi Ravindra ,
      Please check this. i am getting error.

      Delete
    2. Hi
      -----------------
      please try this code it is working for me.
      -----------------------------
      $todayis = date("l, F j, Y, g:i a") ;
      $name = $_REQUEST["name"];
      $lastname = $_REQUEST["lastname"];
      $email = $_REQUEST["email"];
      $cmname = $_REQUEST["cmname"];
      $add = $_REQUEST["add"];
      $city = $_REQUEST["city"];
      $state = $_REQUEST["state"];
      $zip = $_REQUEST["zip"];
      $phone = $_REQUEST["phone"];
      $fax = $_REQUEST["fax"];
      $comments = $_REQUEST["comments"];
      $message = "
      Date : $todayis
      Name : $name
      Last Name : $lastname
      Email : $email
      Company : $cmname
      Address : $add
      City : $city
      State : $state
      Zip : $zip
      Phone : $phone
      Fax : $fax
      Message : $comments
      ";

      Delete
  2. How Can I set a size limitation and specify the type of files to upload. eg. only jpeg and pdf

    ReplyDelete
  3. Hi check this here is much better running code .. http://phpsollutions.blogspot.com/2014/02/send-multiple-attachment-in-mail-using.html

    ReplyDelete
  4. I got this error after i hit the submit button. any idea why? i upload the sendmail.php file in php folder & sendmail.html. thanks a mill!! "The requested URL /sendmail.php was not found on this server."

    ReplyDelete
  5. it works great that code, the one who wrote it thanks so much a lot !!,

    ReplyDelete
  6. Attachment is sending except data but why? where is the problem?

    ReplyDelete
  7. code working....... thank you.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. pleas sir can you send the the complit code the forms in html varables and the php script please sir

    ReplyDelete
  10. not working


    text/x-generic error_log
    ASCII text

    [15-Apr-2016 00:37:35 Etc/GMT] PHP Notice: Undefined variable: fileatt_name in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 46
    [15-Apr-2016 00:41:56 Etc/GMT] PHP Notice: Undefined index: name in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 6
    [15-Apr-2016 00:41:56 Etc/GMT] PHP Notice: Undefined index: lastname in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 7
    [15-Apr-2016 00:41:56 Etc/GMT] PHP Notice: Undefined index: email in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 8
    [15-Apr-2016 00:41:56 Etc/GMT] PHP Notice: Undefined index: cmname in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 9
    [15-Apr-2016 00:41:56 Etc/GMT] PHP Notice: Undefined index: add in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 10
    [15-Apr-2016 00:41:56 Etc/GMT] PHP Notice: Undefined index: city in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 11
    [15-Apr-2016 00:41:56 Etc/GMT] PHP Notice: Undefined index: state in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 12
    [15-Apr-2016 00:41:56 Etc/GMT] PHP Notice: Undefined index: zip in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 13
    [15-Apr-2016 00:41:56 Etc/GMT] PHP Notice: Undefined index: phone in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 14
    [15-Apr-2016 00:41:56 Etc/GMT] PHP Notice: Undefined index: fax in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 15
    [15-Apr-2016 00:41:56 Etc/GMT] PHP Notice: Undefined index: comments in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 16
    [15-Apr-2016 00:41:56 Etc/GMT] PHP Notice: Undefined index: attachment in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 17
    [15-Apr-2016 00:42:40 Etc/GMT] PHP Notice: Undefined index: attachment in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 17
    [15-Apr-2016 00:46:59 Etc/GMT] PHP Notice: Undefined index: attachment in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 17
    [15-Apr-2016 00:49:34 Etc/GMT] PHP Notice: Undefined index: attachment in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 17
    [15-Apr-2016 00:53:06 Etc/GMT] PHP Notice: Undefined index: attachment in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 17
    [15-Apr-2016 00:54:03 Etc/GMT] PHP Notice: Undefined index: attachment in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 17
    [15-Apr-2016 00:57:17 Etc/GMT] PHP Notice: Undefined variable: fileatt_name in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 58
    [15-Apr-2016 00:57:38 Etc/GMT] PHP Notice: Undefined variable: fileatt_name in /home/ktkdexpg/public_html/citep.in/mail/sendmail.php on line 58

    ReplyDelete
    Replies
    1. Kindly add below content in sendmail.php file
      $to = "your mail@gmail.com";
      $subject= "Request For Quote from WWW.JEEVANMUKTI.INF0";
      $name = $_POST['name'];
      $lastname = $_POST['lastname'];
      $email = $_POST['email'];
      $cmname = $_POST['cmname'];
      $add = $_POST['add'];
      $city = $_POST['city'];
      $state = $_POST['state'];
      $zip = $_POST['zip'];
      $phone = $_POST['phone'];
      $fax = $_POST['fax'];
      $comments = $_POST['comments'];

      Delete
  11. thousands thanks. It works perfect. very good job.

    ReplyDelete
  12. thanks thanks thanks alot u don't know Your Artical is V.harmfully for me thanks again alot

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...