www.webdeveloper.com
Results 1 to 7 of 7

Thread: Sending emails with attachments

  1. #1
    Join Date
    Aug 2013
    Posts
    40

    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.


    index.php


    Code:
    <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>
      &nbsp;&nbsp;
     
     <label for='uploaded_file'>Select A File To Upload:</label> <br>
      <input type="file" name="uploaded_file">
    
      </p>
    </fieldset>
    email.php

    Code:
                                        
    
    //sql insert code
                             $outgoing=htmlspecialchars($_POST['outgoing']);
    
    			$incoming=htmlspecialchars($_POST['incoming']);
    
    	 		$shift_comments=htmlspecialchars($_POST['shift_comments']);
    
    			$shift_comments=mysql_real_escape_string($shift_comments);
    
    		
    
    			$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
                                                    $from="customer@abc.com";
    
    						$to="xyz@abc.com";
    
    						$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
    Posts
    27
    All your ans lies in the link
    http://www.tutorialspoint.com/php/ph...ing_emails.htm

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

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

  4. #4
    Join Date
    Aug 2013
    Posts
    40
    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.

    Code:
    <html>
    <head>
    <title>Sending attachment using PHP</title>
    </head>
    <body>
    <?php
    
      $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";
         exit();
      }
      # 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...";
       }
       else
       {
          echo "Message could not be sent...";
       }
    ?>
    </body>
    </html>

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,206
    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
    Posts
    40
    After the changes the attachment is now opening but showing the content like NŠ^ž* ? what i did wrong.

  7. #7
    Join Date
    Aug 2013
    Posts
    40
    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