You are here: Re: email attachment « PHP Programming Language « IT news, forums, messages
Re: email attachment

Posted by Jerry Stuckle on 08/29/07 11:26

Ken wrote:
> "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.
>

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация