6
Contributors
17
Replies
27
Views
7 Years
Discussion Span
Last Post by rajarajan07
-1

Is there a way to attach multiple files to email using php?

yes. Enjoy and send mails with any number of attachments without any class/library.

<?php
 
// array with filenames to be sent as attachment
$files = array("file_1.ext","file_2.ext","file_3.ext",......);
 
// email fields: to, from, subject, and so on
$to = "mail@mail.com";
$from = "mail@mail.com"; 
$subject ="My subject"; 
$message = "My message";
$headers = "From: $from";
 
// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
 
// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
 
// multipart boundary 
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$message .= "--{$mime_boundary}\n";
 
// preparing attachments
for($x=0;$x<count($files);$x++){
	$file = fopen($files[$x],"rb");
	$data = fread($file,filesize($files[$x]));
	fclose($file);
	$data = chunk_split(base64_encode($data));
	$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
	"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
	$message .= "--{$mime_boundary}\n";
}
 
// send
 
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
	echo "<p>mail sent to $to!</p>"; 
} else { 
	echo "<p>mail could not be sent!</p>"; 
} 
 
?>
0

i have stored path and file name in datbase. how can i send 2 files attach using above script

0

Hi vipi432,

$files is an array, instead of that you stored in a database, get the value from the database and stored it in an array like same $files. Thats it!. Whatever length(no of items) you have in array will become as an attachment by using the below code.

// preparing attachments
for($x=0;$x<count($files);$x++){
	$file = fopen($files[$x],"rb");
	$data = fread($file,filesize($files[$x]));
	fclose($file);
	$data = chunk_split(base64_encode($data));
	$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
	"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
	$message .= "--{$mime_boundary}\n";
}

Edited by rajarajan07: n/a

0

hi raj ! thanks for replay. i hv took values from database and inside a for loop i put double quotes and comma in the values. bt hw can i put it in the array $file

0

thanks u raj.can u send the ful code. i used array_push for putting data in a page. u used to send mail in the second page

0

raj dis is my page

<?
    session_start();  
    include("dbconn.inc");
    include("dbfuc.inc");
    ?>
    <form id="form1" name="form1" method="post" action="multi.php">

<?

    $loop=1;$ttcnt=3;$lscnt=3;$cntnum=0;$rcnt=2;$stack=array();
     $sql="select custid,cvfilename from customer where custid in (333,370) order by custid  ";
            $rs=mysql_query($sql);
            while ($adatac=mysql_fetch_array ($rs)){
            $cntnum++;
             $list[$loop]="cv_upload/".$cvfilename=$adatac["cvfilename"];
            $cvfilename=$adatac["cvfilename"];
             $cvpath="cv_upload/".$cvfilename;
            $loop++;
             $mlist=count($list);

        }
                  $cm=$mlist+1;
                            for($cnt=1;$cnt<=$mlist;$cnt++){
                            if($cm-$cnt==1){ $com="";
                            }else {
                            $com=",";
                            }       
                            $qt="&rdquo;";
                            $lqt="&ldquo;";
                             $cma="'";
                             $cvdat =$list[$cnt];       
                             $cdt="\"$cvdat\"$com";         
                             array_push($stack,$cdt);

                            }
                             $pusha=join("",$stack);

                            echo "<input type='hidden' name='atlist' value='$pusha'/>";
                            ?>

                             <input type="submit" name="Submit" value="Submit" />
</form>

Edited by mike_2000_17: Fixed formatting

0

hi raj gud mng

pls check the below name of files in array like file_1.txt etc are stored in my database . hw can i use it in this program . if u dnt mind pls send me

// array with filenames to be sent as attachment
$files = array("file_1.ext","file_2.ext","file_3.ext",......);
 
// email fields: to, from, subject, and so on
$to = "mail@mail.com";
$from = "mail@mail.com"; 
$subject ="My subject"; 
$message = "My message";
$headers = "From: $from";
 
// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
 
// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
 
// multipart boundary 
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$message .= "--{$mime_boundary}\n";
 
// preparing attachments
for($x=0;$x<count($files);$x++){
	$file = fopen($files[$x],"rb");
	$data = fread($file,filesize($files[$x]));
	fclose($file);
	$data = chunk_split(base64_encode($data));
	$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
	"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
	$message .= "--{$mime_boundary}\n";
}
 
// send
 
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
	echo "<p>mail sent to $to!</p>"; 
} else { 
	echo "<p>mail could not be sent!</p>"; 
} 
 
?>

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

0

Ho ho ho - Raj - you got one lazy so-and-so here - won't do or try anything himself. Well, serves you right for giving code without telling them to follow guidlines. Well, murali's fault I suppose. Feed a dog ...

0

can anybody help me .

i have stored file_1.ext,file_2.ext,file_3.ext in my database.
can i use the values in the below code

array(“file_1.ext”,”file_2.ext”,”file_3.ext”);

0

Hi guys this code is useful for attach files which filenames are stored in database

check it

<?php
include("dbconn.inc");
$to = "vipi432@yahoo.com";
$from = "vipi432@yahoo.com"; 
$subject ="My subject"; 
$message = "My message";
$headers = "From: $from";
 
// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
 
// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
 
// multipart boundary 
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$message .= "--{$mime_boundary}\n";
 
// retreive datas from dataabase 
			$stack=array();
			$sql="select filename from table";
			$rs=mysql_query($sql);
			while ($vipi=mysql_fetch_array ($rs)){
			$x++;
			 $cvfilename=$vipi["filename"];
			 $cvpath="cv_upload/".$cvfilename;//cv_upload is the folder name
			 array_push($stack,$cvpath);
			 }
		
	// preparing attachments
	for($x=0;$x<count($stack);$x++){
	$file = fopen($stack[$x],"rb");
	$data = fread($file,filesize($stack[$x]));
	fclose($file);
	$data = chunk_split(base64_encode($data));
	$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$stack[$x]\"\n" . 
	"Content-Disposition: attachment;\n" . " filename=\"$stack[$x]\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
	$message .= "--{$mime_boundary}\n";
}

// send
 
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
	echo "<p>mail sent to $to!</p>"; 
} else { 
	echo "<p>mail could not be sent!</p>"; 
} 
 
?>

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.