We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,965 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP Array $files email attachments

Im stuck now it is day four, I have a bunch of clients that upload documents to the web server, the code works if I maually type the names of the documents into the array, but that is not what I want, Mysql has table called "transact" with a field "documents" that the document name is stored under.

I pull the email address already from the tabel, It sends the email perfectly but the attachements are suposed to be two files, it is now one file but the name of that file is one long name like 1234567.pdf2341234556.pdf

Adobe does not open it, so I guess there is a probem with my arry assigning or something, In line 35 and 36 I have tried using "; " and ", " and even spaces but nothing works, it is something stupid in my array im doing I know, can someone point me in teh right direction

My maual Array looks like this and it works perfectly I get the files:

$files = array("1003020122510160249.pdf","1003020122510160248.pdf");

But I want it form MYSQL here is my code:

<?php
include_once "db_conf.php";

// GET PREVIOUS DATA FROM PAGE
$clientid = $_GET['id'];
$emailname = $_POST['mails'];
$mailbody = $_POST['comments'];

        //SEARCH FOR EMAIL ADDRESS IN MAIL TABLE
        $sql="SELECT * FROM mails where name='$emailname'"; 
        $result=mysql_query($sql); 

        //CLEAR THE OPTIONS
        $options=""; 

        while ($row=mysql_fetch_array($result)) { 
            $id=$row["id"]; 
            $name=$row["name"]; 
            $tomail=$row["email"]; 
        } 


// RETRIEVE DOCUMENT NAMES THAT NEED TO BE SENT AS ATTACHMENTS
        $query = "SELECT document FROM transact WHERE clientid=$clientid";
        $result = mysql_query($query) or die (mysql_error());
        while ($record = mysql_fetch_array($result)) {

//ARRAY DATA
        $arr[] = $record['document'];


    }

//DOCUMENT NAME ARRAY
    $docz = implode("; ",$arr);
    $rray = ''.$docz.'';

//ASSIGN ARRAY NAMES TO FILES TO BE SENT
    //$files = $rray;
    $files = array($rray);
    $filecount=count($files);



// email fields: to, from, subject, and so on   
$to = $tomail;
$from = "info@email.com"; 
$subject ="Query RE: $clientid "; 
$message = $mailbody;
$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 MY EMAILS WITH THE ATTACHEMNTS
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
    echo "<p>mail sent to $to!</p>"; 
} else { 
    echo "<p>mail could not be sent!</p>"; 
} 
?>

I am stumped here How do I split the file names in that array.

Mike

2
Contributors
6
Replies
50 Minutes
Discussion Span
6 Months Ago
Last Updated
7
Views
Question
Answered
branding4you
Junior Poster in Training
75 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You do a lot of copying... why not use $arr, and remove all the weird copying that happens after that. $arr is already an array.

pritaeas
Posting Prodigy
Moderator
9,284 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,457
Skill Endorsements: 86

Tried that .. it send me a blank .DAT file, line 40 would then look like this

$files = array($arr);

Basically I need to find a way to split the name, it comes thorugh in my email as

"1003020122510160249.pdf1003020122510160248.pdf" yet the file should be two files "1003020122510160249.pdf" And "1003020122510160248.pdf" not one single one with one long name - I guess my code joins the file names so the ; or , or ' or something should be the problem or it is the IMPLODE that is wrong ... sigh (banging my head against the desk)

branding4you
Junior Poster in Training
75 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You should remove lines 34-41, and from line 64 onwards, use $arr instead of $files.

pritaeas
Posting Prodigy
Moderator
9,284 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,457
Skill Endorsements: 86

lol ... sorry now im getting Array.dat file in my email. Replaced code from line

// RETRIEVE DOCUMENT NAMES THAT NEED TO BE SENT AS ATTACHMENTS
        $query = "SELECT document FROM transact WHERE clientid=$clientid";
        $result = mysql_query($query) or die (mysql_error());
        while ($record = mysql_fetch_array($result)) {

//ARRAY DATA
        $arr[] = $record['document'];


    }


    $filecount=count($arr);



// email fields: to, from, subject, and so on   
$to = $tomail;
$from = "info@email.com"; 
$subject ="Query RE: $clientid "; 
$message = $mailbody;
$headers = "From: $from"; 

The count is important as some clients have one attachemnt others have 10 or more

branding4you
Junior Poster in Training
75 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The count is important

You don't even use $filecount.

If you don't show what you changed I can't help you.

pritaeas
Posting Prodigy
Moderator
9,284 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,457
Skill Endorsements: 86

Ok, guess RTFS (READ THE F@#$% SCREEN ) is the main thing ... lol
I did not read the screen, replaced $file AND $files with $arr
You need another sigar mate :)

branding4you
Junior Poster in Training
75 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 6 Months Ago by pritaeas

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0891 seconds using 2.76MB