Jump to content

Help with php email w/attachment


millercj

Recommended Posts

What i need to do is have an html form where the user selects a local file on their system and it goes emailed via a php script. I've got this script but now do i adapt it to accept an input from a form, will the $_POST variable from the form suffice for the file location and how do I accept any type of file the user submits...not just PDF's

 

<?php
    
$to = "Chris Miller <chris@digiconmediagroup.com>"; 
$from = "John-Smith <john.smith@domain.com>"; 
$subject = "Here is your attachment"; 
$fileatt = "logo.pdf";
$fileatttype = "application/pdf"; 
$fileattname = "newname.pdf";
$headers = "From: $from";

$file = fopen( $fileatt, 'rb' ); 
$data = fread( $file, filesize( $fileatt ) ); 
fclose( $file ); 

$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}\"";
    
$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";
    
$data = chunk_split( base64_encode( $data ) );
                 
$message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatttype};\n" . " name=\"{$fileattname}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileattname}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; 	

if( mail( $to, $subject, $message, $headers ) ) {         
echo "<p>The email was sent.</p>"; 
    }
    else { 
          echo "<p>There was an error sending the mail.</p>"; 
         }
?>

Link to comment
Share on other sites

You need to edit a couple of things here.

 

You should make a case for several file types,

 

so for $fileattype you'd do

 

switch($fileatt) {

 

case application/pdf;

// Blah

break;

}

 

Just start by working on that, the make the fileatname to the value that was posted from the form, and maybe someone else can add to what I suggested :)

 

Link to comment
Share on other sites

Ok i've done this switch but it's not functioning, it always goes to the default case...any ideas?

 

<?php
    
$to = "Chris Miller <chris@digiconmediagroup.com>"; 
$from = "John-Smith <john.smith@domain.com>"; 
$subject = "Here is your attachment"; 
$fileatt = "image.png";

switch ($fileatt)
{
case 'application/pdf':
	$fileatttype = "application/pdf"; 
	$fileattname = "document.pdf";
	break;  

case 'image/jpg':
	$fileatttype = "image/jpg"; 
	$fileattname = "image.jpg";
  	    break;

case 'image/jpeg':
	$fileatttype = "image/jpeg"; 
	$fileattname = "image.jpeg";
  	    break;

case 'image/png':
	$fileatttype = "image/png"; 
	$fileattname = "image.png";
  	    break;

case 'image/gif':
	$fileatttype = "image/gif"; 
	$fileattname = "image.gif";
  	    break;

case 'application/zip':
	$fileatttype = "application/zip"; 
	$fileattname = "picfile.zip";
  	    break;


default:
  echo'theres a problem';
}

$headers = "From: $from";

$file = fopen( $fileatt, 'rb' ); 
$data = fread( $file, filesize( $fileatt ) ); 
fclose( $file ); 

$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}\"";
    
$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";
    
$data = chunk_split( base64_encode( $data ) );
                 
$message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatttype};\n" . " name=\"{$fileattname}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileattname}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; 	

if( mail( $to, $subject, $message, $headers ) ) {         
echo "<p>The email was sent.</p>"; 
    }
    else { 
          echo "<p>There was an error sending the mail.</p>"; 
         }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.