Jump to content


Photo

Help with Multiple Email Attachments


  • Please log in to reply
2 replies to this topic

#1 thisagain

thisagain
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 05 August 2006 - 07:41 PM

I've managed to make my webform email form information and one attachment file but I want to make it add more than one file attachments.

Can someone please help get me started on how to do this?

My code is:

<?php

$to = 'myemail@someplace.com';
$from = 'My Webform';
$subject = 'Webform Submission - ALERT';

// Read POST request params into global vars
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];

$message = "Someone has submitted something via webform";

// Obtain file upload vars
$photo1      = $_FILES['photo1']['tmp_name'];
$photo1_type = $_FILES['photo1']['type'];
$photo1_name = $_FILES['photo1']['name'];

$headers = "From: $from";

if (is_uploaded_file($photo1)){
 // Read the file to be attached ('rb' = read binary)
 $file1 = fopen($photo1,'rb');
 $data1 = fread($file1,filesize($photo1));
 fclose($file1);
 // Generate a boundary string
 $semi_rand = md5(time());
 $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
 // Add the headers for a file attachment
 $headers .= "\nMIME-Version: 1.0\n" .
             "Content-Type: multipart/mixed;\n" . 
             " boundary=\"{$mime_boundary}\"";
 // Add a multipart boundary above the plain message
 $message = "This is a multi-part message in MIME format.\n\n" .
            "--{$mime_boundary}\n" .
            "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\n\n" .
            $message .
            "\n\n";
 // Base64 encode the file data
 $data1 = chunk_split(base64_encode($data1));
 // Add file attachment to the message
 $message .= "--{$mime_boundary}\n" .
             "Content-Type: {$photo1_type};\n" .
             " name=\"{$photo1_name}\"\n" .
             //"Content-Disposition: attachment;\n" .
             //" filename=\"{$photo1_name}\"\n" .
             "Content-Transfer-Encoding: base64\n\n" .
             $data1 .
             "\n\n" .
             "--{$mime_boundary}--\n";
 }
// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok){
 echo '<p>Thank you for sending your Model Submission.</p>';
 }
else{
 echo '<p>Mail could not be sent.</p>';
 }
?>


Any help would be great.  Thanks

#2 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 05 August 2006 - 08:32 PM

Have a look at http://codewalkers.c...eecode/231.html

Description:

PHP Email Attachment v2 by Christoph2k on 01/16/03
This code will allow you to send an email using PHP and include multiple attachments with it! Its really simple to use!


RTFM is an almost extinct art form, it should be subsidized.

#3 thisagain

thisagain
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 05 August 2006 - 08:57 PM

Thanks ronverdonk!

Got it working.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users