473,372 Members | 980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

How can i send the mail with db field attachment?

I am already send a mail using Php with some attachement into it. My Problem is the attachement file is in my Database(mysql). I don't know how can i get from database and then add to my mail.
Expand|Select|Wrap|Line Numbers
  1. <?php  function send_mail($emailaddress, $fromaddress, $emailsubject, $body, $ccaddress, $attachments=false)  {   
  2.  $eol="\r\n";  
  3.   $mime_boundary=md5(time());            # Common Headers    
  4.   $headers .= 'From: Sangeetha<'.$fromaddress.'>'.$eol;    
  5. $headers .= 'Cc: <'.$ccaddress.'>'.$eol;       
  6.  $headers .= "Message-ID: <".$now."TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;   
  7.  $headers .= "X-Mailer: PHP v".phpversion().$eol;          // These two to help avoid spam-filters    
  8.   # Boundry for marking the split & Multitype Headers 
  9.    $headers .= 'MIME-Version: 1.0'.$eol;  
  10.   $headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;      
  11.   $msg = "";           
  12.    if ($attachments !== false)    {   
  13.      for($i=0; $i < count($attachments); $i++)      { 
  14.        if (is_file($attachments[$i]["file"]))        {             # File for Attachment       
  15. $file_name = substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/")+1));                 
  16. $handle=fopen($attachments[$i]["file"], 'rb');         
  17. $f_contents=fread($handle, filesize($attachments[$i]["file"]));          $f_contents=chunk_split(base64_encode($f_contents));    //Encode The Data For Transition using base64_encode();          
  18. fclose($handle);                    # Attachment          
  19. $msg .= "--".$mime_boundary.$eol;          
  20. $msg .= "Content-Type: ".$attachments[$i]["content_type"]."; name=\"".$file_name."\"".$eol;          
  21. $msg .= "Content-Transfer-Encoding: base64".$eol;          
  22. $msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!          
  23. $msg .= $f_contents.$eol.$eol;               
  24.    }    
  25.   }    
  26. }        # Setup for text OR html    
  27. $msg .= "Content-Type: multipart/alternative".$eol;        # Text Version    
  28. $msg .= "--".$mime_boundary.$eol;    
  29. $msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;    
  30. $msg .= "Content-Transfer-Encoding: 8bit".$eol;    
  31. $msg .= strip_tags(str_replace("<br>", "\n", $body)).$eol.$eol;        # HTML Version    
  32. $msg .= "--".$mime_boundary.$eol;    
  33. $msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;    
  34. $msg .= "Content-Transfer-Encoding: 8bit".$eol;    
  35. $msg .= $body.$eol.$eol;        # Finished    
  36. $msg .= "--".$mime_boundary."--".$eol.$eol;  // finish with two eol's for better security. see Injection.         
  37.  # SEND THE EMAIL    ini_set(sendmail_from,$fromaddress);  // the INI lines are to force the From Address to be used !    
  38. mail($emailaddress, $emailsubject, $msg, $headers);    ini_restore(sendmail_from);   
  39.  echo "mail send";  }    
  40. $emailaddress = "xxx@xx.com";  
  41. $fromaddress = "yyy@yahoo.com";  
  42. $ccaddress = "zzz@yahoo.com";    
  43. $emailsubject = "Test";    $link = mysql_connect("myConnection") or die                        ("Error connecting to mysql:");    mysql_select_db("index",$link) or die("Unable to select database: . mysql_error()");   
  44.  //$commid = $_POST["CommId"];  
  45. $selectSql = "select CommId, Attachment from COMM_EML_ATTACHMENT where CommId = 4167";    $
  46. result = mysql_query($selectSql,$link); 
  47.  while($row = mysql_fetch_array($result))  { 
  48.  $commid = $row["CommId"];  # Use relative paths to the attachments    $attachments = Array(    Array("file"=> $row["Attachment"],"content_type"=>"application/msword"));  
  50. # Message Body  $body="This is a test message! ";   
  52. $result = send_mail($emailaddress, $fromaddress, $emailsubject, $body, $ccaddress, $attachments);  }  
  53. mysql_close($link);  ?>
please help to solve it

Sep 20 '07 #1
1 2390
5,821 Expert 4TB
Heya, Chitu03. Welcome to TSDN!

You can retrieve your file from the database the same way you would retrieve any other data. MySQL and PHP simply treat the file as a really long binary string.

[EDIT: Ah. I see that the framework you are using requires a file path. Try using file_put_contents() to create a temporary file.]
Sep 20 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Jammer | last post by:
Can someone give me some sample code to use CDONTS.new to send email with an attachment? I've got sample code from the following website; ...
by: Robbie De Sutter | last post by:
Hello, How do I open a new, empty e-mail message from the default e-mail client whereby the sender is given and a file is attached? Currently I use the command (vb.net): ---...
by: m_evergreen | last post by:
Errors: An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll Additional information: Could not access 'CDO.Message' object. innerexception is "Exception has...
by: Mike Charney | last post by:
Is there a simple way to send SMTP email from Access VBA? Mike m charney at dunlap hospital dot org
by: Cimento Cola | last post by:
Hello all. Hope anyone can help me! I have this form, with: Please choose your file: (browse) The main purpose is the user to select a local file and then when he submits a mail should be...
by: =?Utf-8?B?SHVnaA==?= | last post by:
Hi There, I use follow code to send email inside VB.NET 2005. It does not work well. Error message of "Failure sending email" would occue. However, email was sent out sometimes. I am confused...
by: maxxxxel | last post by:
Hi Can anyone help me with some asp code , I changed the code to use CDO.message instead of the old cdont.sys to send mail from a ASP webpage which works fine. Our problem is that when we send...
by: sxwend | last post by:
I am trying to use the following post results (http://www.thescripts.com/forum/thread189759.html) and add another requirement. I need to send the results to just the email addresses that the query...
by: deepaks85 | last post by:
Dear All, I want to send some data through a form with Multiple attachment in an HTML Format. I have tried it but it is not working for me. I am able to send data without attachment but with the...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.