email attachment

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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


Re: email attachment


These 2 links have some sample code:

Quoted text here. Click to load it

Re: email attachment

petersprc wrote:

Quoted text here. Click to load it

You can also use the class phpmailer from sourceforge.

 MfG, Christian Welzel aka Gawain@Regenbogen

  Fingerprint: 4F50 19BF 3346 36A6 CFA9 DBDC C268 6D24 70A1 AD15

Re: email attachment

Quoted text here. Click to load it

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:

Hope this helps.


Re: email attachment

Quoted text here. Click to load it

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  

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

$fromname = "Name";
$fromaddress = ";
# 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;  
$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);
# 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.
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);
echo $mail_sent ? "Mail sent" : "Mail failed";
return $mail_sent;  

Re: email attachment

Ken wrote:
Quoted text here. Click to load it

When I run into these problems, I just send myself a copy of the message  
from my email program, then from the web site.  I then compare the  
source of both messages.  I always find what I did wrong.

Quoted text here. Click to load it

As you should.  sendmail_from is not a defined constant, variable or  
string.  The correct syntax is

   ini_set('sendmail from', $fromaddress);

But you should be using the From: header, as others have told you.

Quoted text here. Click to load it

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline