Always attach file from server to email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ecco_locco
Forum Newbie
Posts: 5
Joined: Tue Aug 09, 2005 9:20 pm
Location: Rogues Island

Always attach file from server to email

Post by ecco_locco »

Hi

I have users fill in many forms. The form response is sent to me via email (plain text), autoresponded to the submitter(plain text) and written to the server as .csv file.

Works great everytime-

but now instead of me ftp'ing in and getting the csv file I really need to have the csv file sent to me as an attachment and I still need the (plain text) form submission with it.

I can not for the life of me figure out how to add this .csv file.

I know the absolute path to the file residing on the server.


TIA
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

search the forums for attachments.. we've talked about this topic a few times..
User avatar
ecco_locco
Forum Newbie
Posts: 5
Joined: Tue Aug 09, 2005 9:20 pm
Location: Rogues Island

quick reply Thanks!

Post by ecco_locco »

but instead of completely re-writting fully functional code
<edit> /* I hoped to elicit some -if this then that else replies </edit>

my searching has at least brought me this far -it's been over 4 days of googling everyquery I can imagine

It seems that my search for attachments almost alway brings up files that are posted -my file resides on the server //
"how to always add file to email as an attachment"

I'll try searching again
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

viewtopic.php?t=34242

you'll have to excuse the code looking a boogered, the forums just went through an upgrade so we're still bringing a few things back online.

You'll have to translate some of the code a bit, such as & changes to & and &#1111; changes to [
User avatar
ecco_locco
Forum Newbie
Posts: 5
Joined: Tue Aug 09, 2005 9:20 pm
Location: Rogues Island

Saw that

Post by ecco_locco »

Yeah I searched that post earlier but the captian hook + "are" arrrrr matey
+ the &amp &1111; made me look elsewhere

I'm just trying to attach a file from a server to an email - I think it's my syntax and not declaring the headers right -- my form is sent

Code: Select all

"Mime-Version: 1.0\r\nFrom: $_send_from\r\nContent-Type: multipart/mixed;\n boundary=\"$mime_delimiter\"\r\nContent-Disposition: inline");
in the forum now i see everystandalone "letterR" is represented as \r


the struggle survives
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I went through and translated it to a "useable" state.. still have to fix the \r to \r however...

You can also look at phpmailer which has the ability to add attachments.
User avatar
ecco_locco
Forum Newbie
Posts: 5
Joined: Tue Aug 09, 2005 9:20 pm
Location: Rogues Island

Readable!

Post by ecco_locco »

Thanks feyd for making it readable!@!

_ i was cursing even using my favorite editor html-kit

My path is to the csv file is

/home/.foo/somemorefoo/foo.com/foofolder/process.csv/

I want that file in my email and I still am clueless as to how to reference this absolute path using that email class (below)

Code: Select all

/***************/
    /* Email Class */
    /***************/
    
    class Email {
    
        var $info;
        var $files;
        var $mimeBoundary;
        
        function Email ( $to, $from, $subject, $message ) {
            $this->info = array( "To" => $to, "From" => $from, "Subject" => $subject, "Message" => $message );
            $this->mimeBoundary = "<<<--==+X?".md5(time())."]";
            $files = array();
        }
        
        function addAttachment ( $filename, $file, $mimetype ) {
            $this->files[$filename] = array( "MIME" => $mimetype, "FILE" => $file );
        }
        
        function sendEmail ( ) {
            $headers = "From: {$this->info['From']}\are\n";
            $headers .= "To: {$this->info['To']}\are\n";
            $headers .= "MIME-Version: 1.0\are\n";
            $headers .= "Content-Type: multipart/mixed;\are\n";
            $headers .= " boundary=\"".$this->mimeBoundary."\"";
            
            $message = "This is a multi-part message in MIME format.  Your email system does not support this feature.  Please contact sender for information.\are\n";
            $message .= "\are\n--".$this->mimeBoundary."\are\n";
            $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\are\n";
            $message .= "Content-Transfer-Encoding: 7bit\are\n";
            $message .= "\are\n{$this->info['Message']}\are\n";
            $message .= "--".$this->mimeBoundary;
            
            foreach ( $this->files as $filename => $file ) {
                $message .= "\are\nContent-Type: {$this->info['MIME']}; name=\"$filename\"\are\n";
                $message .= "Content-Transfer-Encoding: base64\are\n";
                $message .= "Content-Disposition: attachment; filename=\"$filename\"\are\n\are\n";
                $base64 = base64_encode($file['FILE']);
                for ( $i=0; $i < strlen($base64) / 76; $i++ )
                    $message .= substr( $base64, $i*76, 76 )."\are\n";
                $message .= "--".$this->mimeBoundary;
            }
            
            echo "\nTo: {$this->info['To']}";
            echo "\nSubject: {$this->info['Subject']}";
            echo "\n\n$headers";
            echo "\n\n$message";
            
            echo mail ( $this->info?"To"], $this->info?"Subject"], $message, $headers ) == true;
            
        }
    
    }
?>
I'm afraid I can't get to
/home/.foo/somemorefoo/foo.com/foofolder/process.csv/
with the code above

Don't I have to give an absolute path?

I've been disassembling phpmailer and I've yet to find a place that I would enter

/home/.foo/somemorefoo/foo.com/foofolder/process.csv/

I've got to referrence

/home/.foo/somemorefoo/foo.com/foofolder/process.csv/
somehow
No?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there were a few more old style mistakes in the code that I just fixed after looking at your copied version.. anyway..

after creating the object you call addAttachment(). The first argument is the filename you want to give the email client, while the second is the actual path to the file to use, and the third is the mime-type to send it as.


again, you may want to go back to the original thread and copy the code again to make sure you just need to replace \r with \r
User avatar
ecco_locco
Forum Newbie
Posts: 5
Joined: Tue Aug 09, 2005 9:20 pm
Location: Rogues Island

getting closer

Post by ecco_locco »

It's a wee bit late for me but I will go line by line

?Is it wrong to want to go by value rather than referrance?
I 'd rather point
/home/.foo/somemorefoo/foo.com/foofolder/process.csv/
then depreciate $this
$file
$filename
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

o.O
you simple do something like this:

Code: Select all

$mailer =& new Email("someone@yahoo.coma","jjones@cowboys.coma","I saw you at the game","Yo, I saw you at that game the other day with those guys and that dude was like 'wow!'");

$mailer->addAttachment('data.csv','/home/.foo/somemorefoo/foo.com/foofolder/process.csv', 'text/csv');

$mailer->sendMail();
Post Reply