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

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>"; 
} 
 
?>

how to implement the file names to a string and use

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

Member Avatar for rajarajan2017

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";
}

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

Member Avatar for rajarajan2017
$file = array();
array_push($file, $row[0]);

Hope you understand the logic!

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

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>
Member Avatar for rajarajan2017

The code is already in the second post.

hey raj i didnt undstand

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>"; 
} 
 
?>
Member Avatar for diafol

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 ...

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”);

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>"; 
} 
 
?>
Member Avatar for rajarajan2017

Thats great!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.