Results 1 to 7 of 7

Thread: Sending emails with attachments

  1. #1
    Join Date
    Aug 2013

    Sending emails with attachments

    Hi All,

    I am working on the project where there is the requirement pop up to send a mail with attachment, normal text emails are working fine without any issue. Please find my code below and let me know what i need to add to send attachment to work.


    <p> <strong><em>PERSON :</em></strong> <input type="text" id="outgoing" name="outgoing" placeholder="Enter Your Name" /> 
    &nbsp;&nbsp;<strong><em>  PERSON 2: </em></strong> <input type="text" id="incoming" name="incoming" autocomplete="on" placeholder="Next Person Name" /> &nbsp;&nbsp;
      <em><strong>COMMENTS :</strong></em> 
      <textarea name="shift_comments" id="shift_comments" style="width: 334px; height: 48px;" placeholder="Shift Comments" ></textarea>
     <label for='uploaded_file'>Select A File To Upload:</label> <br>
      <input type="file" name="uploaded_file">

    //sql insert code
    			$sql = mysql_query("INSERT INTO customer(DATE,OUTGOING,INCOMING,COMMENTS) VALUES('$date','$outgoing','$incoming','$shift_comments')")
    			or die ("Database Connection Error" .mysql_error());
    //email code
    						$subject="customer details of $date";
    						$message ="message body.";
                                                    $header = 'MIME-Version: 1.0' . "\r\n";
    						$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";			
    						$header .= "From: {$from}\r\n"; 
    						$header .= "Reply-To: {$to}\r\n"; 
    						$header .= "Return-path: " . $to; 	
     						mail( $to,  $subject, $message, $header, "-f{$from}");

    Please help me out what i need to do in my email.php script so that attachments can send properly.

  2. #2
    Join Date
    Jun 2014
    All your ans lies in the link

    read this carefully and you will get the idea what you want to do...........

  3. #3
    Join Date
    Aug 2013
    Thanks you , Let me check

  4. #4
    Join Date
    Aug 2013
    I tried the code and got little bit success into it, now the email with attachment arrives in my inbox, but the problem is that the attachment is empty but the size of attachment is not "zero" byte and when i open the attach its show nothing.

    Please find the code below.

    <title>Sending attachment using PHP</title>
      $to = "xyz@abc.com";
      $subject = "This is subject";
      $message = "This is test message.";
      # Open a file
      $file = fopen( "upload/test.txt", "r" );
      if( $file == false )
         echo "Error in opening file";
      # Read the file into a variable
      $size = filesize("upload/test.txt");
      $content = fread( $file, $size);
      # encode the data for safe transit
      # and insert \r\n after every 76 chars.
      $encoded_content = chunk_split( base64_encode($content));
      # Get a random 32 bit number using time() as seed.
      $num = md5( time() );
      # Define the main headers.
      $header = "From:xyz@somedomain.com\r\n";
      $header .= "MIME-Version: 1.0\r\n";
      $header .= "Content-Type: multipart/mixed; ";
      $header .= "boundary=$num\r\n";
      $header .= "--$num\r\n";
      # Define the message section
      $header .= "Content-Type: text/plain\r\n";
      $header .= "Content-Transfer-Encoding:8bit\r\n\n";
      $header .= "$message\r\n";
      $header .= "--$num\r\n";
      # Define the attachment section
      $header .= "Content-Type:  multipart/mixed; ";
      $header .= "name=\"test.txt\"\r\n";
      $header .= "Content-Transfer-Encoding:base64\r\n";
      $header .= "Content-Disposition:attachment; ";
      $header .= "filename=\"test.txt\"\r\n\n";
      $header .= "$encoded_content\r\n";
      $header .= "--$num--";
      # Send email now
      $retval = mail ( $to, $subject, "", $header );
      if( $retval == true )
          echo "Message sent successfully...";
          echo "Message could not be sent...";

  5. #5
    Join Date
    Mar 2007
    PHP Code:
    $retval mail $to$subject""$header ); 
    doesn't look right, should it be...
    PHP Code:
    $retval mail $to$subject$message$header ); 
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  6. #6
    Join Date
    Aug 2013
    After the changes the attachment is now opening but showing the content like NŠ^ž* ? what i did wrong.

  7. #7
    Join Date
    Aug 2013
    This chuck message in the attached file is because of wrong encoding ?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center

Recent Articles