Jump to content

SIMPLE Way to Attach File to Email Using MIME?


DamienRoche

Recommended Posts

Whoa, I didn't think it would be this hard to attach a simple file to an email. I can do everything BUT this. I know there is a function:

add_attachment()

But I can't get it to work.

 

I have tried to use phpMailer, oh no...I don't ever want to see that script again.

 

Can anyone recommend a simple way to attach files to emails?

 

Here is some code I was using:


$fileatt = "uploads/image.gif";
$fileatttype = "image/gif";
$fileattname = "image_sent.gif; //can be named anything.

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

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
  

  $semi_rand = md5( time() ); 
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

  $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"; 

mail ($email, $subject, $message, $headers);

 

Yet when I receive the email, there is no attachment and inside it where the $data variable is called there is nothing but an orgasm of dreadfully depressing characters.

 

Can anyone shed some light on what I've missed?

 

Thanks.

 

I can't use it. I just can't get my head around it.

 

I have tried to configure it, but it just won't work.

 

Can you advise, a little, how I would attach a doc using phpMailer.

 

Much obliged.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.