email with multiple attachments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tinus
    New Member
    • Jul 2010
    • 30

    #1

    email with multiple attachments

    Hi ,I need my php program to send an email with multiple attachments.
    The following code only sends one attatchment even though I am attaching 2 files.

    Code:
    <?php
    require_once "Mail.php"; // PEAR Mail package
    require_once ('Mail/mime.php'); // PEAR Mail_Mime packge
    $thedate="20100907";
    $from = "thinus <167677690@nwu.ac.za>";
    $to = "thinus <1236767690@nwu.ac.za>";
    $subject = 'Test mime message with an attachment';
    $headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
    $text = 'Text version of email'; // text and html versions of email.
    $html = '<html><body>HTML version of email. <strong>This should be bold</strong></body></html>';
    $file1 = "C:/aptana/xampp-win32-1.7.3/xampp/htdocs/besa/RD_Env_ICV5/Report/MtM_Bond_".$thedate."/"."TRI_FP_difference_".$thedate.".pdf"; // attachment
    $file2 ="C:/aptana/xampp-win32-1.7.3/xampp/htdocs/besa/RD_Env_ICV5/Report/MtM_Bond_".date("Ymd",strtotime("+1 days",$thedate))."/"."TRI_FP_difference_".date("Ymd",strtotime("+1 days",$thedate)).".pdf";
    $crlf = "\n";
    $mime = new Mail_mime($crlf);
    $mime->setTXTBody($text);
    $mime->setHTMLBody($html);
    $mime->addAttachment($file1,'pdf');
    $mime->addAttachment($file2,'pdf');
    
    $body = $mime->get();
    $headers = $mime->headers($headers);
    $host = "scanmail.nwu.ac.za";
    $username = "bwi67e";
    $password = "b53$";
    $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true,
    'username' => $username,'password' => $password));
    ////////////////////////////////////////
    $mail = $smtp->send($to, $headers, $body);
    
    if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
    }
    else {
    echo("<p>Message successfully sent!</p>");
    }
    ?>

    Please advise.
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    Which attachment gets sent? The first attachment or the second one? And can you give us an example of the raw headers in the e-mail that was received? (censor out personal info, like your e-mail address)

    Comment

    • Tinus
      New Member
      • Jul 2010
      • 30

      #3
      Hi

      file1 is being sended as the attatchment.
      I am not sure what you mean with raw headers.
      The headers I can view in the mail is as follow.
      From : me@skl.com
      To:me@skl.com
      subject:Test mime message with an attachment

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        There should be a lot more than that if you are sending file attachments. What mail application do you use to view your mail? They all have means of displaying detailed headers.

        Comment

        • Tinus
          New Member
          • Jul 2010
          • 30

          #5
          Thanks for the help.

          It is working now:

          Code:
          $headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
          $text = 'Text version of email'; // text and html versions of email.
          $html = '<html><body>HTML version of email. <strong>This should be bold</strong></body></html>';
          $file1 = "C:/aptana/xampp-win32-1.7.3/xampp/htdocs/besa/RD_Env_ICV5/Report/MtM_Bond_".date('Ymd',$MyToday)."/"."TRI_FP_difference_".date('Ymd',$MyToday).".pdf"; // attachment
          $crlf = "\n";
          $mime = new Mail_mime($crlf);
          $mime->setTXTBody($text);
          $mime->setHTMLBody($html);
          $mime->addAttachment($file1,'pdf');
          // Add a second attachment
          $file2 ="C:/aptana/xampp-win32-1.7.3/xampp/htdocs/besa/RD_Env_ICV5/Report/MtM_Bond_".date('Ymd',$MyToday)."/"."TRI_FP_difference_".date("Ymd",strtotime("+1 days",$MyToday)).".pdf"; 
          echo $file2;
          //file3
          $mime->addAttachment($file2,'pdf');
          $file3 ="C:/aptana/xampp-win32-1.7.3/xampp/htdocs/besa/RD_Env_ICV5/Report/MtM_Bond_".date('Ymd',$MyToday)."/"."TRI_FP_difference_".date("Ymd",strtotime("+2 days",$MyToday)).".pdf"; 
          echo 'file3:'.$file3;
          $mime->addAttachment($file3,'pdf');
          //do not ever try to call these lines in reverse order
          $body = $mime->get();
          $headers = $mime->headers($headers);
          $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true,
          'username' => $username,'password' => $password));

          Comment

          Working...