0

recently I have created email sender using ProjectPier project with PHP.I am using my Gmail SMTP server information to send email. This script works perfectly.

I want to add file attachment in my php mail sender script

. So how can I do that?

<?php
$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: $_POST[name] <$_POST[email]>";
$headers[] = $_POST['name'];
$headers[] = $_POST['email'];
$subject = $_POST['subject'];
$body = $_POST['body'];

if(mail("nur_selam@yahoo.com",$subject,$body, implode("\r\n", $headers))){
echo "your email has been sent.";
}
else{
echo "email sending failed.";
}
?>
usminuru
  • 335
  • 4
  • 8
  • 19

2 Answers2

0

I have used the below code to send an attachment image.. You can try this one.

            <?php


            if (isset($_POST['submit'])) {

            $headers = "From: abc@example.com  mailer \r\n";
            $message .= $_POST['message'];  
                /* GET File Variables */ 
            $tmpName = $_FILES['attachment']['tmp_name']; 
            $fileType = $_FILES['attachment']['type']; 
            $fileName = $_FILES['attachment']['name']; 

            if (file_exists($tmpName)) { 
              /* Reading file ('rb' = read binary)  */

              $file = fopen($tmpName,'rb'); 
              $data = fread($file,filesize($tmpName)); 
              fclose($file); 

              /* 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/plain; charset=\"iso-8859-1\"\n" . 
              "Content-Transfer-Encoding: 7bit\n\n" . 
              $message . "\n\n"; 

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

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

            } 

            $to_user = "your emailid";



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


            //require_once ('./includes/functions_mail.php');

                if ($flgchk) {

                echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"2;url=forwardtoother.php?page=inbox.php\">";
                echo "Done, <br>";
                echo "Redirecting you to your <a href=\"?page=inbox.php\">Inbox</a>";
                }
                else
                {
                     echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"2;url=forwardtoother.php?page=inbox.php\">";
                echo "Failed to send email, <br>";
                echo "Redirecting you to your <a href=\"?page=inbox.php\">Inbox</a>";
                }

            } 

            ?>
            <form name="form1" method="post" action="" enctype="multipart/form-data">
            <table>
                <tr>
                <td width='20'><b>Select File:</b> </td>
                <td width='20'><input type="file" name="attachment"></td>
                </tr>
              <p><td width='20'>
                 <input type="reset" name="Submit2" value="Start Over." class="Submit"/></td><td>
                 <input type="submit" name="Submit" value="Send" class="Submit" /></td></tr></table>
              </p>
            </form>
Phani
  • 549
  • 3
  • 8
  • 22
  • Thanks for help. the attachement sent but in my Inbox the attachment file is like: This is a multi-part message in MIME format. --==Multipart_Boundary_x619a172d2d9bc6e01a88f62cc7cf26adx Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit test --==Multipart_Boundary_x619a172d2d9bc6e01a88f62cc7cf26adx Content-Type: image/jpeg; name="23.jpg" Content-Transfer-Encoding: base64 /9j/4AAQSkZJRgABAQEAYABgAAD/4QC8RXhpZgAASUkqAF4AAAAEABoBBQABAAAAPgAAABsBBQAB – usminuru Jul 14 '14 at 09:14
-1

Take a look at this you can upload file using this and use header to make it as attachment How to upload & Save Files with Desired name

Community
  • 1
  • 1
  • Welcome to Stack Overflow! Link-only answers are strongly discouraged here at Stack Overflow. Instead, [it is preferable](http://meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – drs Jul 14 '14 at 14:49