Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- answer for: http://answers.yahoo.com/question/?qid=20110623172100AAOSnDt
- */
- /*
- * Functions
- */
- function cleanPost(){ // Gets the information from the $_POST variables, cleans them, then sends them back
- $a = func_get_args();
- $c = func_num_args();
- $m = get_magic_quotes_gpc();
- $r = Array();
- if($c > 0){
- for($i=0;$i<$c;++$i){
- $r[] = str_replace(',',',',(($m)? stripslashes($_POST[$a[$i]]) : $_POST[$a[$i]]));
- }
- return $r;
- }
- return false;
- }
- function mail_attachment($mailto, $subject, $message,$file) {
- // Modified From http://www.finalwebsites.com/forums/topic/php-e-mail-attachment-script Olafs Example
- // Good for only one file at a time.
- $fh = fopen($file, "r");
- $content = chunk_split( base64_encode( fread($fh, filesize( $file ))));
- fclose($fh);
- $uid = uniqid(time(),true);
- $filename = basename($file);
- $header = "From: Website Form <form@website.com>\r\n";
- $header .= "MIME-Version: 1.0\r\n";
- $header .= "Content-Type: multipart/mixed; boundary=\"{$uid}\"\r\n\r\n";
- $header .= "This is a multi-part message in MIME format.\r\n";
- $header .= "--{$uid}\r\n";
- $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
- $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
- $header .= "{$message}\r\n\r\n";
- $header .= "--{$uid}\r\n";
- $header .= "Content-Type: text/csv; name=\"{$filename}\"\r\n"; // use different content types here
- $header .= "Content-Transfer-Encoding: base64\r\n";
- $header .= "Content-Disposition: attachment; filename=\"{$filename}\"\r\n\r\n";
- $header .= $content."\r\n\r\n";
- $header .= "--".$uid."--";
- return mail($mailto, $subject, "", $header);
- }
- /*
- * Variables
- */
- list($surname,$middlename,$forename,$type,$area) = cleanPost('surname','middlename','forename','type','area'); // Will take $_POST[name] for each supplied name and create the same variable. So similar to writing $surname = $_POST['surname']; But easier and quicker.
- $tempFilename = "{$forename} {$middlename} {$surname}.csv";
- $tempFile = tempnam("/tmp", $tempFilename);
- $fileContents = "{$surname},{$middlename},{$forename},{$type},{$area}" . PHP_EOL;
- $email = Array(
- 'recipient' => 'membership_secretary@company.com', // The Email Address of the recipient
- 'subject' => 'New Application For Membership', // Subject Line
- 'message' => 'Info For New Applicant' . PHP_EOL . $fileContents // Email Message Body
- );
- /*
- * Temp File Creation & Population
- */
- $fh = fopen($tempFile,'w') or die('Couldn\'t create Temp file');
- fwrite($fh,$fileContents) or die('Couldn\'t write data to Temp file');
- fclose($fh) or die('Couldn\'t Close Temp File');
- /*
- * Mail Sending
- */
- if( mail_attachment($email['recipient'], $email['subject'], $email['message'],$tempFile)){
- // Email Was Successful
- echo 'Email Sent';
- }else{
- // Email Failled
- echo 'Email Error. Email was not sent';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement