Hey guys I have a variable $csvdata which has the following value : $cr = "\n"; $csvdata = "First Name" . ',' . "Last Name" . ' . $cr; $csvdata .= $txtFName . ',' . $txtLName . ' . $cr; I now want to send an email to myself with a .csv attachment containing this data, however I want the attachment to be created on the fly. Any ideas?
Have a look at base64_encode, and the comments below. There's an example on how to mail base64 encoded strings as attachment.
I couldnt work out how to solve my problem with the link, sorry nicro. What I have so far is a php file that will be used to send the email (with the csv file attached) and a php file doign the following : <?php //Define a carriage return $cr = "\n"; //Set the column names for the CSV file $csvcontent = "First Name" . ',' . "Last Name" . $cr; //Enter the values for each column $csvcontent .= $txtFName . ',' . $txtLName . $cr; header("Content-type: application/vnd.ms-excel"); header("Content-disposition: attachment; filename=temp.csv"); echo $csvcontent; ?> As i'm lead to believe, this code will create a temp.csv file on-the-fly (dynamically without saving to the server) I now need the mailing code to send an email to with the temp.csv as an attachment, using a 'require' to include the .csv creation php file. anyone?
Give this a try: <?php $cr = "\n"; $csvdata = "First Name" . ',' . "Last Name" . $cr; $csvdata .= $txtFName . ',' . $txtLName . $cr; $thisfile = 'file.csv'; $encoded = chunk_split(base64_encode($csvdata)); // create the email and send it off $subject = "File you requested from RRWH.com"; $from = "scripts@rrwh.com"; $headers = 'MIME-Version: 1.0' . "\n"; $headers .= 'Content-Type: multipart/mixed; boundary="----=_NextPart_001_0011_1234ABCD.4321FDAC"' . "\n"; $message = ' This is a multi-part message in MIME format. ------=_NextPart_001_0011_1234ABCD.4321FDAC Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Hello We have attached for you the PHP script that you requested from http://rrwh.com/scripts.php as a zip file. Regards ------=_NextPart_001_0011_1234ABCD.4321FDAC Content-Type: application/octet-stream; name="'; $message .= "$thisfile"; $message .= '" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="'; $message .= "$thisfile"; $message .= '" '; $message .= "$encoded"; $message .= ' ------=_NextPart_001_0011_1234ABCD.4321FDAC-- '; // now send the email mail($email, $subject, $message, $headers, "-f$from"); ?> PHP:
Amazing, simply amazing, took me several days of hacking, yet you seem to of solved the issue quickly, thanks a lot!, repped
I tried this, and it worked fine! The first time... Now I am getting an empty email, the from + subject works fine, but i am not getting any email body and no attachment, any ideas? here is the code :