Jump to content

Email attachments not working


Nodral

Recommended Posts

Hi all

 

I'm trying to send an automated email of a zip file.  However it seems to be just reading the files into the body of the mail rather than attaching a single zip archive containing several files.

 

I receive the mails, but I effectively want a blank message body with a zip file attached.

 

Please see code below, any ideas why this is not working.  I'm a bit of a newbie and never used this before so any pointers would be good.

 

<?php
//Send Zip file as attachment

//Set Email and attachment details
$to = "me@123.com";
$from = "Visionnet <DO_NOT_REPLY>";
$subject = "Subject";
$message = "Please see attached file";
$fileatt = "wiki_zip.zip";
$fileatt_type = "application/zip";
$fileatt_name = "wiki_zip.zip";


//Read in the attachment
$file=fopen($fileatt,'rb');
$data=fread($file,filesize( $fileatt));
fclose($file);

//Add the MIME content

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

// Add the Headers for file attachment

$headers .= "\nMIME-Version: 1.0\n" . 
		"Content-Type: multipart/mixed;\n" . 
		" boundary=\"{$mime_boundary}\"";
$message = "This is a multipart 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 file data
$data = chunk_split(base64_encode($data));

//Add attachment to message

$message .= "--{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";

//Send message
$ok = @mail($to, $subject, $message, $headers, "$from");
if($ok)
{
echo "mail sent";
}
else
{
echo "mail failed";
}

?>

Link to comment
Share on other sites

i don't like messing with all that email attachments base 64 encoding whatchamacallit stuff. of course, it's nice to know how it works, but I suggest that you use an existing class to handle all the ugly stuff. i use rmail:

 

http://www.phpguru.org/downloads/Rmail/Rmail%20for%20PHP/

 

There is probably newer, but I can't imagine easier. for instance, here is example code to attach a file:

 

$mail->addAttachment(new fileAttachment('example.zip'));

 

wow! no content disposition mime boundary type whatisthat?

 

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.