28, May 2012

need help with multiple attachments - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Miscellaneous scripts or snippets (Moderator: Olaf)
Topic: need help with multiple attachments
« previous next »
Pages: [1] 2 Print

Author Topic: need help with multiple attachments  (Read 3996 times)
What a dork
*
Posts: 17
106 credits
Members referred : 0


« on: Apr 23, 2007, 02:45:26 pm »

Hello Olaf Lederer!

How r u. I am Qasim Ali. I am using your Attachment Mailer class. Its a wonderful class and works well for email attachment using one attachment currently. I want to modify it for multiple attachements. But somehow i am not getting it right. I am sending you the code for what i am doing currently. Please help me modify it for multiple attachments.

Please see attachment.

Thanks!

Regards,
Qasim Ali

* upload_and_mail_example.txt (3.73 KB - downloaded 2 times.)
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Apr 23, 2007, 02:57:14 pm »

hello,

check the basic example for handling multiple attachments:

Code:
<?php
$test 
= new attach_mailer($name "Olaf"$from "your@mail.com"$to "he@gmail.com"$cc ""$bcc ""$subject "Test text email with attachments");
$test->text_body "...Some body text\n\n the admin";
$test->add_attach_file("image.gif"); // att. 1
$test->add_attach_file("ip2nation.zip"); // att. 2
$test->process_mail();

of course you need all attachments on the server

What a dork
*
Posts: 17
106 credits
Members referred : 0


« Reply #2 on: Apr 23, 2007, 03:24:45 pm »

Hi Dear!

I have seen this! but what i need you to please guide me where i am wrong in the code. i have already send you the attached file. Please help.

Thanks for showing quick response.

Regards,
Qasim
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Apr 23, 2007, 03:30:24 pm »

sorry I don't fix your code, I will guide you that you learn how it have to work.

if you can use the example with multiple attachments its time to learn how to upload multiple files...

What a dork
*
Posts: 17
106 credits
Members referred : 0


« Reply #4 on: Apr 23, 2007, 03:37:58 pm »

Hi! i am not telling u to fix it, i am just asking that where i am wrong in the following code since it was not working.

if(isset($_POST['Submit'])) {
   $my_mail = new attach_mailer($name = $_POST['from_name'], $from = $_POST['from_email'], $to = $_POST['mail_to'], $cc = "", $bcc = "", $subject = $_POST['mail_subject'], $body = $_POST['mail_body']);
      
         //loop will go here
         for ($i = 1; $i <= $num_files; $i++) {    
               $my_upload->the_temp_file = $_FILES['upload'.$i]['tmp_name'];
               $my_upload->the_file = $_FILES['upload'.$i]['name'];
               $my_upload->http_error = $_FILES['upload'.$i]['error'];   
            if ($my_upload->upload()) {               
               $full_path = $my_upload->upload_dir.$my_upload->file_copy;      
               $my_mail->create_attachment_part($full_path);   
            }
         }
         $my_mail->process_mail();
         $msg .= $my_upload->show_error_string();
         $msg .= $my_mail->get_msg_str();
}

Please help!
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #5 on: Apr 23, 2007, 03:49:48 pm »

With a fast look, I see that $num_files is not defined anywhere in your code. Maybe this is the source of the problem?

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
What a dork
*
Posts: 17
106 credits
Members referred : 0


« Reply #6 on: Apr 23, 2007, 03:58:24 pm »

Oh no $num_files is not the problem, i just didn't copied the whole code, If you want to see the whole page its attached in the first post. The problem lies here

Hi! i am not telling u to fix it, i am just asking that where i am wrong in the following code since it was not working.

if(isset($_POST['Submit'])) {
   $my_mail = new attach_mailer($name = $_POST['from_name'], $from = $_POST['from_email'], $to = $_POST['mail_to'], $cc = "", $bcc = "", $subject = $_POST['mail_subject'], $body = $_POST['mail_body']);
     
         //loop will go here
         for ($i = 1; $i <= $num_files; $i++) {   
               $my_upload->the_temp_file = $_FILES['upload'.$i]['tmp_name'];
               $my_upload->the_file = $_FILES['upload'.$i]['name'];
               $my_upload->http_error = $_FILES['upload'.$i]['error'];   
            if ($my_upload->upload()) {               
               $full_path = $my_upload->upload_dir.$my_upload->file_copy;     
               $my_mail->create_attachment_part($full_path);   
            }
         }
         $my_mail->process_mail();
         $msg .= $my_upload->show_error_string();
         $msg .= $my_mail->get_msg_str();
}

Please help me to correct it.
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #7 on: Apr 23, 2007, 04:26:08 pm »

To be honest I have no clue how Olaf's class works, but I will try to help Smiley

Add this line of code before the for loop :

var_dump($_FILES)?>

What output do you get?

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #8 on: Apr 23, 2007, 04:38:58 pm »

Oh no $num_files is not the problem, i just didn't copied the whole code, If you want to see the whole page its attached in the first post. The problem lies here

Hi! i am not telling u to fix it, i am just asking that where i am wrong in the following code since it was not working.

if(isset($_POST['Submit'])) {
   $my_mail = new attach_mailer($name = $_POST['from_name'], $from = $_POST['from_email'], $to = $_POST['mail_to'], $cc = "", $bcc = "", $subject = $_POST['mail_subject'], $body = $_POST['mail_body']);
     
         //loop will go here
         for ($i = 1; $i <= $num_files; $i++) {   
               $my_upload->the_temp_file = $_FILES['upload'.$i]['tmp_name'];
               $my_upload->the_file = $_FILES['upload'.$i]['name'];
               $my_upload->http_error = $_FILES['upload'.$i]['error'];   
            if ($my_upload->upload()) {               
               $full_path = $my_upload->upload_dir.$my_upload->file_copy;     
               $my_mail->create_attachment_part($full_path);   
            }
         }
         $my_mail->process_mail();
         $msg .= $my_upload->show_error_string();
         $msg .= $my_mail->get_msg_str();
}

Please help me to correct it.

the problem is that you mix some code which won't work, isolate the functions as suggested:

a - build the code for the upload (use two objects if you have two files otherwise it could be useful to use a loop)
b - build the code for the e-mail

don't try to get both working at the same time if you don't know what the code is doing.

Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #9 on: Apr 23, 2007, 04:40:08 pm »

and maybe you need some basics about using classes/objects:
http://nl3.php.net/manual/en/ref.classobj.php

What a dork
*
Posts: 17
106 credits
Members referred : 0


« Reply #10 on: Apr 24, 2007, 07:33:49 am »

Hello Olaf!

Thanks for your help. I am very close to getting it done. The code give below works fine for one file, but when i upload two files it says "Error! Sorry, a file with this name already exitst." bit in my case their is no file in the directory of the same name. Please let me know whats the problem.

if(isset($_POST['Submit'])) {
   $my_mail = new attach_mailer($name = $_POST['from_name'], $from = $_POST['from_email'], $to = $_POST['mail_to'], $cc = "", $bcc = "", $subject = $_POST['mail_subject'], $body = $_POST['mail_body']);
   for ($i = 1; $i <= $num_files; $i++){
      $my_upload->the_temp_file = $_FILES['upload'.$i]['tmp_name'];
      $my_upload->the_file = $_FILES['upload'.$i]['name'];
      $my_upload->http_error = $_FILES['upload'.$i]['error'];
      if ($my_upload->upload()) {
         //loop will go here
         $full_path = $my_upload->upload_dir.$my_upload->file_copy;      
         echo $full_path;
         $files_uploaded[$i] = $full_path;         
      }
   }
   for ($i = 1; $i <= $num_files; $i++){
      $my_mail->create_attachment_part($files_uploaded[$i]);
   }
   $my_mail->process_mail();
   $msg .= $my_upload->show_error_string();
   $msg .= $my_mail->get_msg_str();
}

Thanks
qasim ali
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #11 on: Apr 24, 2007, 08:22:30 am »

you need to create a new object for every file:

$my_upload = new upload; need to be inside the loop (also all other upload vars)

What a dork
*
Posts: 17
106 credits
Members referred : 0


« Reply #12 on: Apr 24, 2007, 08:38:13 am »

Hello Olaf!

Thanks! now one last thing, it gives an Error "Error, the file "" does not exist." when i try to upload files of two same extensions, other wise it works fine.

Shows following error

File: iamuploadingit today.zip successfully uploaded!
The uploaded file is renamed to 1177397078.zip.
Error, the file "" does not exist.
Your mail is succesfully submitted.



Regards,
Qasim
What a dork
*
Posts: 17
106 credits
Members referred : 0


« Reply #13 on: Apr 24, 2007, 09:02:10 am »

for your information this is what it returns

array(2) {
  ["upload1"]=>
  array(5) {
    ["name"]=>
    string(24) "iamuploadingit today.zip"
    ["type"]=>
    string(28) "application/x-zip-compressed"
    ["tmp_name"]=>
    string(14) "/tmp/phpfmkTgy"
    ["error"]=>
    int(0)
    ["size"]=>
    int(9809)
  }
  ["upload2"]=>
  array(5) {
    ["name"]=>
    string(19) "samp_bottom_tab.zip"
    ["type"]=>
    string(28) "application/x-zip-compressed"
    ["tmp_name"]=>
    string(14) "/tmp/php9Eq8Gv"
    ["error"]=>
    int(0)
    ["size"]=>
    int(115499)
  }
}

Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #14 on: Apr 24, 2007, 10:01:03 am »

what is the code you're using now?

What a dork
*
Posts: 17
106 credits
Members referred : 0


« Reply #15 on: Apr 24, 2007, 10:45:02 am »

here it is

if(isset($_POST['Submit'])) {
   $my_mail = new attach_mailer($name = $_POST['from_name'], $from = $_POST['from_email'], $to = $_POST['mail_to'], $cc = "", $bcc = "", $subject = $_POST['mail_subject'], $body = $_POST['mail_body']);
   echo "<pre>";
   var_dump($_FILES);
   echo "</pre>";
   for ($i = 1; $i <= $num_files; $i++){
      $my_upload = new file_upload;
      $my_upload->upload_dir = "/home/qasim/new/upload/100/"; // "files/mail" is the directory for the uploaded files (you have to create this directory)
      $my_upload->extensions = array(".png", ".zip", ".pdf", ".jpg",".txt",".doc",".gif"); // specify the allowed extensions here
      // $my_upload->extensions = "de"; // use this to switch the (upload) messages into an other language
      $my_upload->rename_file = true; // rename the file to get a valid file string in your mail

      $my_upload->the_temp_file = $_FILES['upload'.$i]['tmp_name'];
      $my_upload->the_file = $_FILES['upload'.$i]['name'];
      $my_upload->http_error = $_FILES['upload'.$i]['error'];
      if ($my_upload->upload()) {
         //loop will go here
         $full_path = $my_upload->upload_dir.$my_upload->file_copy;      
         echo $full_path;
         $files_uploaded[$i] = $full_path;      
         $msg .= $my_upload->show_error_string();   
         //$my_mail->create_attachment_part($full_path);
         //$my_mail->process_mail();
         //$my_mail->del_temp_file($full_path); // the file is not needed anymore
      }
   }
   for ($i = 1; $i <= $num_files; $i++){
      $my_mail->create_attachment_part($files_uploaded[$i]);
   }
   $my_mail->process_mail();
   $msg .= $my_mail->get_msg_str();
}
What a dork
*
Posts: 17
106 credits
Members referred : 0


« Reply #16 on: Apr 24, 2007, 12:11:57 pm »

Hello Sir!

Its really a strange problem, its working fine for messages with differnent extensions but its creatin problems for same extension files. Have you got to any conclusion.

Thanks for you rcooperation.

Regards,
Qasim
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #17 on: Apr 24, 2007, 03:10:21 pm »

is see the problem, your server is to fast Cheesy

add sleep(2) before the end of the loop.

I think you're finished than!

What a dork
*
Posts: 17
106 credits
Members referred : 0


« Reply #18 on: Apr 25, 2007, 06:34:32 am »

Hello Olaf!

Its working fine now. One thing more, when i try to send email to multiple recipients comma separated, it fails and returns error as
|Error, the "mailto" address is empty or not valid.

Please let me know how to handle this now.
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #19 on: Apr 25, 2007, 08:23:22 am »

Hello Olaf!

Its working fine now. One thing more, when i try to send email to multiple recipients comma separated, it fails and returns error as
|Error, the "mailto" address is empty or not valid.

Please let me know how to handle this now.

this is fixed in the next version, while I think that sending mails this way is wrong, check the modification inside the method attach_mailer:

Code:
if (is_array($cc)) { // changed in version  1.03
$invalid_cc = array();
foreach ($cc as $c) {
if (!$this->check_mail_address($c)) {
$invalid_cc[] = $c;
}
}
if (count($invalid_cc) > 0) {
$this->msg[] = "Error, invalid \"Cc\"(s): ".implode(", ", $invalid_cc);
$this->valid_mail_adresses = false;
}
} else {
$cc = array();
}


if you change this you need to add email addresses as an array

Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=6436
Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] 2 Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Miscellaneous scripts or snippets (Moderator: Olaf)
Topic: need help with multiple attachments
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 08:35:50 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.