Olaf posted on 2009-09-11 13:53:12 #
I got several times the question how-to send mail messages with multiple attachments using the upload class together with the attachment mailer class. This short tutorial will show how to do that.
First we need to include both class (check the file paths to get it work)
include_once($_SERVER['DOCUMENT_ROOT']."/classes/upload/upload_class.php");
include_once($_SERVER['DOCUMENT_ROOT']."/classes/attach_mailer/attach_mailer_class.php");
$error = '';
Next we need some upload form that can upload X files.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
<label for="upload[]">File 1:</label>
<input type="file" name="upload[]" size="30">
<label for="upload[]">File 2:</label>
<input type="file" name="upload[]" size="30">
<label for="upload[]">File 3:</label>
<input type="file" name="upload[]" size="30">
<!-- Add here more file fields if you need. -->
<input type="submit" name="Submit" value="Submit">
</form>
Nore the the name for the file elements: "upload[]", these square brackets take care about that the uploaded files become an array in PHP.
If we submit the form this code should be executed (I placed the code before the html part begins)
if(isset($_POST['Submit'], $_FILES['upload'])) {
$my_mail = new attach_mailer('MyName', 'mail@me.com', 'sendto@someone.com', '', '', 'test mail multi att.');
$my_mail->text_body = 'Hello World';
$num_files = count($_FILES['upload']['name']);
for ($i = 0; $i < $num_files; $i++) {
if ($_FILES['upload']['name'][$i] != '') {
$my_upload = new file_upload;
$my_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/files/multi/";
$my_upload->extensions = array(".png", ".jpg", ".gif");
$my_upload->the_temp_file = $_FILES['upload']['tmp_name'][$i];
$my_upload->the_file = $_FILES['upload']['name'][$i];
$my_upload->http_error = $_FILES['upload']['error'][$i];
$my_upload->replace = "n";
if ($my_upload->upload()) {
$full_path = $my_upload->upload_dir.$my_upload->file_copy;
$my_mail->add_attach_file($full_path);
$error .= 'File "'.$my_upload->file_copy.'" uploaded';
} else {
break;
$error = 'Error uploading file(s)';
}
}
}
if ($my_mail->process_mail()) {
$error .= 'Mail send!';
} else {
$error .= 'Error sending mail';
}
}
First we test if there is an array of files and than we count the number of files, because we need that number in the loop. We use this loop to test each file if it exists and if uploaded we add the file as an attachment to the mail object we created before.
After all file uploads are process we send the mail.
Note the tutorial is a little bit quick and dirty but should work on all Linux distros (I did a test on my local Ubuntu config). The e-mail class used in this tutorial is using the native PHP "mail()" function. The complete example file (multimail.php) is included in the recent version of the attachment mailer class.
The most popular forum posts:
Comments / discussions
hosam76 posted on 2009-09-11 16:10:40 #
thanx for your great and fast help
it works well
but when I tried to add the fields to your new form
it works also but when I leave any field blank it still work without any error message
I need the script to stop if the user leave the e-mail or name or file field blank
this is what I did and I hope you can help
snip...
Olaf posted on 2009-09-11 19:07:18 #
hosam76 posted on 2009-09-12 00:48:04 #
actually I'm amateur in PHP
I tried to upgrade your new code but I failed
I appreciate your help any way
hosam76 posted on 2009-09-16 00:18:44 #
hello mr Olaf
can you upgrade this script and make it resize the photos befor sending it by mail
((same the foto_upload_script.php))
I'm ready to pay the cost of this upgrade
Olaf posted on 2009-09-16 04:33:21 #
I'm sorry hosam76, but I don't have the time during the next weeks.
hosam76 posted on 2009-09-16 13:11:41 #
I'm not in hurry to do it
but if you ready to do it tell me so I can remind you later
but if you don't like to do it ,it's ok I can understand.
Olaf posted on 2009-09-16 13:39:23 #
... in the meantime you can try to solve it by yourself :)
hosam76 posted on 2009-09-16 14:34:50 #
I told I'm not profesional in PHP if I know how to upgrade it ,I will not ask you to do it
do you know any script do the same purpose????
Olaf posted on 2009-09-16 14:52:59 #
Hi,
what you're asking and also this tutorial is not standard.
PHP is easy to learn and doesn't work out of the box. If you need something out of the box, you need to work with basic applications like Joomla or wordpress.
Most people writing php code without being a professional. Check the example files from the class and also the PHP manual and you will see that you don't need to be a professional...
hosam76 posted on 2009-09-20 02:23:49 #
hello olaf
I success to make the script resize the photos then sending them in the new size to the mail
but my problem now
only the warning message when the user left the(name or e-mail or file) blank
it's not working
do you have time to may a look or you still bussy
thanx
Olaf posted on 2009-09-20 06:02:11 #
Please open a new topic and post the code you have
dannyboy263 posted on 2009-10-01 16:39:01 #
Hi thanks for this script, its working, but I wonder How to send a copie of the same email + attachments to another email addres? I need to have a copie with 2 different texts, but same attachments
Thanks
Olaf posted on 2009-10-01 18:10:14 #
You need to know that every message you send by email has it's own process (accept if add some Cc or Bcc receptionists)
The upload is still available after the first mail is send, just use them again in a new mail object using different body text pieces. If you have further questions, please open a new topic.
ayaz posted on 2009-10-15 06:17:30 #
Hello Olaf,
Firstly, Thank you for the multiple attachment code.
I tried using this code but it gave me errors. It does not work. Find the errors below. Can you kindly help? I would really appreciate it.
ERROR LINES---------->
Warning: mkdir() [function.mkdir]: No such file or directory in /home4/tanzani2/public_html/upload/classes/upload/upload_class.php on line 151
Warning: move_uploaded_file(/home4/tanzani2/public_html/files/multi/firewall.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home4/tanzani2/public_html/upload/classes/upload/upload_class.php on line 131
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/var/tmp/phpyckgvq' to '/home4/tanzani2/public_html/files/multi/firewall.jpg' in /home4/tanzani2/public_html/upload/classes/upload/upload_class.php on line 131
Olaf posted on 2009-10-15 07:42:40 #
Hello ayaz and welcome,
there is something wrong with your paths, check your upload path settings.
note that only one level of directories will be created by the script, example:
Okay:
uploads_here/
uploads_here/new_created/
doens't work:
uploads_here/
uploads_here/some_directory/new_created/
Since this question is not related to the multi upload/attachment feature please start a new topic.
Thanks
ayaz posted on 2009-10-15 08:35:55 #
Thank you Olaf.
Let me look into it. if I can not sort it then I'll open a new topic.
Thanks
krailerk posted on 2010-07-15 03:59:34 #
krailerk posted on 2010-07-15 04:34:16 #
Hello olaf
my code is error Fatal error: Cannot instantiate non-existent class: attach_mailer in D:\Inetpub\wwwroot\complain\multimail.php on line 8
i'm use PHP V.4.3.8.
Can you kindly help me?
Olaf posted on 2010-07-15 06:51:53 #
Since your problem is not related to this topic, please start a new topic.