|
Hi
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. -
<?php function send_mail($emailaddress, $fromaddress, $emailsubject, $body, $ccaddress, $attachments=false) {
-
$eol="\r\n";
-
$mime_boundary=md5(time()); # Common Headers
-
$headers .= 'From: Sangeetha<'.$fromaddress.'>'.$eol;
-
$headers .= 'Cc: <'.$ccaddress.'>'.$eol;
-
$headers .= "Message-ID: <".$now."TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
-
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters
-
# Boundry for marking the split & Multitype Headers
-
$headers .= 'MIME-Version: 1.0'.$eol;
-
$headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
-
$msg = "";
-
if ($attachments !== false) {
-
for($i=0; $i < count($attachments); $i++) {
-
if (is_file($attachments[$i]["file"])) { # File for Attachment
-
$file_name = substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/")+1));
-
$handle=fopen($attachments[$i]["file"], 'rb');
-
$f_contents=fread($handle, filesize($attachments[$i]["file"])); $f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode();
-
fclose($handle); # Attachment
-
$msg .= "--".$mime_boundary.$eol;
-
$msg .= "Content-Type: ".$attachments[$i]["content_type"]."; name=\"".$file_name."\"".$eol;
-
$msg .= "Content-Transfer-Encoding: base64".$eol;
-
$msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
-
$msg .= $f_contents.$eol.$eol;
-
}
-
}
-
} # Setup for text OR html
-
$msg .= "Content-Type: multipart/alternative".$eol; # Text Version
-
$msg .= "--".$mime_boundary.$eol;
-
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
-
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
-
$msg .= strip_tags(str_replace("<br>", "\n", $body)).$eol.$eol; # HTML Version
-
$msg .= "--".$mime_boundary.$eol;
-
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
-
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
-
$msg .= $body.$eol.$eol; # Finished
-
$msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection.
-
# SEND THE EMAIL ini_set(sendmail_from,$fromaddress); // the INI lines are to force the From Address to be used !
-
mail($emailaddress, $emailsubject, $msg, $headers); ini_restore(sendmail_from);
-
echo "mail send"; }
-
$emailaddress = "xxx@xx.com";
-
$fromaddress = "yyy@yahoo.com";
-
$ccaddress = "zzz@yahoo.com";
-
$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()");
-
//$commid = $_POST["CommId"];
-
$selectSql = "select CommId, Attachment from COMM_EML_ATTACHMENT where CommId = 4167"; $
-
result = mysql_query($selectSql,$link);
-
while($row = mysql_fetch_array($result)) {
-
$commid = $row["CommId"]; # Use relative paths to the attachments $attachments = Array( Array("file"=> $row["Attachment"],"content_type"=>"application/msword"));
-
-
# Message Body $body="This is a test message! ";
-
-
$result = send_mail($emailaddress, $fromaddress, $emailsubject, $body, $ccaddress, $attachments); }
-
mysql_close($link); ?>
please help to solve it
Thanks
Chitu.
| |