SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple email attachments with php

    I've been struggling to find any simple to follow guides or tutorials on how to do this so I thought I would ask here. I am sending out an email and all I want to do is attach 2 pdf files to it. I have found a script that allows me to send a single email with a single attachment.

    Code:
    <?php
    $from = "me@domain.com";
    $to = $_POST['email'];
    $subject = "Uploaded file";
    $mail_body = "Message body";
    $fileatt = $_FILES['file']['tmp_name'];
    $fileatt_type = $_FILES['file']['type'];
    $fileatt_name = $_FILES['file']['name'];
    
    $headers = "From: $from";
     
    if (is_uploaded_file($fileatt)) 
    {
    	$file= fopen($fileatt, 'rb');
    	$data = fread($file, filesize($fileatt));
    
    	$semi_rand = md5(time()); 
    	$mime_boundary = "Multipart_Boundary_x{$semi_rand}x"; 
    
    	$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
    
    	$mail_body = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . 
    	"Content-Transfer-Encoding: 7bit\n\n" . $mail_body . "\n\n";
    
    	$data = chunk_split( base64_encode($data));
    
    	$mail_body .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . 
    	" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; 
    
    	$ok = @mail($to, $subject, $mail_body, $headers);
    	
    	if($ok)
    		echo "Email sent :)";
    	else
    		echo "Failed !!!";
    	
    	fclose($file);
    }
    ?>
    This works fine but how can I adapt this to send 2 attachments? Here's the code for the form that gets the user info;

    Code:
    <form action="process.php" method="post" enctype="multipart/form-data">
    To:<input type="text" name="email" /><br />
    File: <input type="file" name="file" id="file"  /> <br />
    <input type="submit" name="submit" value="Sent"  />
    </form>
    If anyone can help or point me in the right direction, then I would be very grateful.

  2. #2
    SitePoint Zealot j3ph's Avatar
    Join Date
    Jan 2006
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put this into a separate php file and submit the form with two attachments.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">
    <head>
    	<title>Multiple File Upload Test</title>
    	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
    </head>
    <body>
    	<?php if($_POST) : ?>
    		<?php @extract($_FILES);?>
    		<pre>
    		<?php var_dump($file);?>	
    		</pre>
    	<?php endif; ?>
    	<form action="test.php" method="post" enctype="multipart/form-data">
    		To:<input type="text" name="email" /><br />
    		File 1: <input type="file" name="file[]" id="file"  /> <br />
    		File 2: <input type="file" name="file[]" id="file"  /> <br />
    		<input type="submit" name="submit" value="Sent"  />
    	</form>
    </body>
    </html>
    You'll see that $file becomes a multidimensional array:
    Code:
    array
      'name' => 
        array
          0 => string 'vlcsnap-2010-05-24-11h03m56s156.png' (length=35)
          1 => string 'screen.png' (length=10)
      'type' => 
        array
          0 => string 'image/png' (length=9)
          1 => string 'image/png' (length=9)
      'tmp_name' => 
        array
          0 => string 'C:\wamp\tmp\php718D.tmp' (length=23)
          1 => string 'C:\wamp\tmp\php71AD.tmp' (length=23)
      'error' => 
        array
          0 => int 0
          1 => int 0
      'size' => 
        array
          0 => int 234418
          1 => int 282939


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •