email attachment

email attachment

am 29.08.2007 03:42:46 von Ken

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

Re: email attachment

am 29.08.2007 04:05:01 von petersprc

Hi,

These 2 links have some sample code:

http://www.ipbwiki.com/Practical_PHP_Programming:Sending_mix ed-type_messages_with_PEAR::Mail_Mime
http://www.ipbwiki.com/Practical_PHP_Programming:Sending_att achments

On Aug 28, 9:42 pm, "Ken" 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

Re: email attachment

am 29.08.2007 04:12:54 von cathy

On Aug 28, 9:42 pm, "Ken" 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

Re: email attachment

am 29.08.2007 10:23:33 von Ken

"Cathy" wrote in message
news:1188353574.246266.224590@e9g2000prf.googlegroups.com...
> On Aug 28, 9:42 pm, "Ken" 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;

Re: email attachment

am 29.08.2007 10:31:35 von Christian Welzel

petersprc wrote:

> These 2 links have some sample code:

You can also use the class phpmailer from sourceforge.

--
MfG, Christian Welzel aka Gawain@Regenbogen

GPG-Key: http://www.camlann.de/key.asc
Fingerprint: 4F50 19BF 3346 36A6 CFA9 DBDC C268 6D24 70A1 AD15

Re: email attachment

am 29.08.2007 13:26:46 von Jerry Stuckle

Ken wrote:
> "Cathy" wrote in message
> news:1188353574.246266.224590@e9g2000prf.googlegroups.com...
>> On Aug 28, 9:42 pm, "Ken" 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.
>

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.

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

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.

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



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================