Results 1 to 3 of 3

Thread: Troubble with sending email including attachement

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Troubble with sending email including attachement

    Hi all,

    I have saved a some data and a file in my Database. Now I want to send this data and file via Email.

    I have successfully save data in database,
    I have successfully retrieve that data from Database.
    I have successfully sent an email BUT without attachement.

    I am facing a problem when it comes to send an email with attachment. this attachement file can be of any type.

    My Code is Below. (addsendcampaign.php)
    =======================================================

    Code:
    $sql2 = "Select * from emailtemplate WHERE ID=$emailmessageID";
    			$rs2 = mysql_query($sql2);
    			if(mysql_num_rows($rs2)>0)
    			{	
    				while($obj2=mysql_fetch_object($rs2)) // Getting the Email Message Information
    				{
    					$Subject = $obj2->Subject;
    					$Message = $obj2->Message;
    					$FromAddress = $obj2->FromAddress;
    					
    					$AttFile = $obj2->filedata;// this field contains the Data of the File
    					
    					$size = @MYSQL_RESULT($rs2,0,"filesize");
    					$data = @MYSQL_RESULT($rs2,0,"filedata");
    					$type = @MYSQL_RESULT($rs2,0,"filetype");
    					$filename = @MYSQL_RESULT($rs2,0,"filename");
    																			
    					$content = str_replace(chr(10), " ", $Message);
    					$content = str_replace(chr(13), " ", $Message);
    					// Replace single quotes.
    					$content = str_replace(chr(145), chr(39), $Message);
    					$content = str_replace(chr(146), chr(39), $Message);
    					$content = "Dear ".$fname." ".$lname."<br />".$Message;					
    										
    					//=============== Attach a File =======================
    										
    					//create a boundary string. It must be unique 
    					$headers .= 'From:'.$FromAddress."\r\n";
    					//$random_hash = md5(date('r', time())); 
    					$semi_rand = md5(time()); 
    					$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
    					
    					$headers .= "\nMIME-Version: 1.0\n"."Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
    					
    					$message = "\n\n" ."--{$mime_boundary}\n" ."Content-Type: text/html; charset=\"iso-8859-1\"\n" ."Content-Transfer-Encoding: 7bit\n\n" .$content . "\n\n";
    					
    					$attachment = chunk_split(base64_encode($data)); 
    					
    					$message .= "--{$mime_boundary}\n" ."Content-Type: {$type};\n" ." name=\"{$filename}\"\n";
    					$message .= "Content-Disposition: attachment;\n" ." filename=\"{$filename}\"\n";
    					$message .= "Content-Transfer-Encoding: base64\n\n".$attachment."\n\n" ."--{$mime_boundary}--\n";
    					
    					//======================================================		
    					//=======================================================
    										
    				}
    			}
    			
    			echo "Header : ".$headers."<br />";
    			echo "Message : ".$message."<br />";
    						
    			if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$to))  // Checking if the Email Address is Correct
    			{
    				if(mail($to, $Subject, $message ,$headers)) // Sending Email
    				{
    					echo "To : ".$to."<br />";
    					echo "Subject : ".$subject."<br />";					
    					echo "Email Sent!!!!!!!! <br />";
    				}
    				else
    				{
    					echo "Email Not Sent !!!!!!!!!!<br />";
    				}
    			}
    			else
    			{	
    				echo "Email Address Was Wrong!!!";
    			}
    ===================================================

    The Error I received is
    =====================================
    Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent
    ============================================


    I hope someone can help me soon. I will be very thankful to you all

    Thanks
    Last edited by mfaisalnice; Sep 15th, 2009 at 03:59 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width