|
Posted by Ken on 08/29/07 08:23
"Cathy" <cathy.fusko@gmail.com> wrote in message
news:1188353574.246266.224590@e9g2000prf.googlegroups.com...
> On Aug 28, 9:42 pm, "Ken" <d...@wi.rr.com> wrote:
>> I am trying to use php to email file, test.doc
>>
>> test.doc is on the server.
>>
>> How do I attach the file to an email mail()?
>>
>> Thank you
>>
>> Ken
>
> Email with attachments and special types of content (e.g. HTML) can be
> sent using mail() function. This is accomplished via MIME-encoding -
> Check out this Zend article:
> http://www.zend.com/zend/spotlight/sendmimeemailpart1.php.
>
> Hope this helps.
>
> Cathy
> www.nachofoto.com
The email is sent and received.
One problem, the transmitted file is not attached to the email, instead it
is shown as encoded text in the body of the email.
What should I change to have the file attached rather than the contents
displayed?
I also receive an error messages for ini_set(sendmail_from, $fromaddress)
and ini_restore(sendmail_from);
Notice: Use of undefined constant sendmail_from - assumed 'sendmail_from'
in ....
$eol="\r\n";
$mime_boundary=md5(time());
$fromname = "Name";
$fromaddress = "name@domain.com;
# Common Headers
$headers = "";
$headers .= "From: ".$fromname."<".$fromaddress.">".$eol;
$headers .= "Reply-To: ".$fromname."<".$fromaddress.">".$eol;
$headers .= "Return-Path: ".$fromname."<".$fromaddress.">".$eol; // these
two to set reply address
$headers .= "Message-ID: <".time()."-".$fromaddress.">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to
help avoid spam-filters
# Boundry for marking the split & Multitype Headers
$headers .= 'MIME-Version: 1.0'.$eol.$eol;
$headers .= "Content-Type: multipart/mixed;
boundary=\"".$mime_boundary."\"".$eol.$eol;
$file_path = "file path/";
$file_name = "test.doc";
# File for Attachment
$handle=fopen($file_path . $file_name, 'rb');
$f_contents=fread($handle, filesize($file_path . $file_name));
$f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data
For Transition using base64_encode();
$f_type=filetype($file_path . $file_name);
fclose($handle);
# Attachment
$msg = "";
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: application/msword";
$msg .= "; name=\"".$file_name."\"".$eol; // sometimes i have to send MS
Word, use 'msword' instead of 'pdf'
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Description: ".$file_name.$eol;
$msg .= "Content-Disposition: attachment;
filename=\"".$file_name."\"".$eol.$eol; // !! This line needs TWO end of
lines !! IMPORTANT !!
$msg .= $f_contents.$eol.$eol;
# Finished
$msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for
better security. see Injection.
# SEND THE EMAIL
ini_set(sendmail_from, $fromaddress); // the INI lines are to force the
From Address to be used !
$mail_sent = mail($to, $subject, $body. $msg, $headers);
ini_restore(sendmail_from);
echo $mail_sent ? "Mail sent" : "Mail failed";
return $mail_sent;
[Back to original message]
|