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 .

code

 checkMail.php

<?php
    if(isset($_POST['submit']))
    {
        //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 = 'himmat.singh@gmail.co';
        $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 = "From:admin@example.com\r\nReply-To: admin@php999.com";
        $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

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

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

$message

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

$attachment
--_1_$boundary--";

        mail($to, $subject, $message, $headers);
    }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>MailFile</title>
</head>

<body>

<?php echo $output; ?>

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

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

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

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

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 comment:

  1. 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

    ReplyDelete

How to Host a Laravel Project on Hostinger’s hPanel: A Step-by-Step Guide

How to Host a Laravel Project on Hostinger’s hPanel: A Step-by-Step Guide If you're looking to host a Laravel project on Hostinger’s hPa...