. . .

Multiple Attachments using PHPMailer Class

Published: April 12, 2013

On This Page

     

    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>&nbsp;</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.


     

    Don't forget to share this post

      Let's Build Digital Excellence Together


      • Cost Efficient Solutions.
      • Minimal Timelines.
      • Effective Communication.
      • High Quality Standards.
      • Lifetime Support.
      • Transparent Execution.
      • 24/7 Availability.
      • Scalable Teams.

      Join Our 200+ Happy Clients Across Globe


      Free Consultation.

        Do you need tech help of your startup/business? Experts from our team will get in touch with you.

        Please do not post jobs/internships inquiries here.