To send an email with multiple attachments using php, we would be using a class PHPMailer. You can download that class from this link: http://phpmailer.worxware.com/index.php?pg=sf&p=dl
To add more than one attachments to our email we would be using the “addattachment()” method from the PHPMailer class. Here is what you would need to do.
We will start with the basic contact form with three files option, that would make the body of email. The form would be posting data to “sendmail.php” file.
<form method="post" action="sendmail.php" enctype="multipart/form-data"> //Adding a table would make things neat and classier. <table width="343" border="1"> <tr> <td>Full Name</td> <td><input name="fullName" type="text" id="fullName" /></td> </tr> <tr> <td>Email</td> <td><input name="senderEmail" type="text" id="senderEmail" /></td> </tr> <tr> <td>Subject</td> <td><input name="emailSubject" type="text" id="emailSubject" /></td> </tr> <tr> <td>Message</td> <td><textarea name="emailMessage" cols="30" rows="4" id="emailMessage"></textarea></td> </tr> <tr> <td>Attachment</td> <td> <input type="file" name="upload[]" /> <input type="file" name="upload[]" /> <input type="file" name="upload[]" /> </td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Send"></td> </tr> </table> </form>
Now that you are done with your email form, let us move to our sendmail.php file. Here is what you have to do with your sendmail.php
<?php // checking if request type is POST if($_SERVER[‘REQUEST_METHOD’] != ‘POST’) { echo ‘Invalid Request’; exit; // If the request type is not post, it would display an “invalid request” message and stop the code. } require("class.phpmailer.php"); // requiring PHPMailer class $mail=new PHPMailer(); // creating new instance of PHPMailer $mail->to=$_POST['emailTo']; $mail->Subject = $_POST['emailSubject']; $mail->MsgHTML($_POST[‘emailMessage’]); $mail->SetFrom($_POST[‘senderEmail’], $_POST['emailFormName']); $mail->AddReplyTo($_POST[‘senderEmail’], $_POST['emailFormName']); ?>
So far you have created a form, and attached the files you have to send.
Now here is what you would have to do with your AddAttachment() function.
Now Add a loop before the function,
for($i=0; $i < count($_FILES[‘upload’]); $i++) // This loop will upload all the files you have attached to your email. foreach($_FILES[‘upload’] as $position => $file) { if($_FILES[‘upload’][$position][‘error’] != “0”) { continue; } $name=$_FILES[‘upload’][$position][‘name’]; $path=$_FILES[‘upload’][$position][‘tmp_name’]; //And attach it using attachment method of PHPmailer. $mail->addattachment($path,$name); } if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; }
Code above will attach the file with $mail object. Upon calling $mail->send() method from PHPmailer it will dispatch the email with attached files.
4 Comments
hi there,
thanks for sharing the info..its useful
however i am looking for something like… picking up files from a folder using the recordset. so far, for sending a single attachment it has been not a problem as i can simply write it as
$mail -> AddAttachment(“path/”.$row[‘filename’].””);
where $row is actually picking the file name from the DB
but when i loop this statement like below
while ($row = mysql_fetch_assoc($query)) {
$mail -> AddAttachment(“path/”.$row[‘filename’].””);
}
it doesn’t work. It attaches just one file and strangely the first file in the loop.
Please can you guide me whether how can i do the same?
Thanks in advance
Ritesh
I had a similar problem, and came across this post when looking for a solution. This is what I ended up with and works as expected:
Hope this helps!
I change to
for($i=0; $i < count($_FILES[‘upload’]); $i++) // This loop will upload all the files you have attached to your email.
{
if($_FILES[‘upload’][‘name’][$i] != “”) { continue; }
$name=$_FILES[‘upload’][‘name’][$i];
$path=$_FILES[‘upload’][‘tmp_name’][$i];
//And attach it using attachment method of PHPmailer.
$mail->addattachment($path,$name);
}
Racha’s Post works with a minor modification
in the Following line (3rd line) != is to be replaced with ==
if($_FILES[‘upload’][‘name’][$i] != “”) { continue; }
is to be replaced with
if($_FILES[‘upload’][‘name’][$i] == “”) { continue; }