Wednesday, January 8, 2014

PHP Mail with attachment file

Here i am discuss very important problem faced by many programer and web developer  
mail with attachment   
here you can solve this problem

1.create  checkMail.php
save it checkMail.php in server folder .



        //The form has been submitted, prep a nice thank you message
        $output = '<h1>Thanks for your file and message!</h1>';
        //Set the form flag to no display (cheap way!)
        $flags = 'style="display:none;"';

        //Deal with the email
        $to = '';
        $subject = 'Message';
                $name = $_POST['name'];
                $email_address = $_POST['email'];
                $message = $_POST['message'];

         $message = "\n\n Name: $name \n\n Email: $email_address \n\nMessage: \n\n $message \n\nHere is your file: ";

        $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
        $filename = $_FILES['file']['name'];

        $boundary =md5(date('r', time()));

        $headers = "\r\nReply-To:";
        $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

        $message="This is a multi-part message in MIME format.
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit


Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment


        mail($to, $subject, $message, $headers);
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">


<?php echo $output; ?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
<label for="message">Message</label>

 <textarea name="message" id="message" cols="20" rows="8"></textarea></p>
<label for="name">Name</label>  

<input type="text" name="name" id="name" ></p>
<label for="name">Name</label>  

<input type="text" name="email" id="name" ></p>
<label for="name">Name</label> <input type="text" name="contact" id="name" ></p>
<label for="file">File</label> <input type="file" name="file" id="file"></p>
<input type="submit" name="submit" id="submit" value="send"></p>

Hi! Thank you everyone who reading my article. My major is electronic and programming. Right now I am doing foreign study at India. I will like sharing to everyone with my works and if you do interesting with my works, please leave you comment on my blog. Any comments is welcoming.


  1. My friend Suggest me this blog and I can say this is the best blog to get the basic knowledge.Thank you so much for this Selenium Training in Chennai

  2. Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
    best java training in coimbatore
    php training in coimbatore
    best php training institutes in coimbatore


Related Posts Plugin for WordPress, Blogger...