views:

269

answers:

2

Hi,

I'm trying to send an email from the php mail command. I've been able to what I've tried so far, but can't seem to get it to work with an attachment. I've looked around the web and the best code I've found led me to this:

$fileatt_name = 'JuneFlyer.pdf';
$fileatt_type = 'application/pdf';
$fileatt = 'JuneFlyer.pdf';
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
$data = chunk_split(base64_encode($data));
$MAEmail = "[email protected]";

mail("$email_address", "$subject", "$message",
"From: ".$MAEmail."\n".
"MIME-Version: 1.0\n".
"Content-type: text/html; charset=iso-8859-1".
"--{$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" ); 

There are two problems when I do this. First, the contents of the email dissappear.

Second, there is an error on the attachment. "Adobe Reader could not open June_flyer.pdf because it is either not a supported file type or because the file has been damaged (for example it was sent as an email attachment and wasn't correctly decoded)"

Any ideas of how to deal with this?

Thanks,

JB

+3  A: 

The very best way how to deal with mail and php is use a reliable well tested library - email with attachments can easily get very nasty. I personally recommend SwiftMailer.

Jakub Hampl
That seems like a lot of files just for a PHP mailing system.
animuson
+1 No need to reinvent the wheel here. Swift Mailer and PHPMailer make sending emails a snap.
webbiedave
Just html email and email with attachments are one of those things where a good library is well worth quite a few files of foreign library code.
Jakub Hampl
A: 

may be problem is within header. if you want to learn the hard way then figure out how you can configure diffrent mimetypes with headers and do the stuff.

or else easy way is use PHPmailer or other email libraries which will do the hard part for you.

hardik