Jump to content

[SOLVED] multiple attachments?


shadiadiph

Recommended Posts

I am using the following which attaches one file fine how can I make it attach and send more than 1 file?

 

 



$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];


        $to = 'website.com';
        $boundary = '==Multipart_Boundary_x' .md5(time()). 'x';
        // headers
        $headers .= "From: APA <noreply@website.com>\n";  
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-Type: multipart/mixed;\n boundary=\"$boundary\"\n\n";
        // message
        $message = "--{$boundary}\n";
        $message .= "Content-Type: text/html; charset=iso-8859-1\n";
        $message .= "Content-Transfer-Encoding: 7bit\n\n";



        // attachment
        $fp = fopen($fileatt, 'rb');
        $data = fread($fp, filesize($fileatt));
        fclose($fp);

        $data = chunk_split(base64_encode($data));
        $message .= "--{$boundary}\n";
        $message .= "Content-Type: $fileatt_type;\n name=\"$fileatt_name\"\n";
      //$message .= "Content-Disposition: attachment;\n filename=\"$fileatt_name\"\n";
        $message .= "Content-Transfer-Encoding: base64;\n\n$data\n\n";
        $message .= "--{$boundary}--\n";

Link to comment
Share on other sites

I am sort of doing the same thing ... But what i am working on is this

 

<input id="file" style="BACKGROUND-COLOR: #ddeeee" type="file" size="55" name="userfile[]1">
<input id="file" style="BACKGROUND-COLOR: #ddeeee" type="file" size="55" name="userfile[]1">
<input id="file" style="BACKGROUND-COLOR: #ddeeee" type="file" size="55" name="userfile[]1">
<input id="file" style="BACKGROUND-COLOR: #ddeeee" type="file" size="55" name="userfile[]1">
<input id="file" style="BACKGROUND-COLOR: #ddeeee" type="file" size="55" name="userfile[]1">

 

then

 

foreach($_FILES['userfile']['tmp_name'] as $key => $value){

// [...]
// $_FILES['userfile']['tmp_name'][$key]
// $_FILES['userfile']['name'][$key]
// [...]

}

Link to comment
Share on other sites

What i gave you is the solution bro hehe the problem i have is a lot more complicated

 

<?php

$target_path = "uploads/";

foreach($_FILES['userfile']['tmp_name'] as $key => $value){

$target_path = $target_path . basename( $_FILES['userfile']['name'][$key]); 

if(move_uploaded_file($_FILES['userfile']['tmp_name'][$key], $target_path)) {
    echo "The file ".  basename( $_FILES['userfile']['name'][$key]). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

}
?>

Link to comment
Share on other sites

mmm I have just tried it with this a remake of the original one that was sending one file with no problem I acn't see why it isn't working??

 




$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

        $to = 'info@apa.com';
        $boundary = '==Multipart_Boundary_x' .md5(time()). 'x';
        // headers
        $headers .= "From: APA <noreply@apa.com>\n";  
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-Type: multipart/mixed;\n boundary=\"$boundary\"\n\n";
        // message
        $message = "--{$boundary}\n";
        $message .= "Content-Type: text/html; charset=iso-8859-1\n";
        $message .= "Content-Transfer-Encoding: 7bit\n\n";
        $message .= "<html>

<head>
<title>HTML message</title>

</head>
<body>
hello
</body>
</html>";


    for($x=0;$x<count($fileatt);$x++){
    $file = fopen($fileatt[$x],"rb");
    $data = fread($file,filesize($fileatt[$x]));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $message .= "--{$boundary}\n";
    $message .= "Content-Type: $fileatt_type[$x];\n name=\"$fileatt_name[$x]\"\n";
  //$message .= "Content-Disposition: attachment;\n filename=\"$fileatt_name[$x]\"\n";
    $message .= "Content-Transfer-Encoding: base64;\n\n$data\n\n";
    $message .= "--{$boundary}--\n";
}

 

 

        mail($to, $subject, $message, $headers);

 

[/code]

Link to comment
Share on other sites

I now have

$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];


        $to = 'info@apa.com';
        $boundary = '==Multipart_Boundary_x' .md5(time()). 'x';
        // headers
        $headers .= "From: Asia Pacific Advisors <noreply@apa.com>\n";  
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-Type: multipart/mixed;\n boundary=\"$boundary\"\n\n";
        // message
        $message = "--{$boundary}\n";
        $message .= "Content-Type: text/html; charset=iso-8859-1\n";
        $message .= "Content-Transfer-Encoding: 7bit\n\n";
        $message .= "<html>

<head>
<title>HTML message</title>

</head>
<body>
hello
if(is_uploaded_file($fileatt)){
for($x=0;$x<count($fileatt);$x++)
{

    $fp = fopen($fileatt[$x], 'rb');
    $data = fread($fp, filesize($fileatt[$x]));
    fclose($fp);
    $data = chunk_split(base64_encode($data));
    $message .= "--{$boundary}\n";
    $message .= "Content-Type: $fileatt_type[$x];\n name=\"$fileatt_name[$x]\"\n";
    //$message .= "Content-Disposition: attachment;\n filename=\"$fileatt_name[$x]\"\n";
    $message .= "Content-Transfer-Encoding: base64;\n\n$data\n\n";
    $message .= "--{$boundary}--\n";
}
}

        mail($to, $subject, $message, $headers);

Link to comment
Share on other sites

mm almost have it working now this is the closest it has been but it is only attaching one file?

 

foreach ($_FILES["fileatt"]["error"] as $key => $error) { 
        if ($error == UPLOAD_ERR_OK) { 
$fileatt = $_FILES['fileatt']['tmp_name'][$key];
$fileatt_type = $_FILES['fileatt']['type'][$key];
$fileatt_name = $_FILES['fileatt']['name'][$key]; 


        // attachment
        $fp = fopen($fileatt, 'rb');
        $data = fread($fp, filesize($fileatt));
        fclose($fp);

        $data = chunk_split(base64_encode($data));
        $message .= "--{$boundary}\n";
        $message .= "Content-Type: $fileatt_type;\n name=\"$fileatt_name\"\n";
        //$message .= "Content-Disposition: attachment;\n filename=\"$fileatt_name\"\n";
        $message .= "Content-Transfer-Encoding: base64;\n\n$data\n\n";
        //$message .= "--{$boundary}--\n";
      
       
    } 
}


        mail($to, $subject, $message, $headers);

Link to comment
Share on other sites

i am close to saying EUREKA but unfortunately not. now it sends both the attachments they have the correct file names types in the email but they are all unreadable? jpg gif pdf .doc .txt the .txt file did open though and had this in it

Content-Disposition: attachment; filename="/tmp/phpS6Wyy3"

 

and nothing else

 

here is the code i am using

 

<? 
session_start();
error_reporting(apa);
error_reporting(7);
include("../secure/global/connection.php");


        $to = 'info@website.com';
        $boundary = '==Multipart_Boundary_x' .md5(time()). 'x';
        // headers
        $headers .= "From: Website name <noreply@website.com>\n";  
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-Type: multipart/mixed;\n boundary=\"$boundary\"\n\n";
        // message
        $message = "--{$boundary}\n";
        $message .= "Content-Type: text/html; charset=iso-8859-1\n";
        $message .= "Content-Transfer-Encoding: 7bit\n\n";
        $message .= "<html>
<head>
<title>HTML message</title>

</head>
<body>
hello testing

</body>
</html>";



foreach ($_FILES["fileatt"]["error"] as $key => $error) { 
        if ($error == UPLOAD_ERR_OK) { 
$fileatt = $_FILES['fileatt']['tmp_name'][$key];
$fileatt_type = $_FILES['fileatt']['type'][$key];
$fileatt_name = $_FILES['fileatt']['name'][$key]; 


        
        // attachment
        $fp = fopen($fileatt, 'rb');
        $data = fread($fp, filesize($fileatt));
        fclose($fp);

        $data = chunk_split(base64_encode($data)); 
        $message .= $data . "\n";
        $message .= "--{$boundary}\n";
        $message .= "Content-Type: application/$fileatt_type; name=\"$fileatt_name\"\n";
        $message .= "Content-Transfer-Encoding: 7bit\n\n";
        $message .= "Content-Disposition: attachment; filename=\"$fileatt\"\n\n";  



       
    } 
}


        mail($to, $subject, $message, $headers);
    

?>	

Link to comment
Share on other sites

solved it at last after 500 test emails here is the solution for that person that asked me to post it if i solved it.

 

upload.php

 

<?php

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload Files To Email</title>
</head>
<body>
<form name="filestomail" method="post" action="submit.php" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />

<table>
<tr><td>FILE ONE</td></tr>
<tr><td><input type="file" name="fileatt[]" /></td></tr>

<tr><td>FILE TWO</td></tr>
<tr><td><input type="file" name="fileatt[]" /></td></tr>

<tr><td> </td></tr>

<tr><td><input name="submit" type="submit" value="Submit" /></td></tr>
<tr><td><input name="reset" type="reset" value="Reset" /></td></tr>

<tr><td> </td></tr>

</table>
</form>
</body>
</html>

 

 

submit.php

 

<?
        $to = 'info@website.com';
        $boundary = '==Multipart_Boundary_x' .md5(time()). 'x';
        // headers
        $headers .= "From: Website Name <noreply@website.com>\n";  
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-Type: multipart/mixed;\n boundary=\"$boundary\"\n\n";
        // message
        $message = "--{$boundary}\n";
        $message .= "Content-Type: text/html; charset=iso-8859-1\n";
        $message .= "Content-Transfer-Encoding: 7bit\n\n";
        $message .= "<html>
<head>
<title>HTML message</title>

</head>
<body>
MESSAGE HTML FORMAT GOES HERE
</body>
</html>";



foreach ($_FILES["fileatt"]["error"] as $key => $error) { 
        if ($error == UPLOAD_ERR_OK) { 
$fileatt = $_FILES['fileatt']['tmp_name'][$key];
$fileatt_type = $_FILES['fileatt']['type'][$key];
$fileatt_name = $_FILES['fileatt']['name'][$key]; 

       
        // attachment

        $fp = fopen($fileatt, 'rb');
        $data = fread($fp, filesize($fileatt));
        fclose($fp);

        $data = chunk_split(base64_encode($data));
        $message .= "\n\n";
        $message .= "--{$boundary}\n";
        $message .= "Content-Type: application/$fileatt_type; name=\"$fileatt_name\"\n";
        $message .= "Content-Transfer-Encoding: base64\n\n";
        $message .= "$data\n\n";


       
    } 
}


        mail($to, $subject, $message, $headers);
    

?>	

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.