Advertisement
gitlez

YA: mail() with file attachment

Jun 24th, 2011
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.99 KB | None | 0 0
  1. <?php
  2. /*
  3.     answer for: http://answers.yahoo.com/question/?qid=20110623172100AAOSnDt
  4. */
  5.  
  6. /*
  7. *        Functions
  8. */
  9. function cleanPost(){ // Gets the information from the $_POST variables, cleans them, then sends them back
  10.     $a = func_get_args();
  11.     $c = func_num_args();
  12.     $m = get_magic_quotes_gpc();
  13.     $r = Array();
  14.     if($c > 0){
  15.         for($i=0;$i<$c;++$i){
  16.             $r[] = str_replace(',','&#44;',(($m)? stripslashes($_POST[$a[$i]]) : $_POST[$a[$i]]));
  17.         }
  18.         return $r;
  19.     }
  20.     return false;    
  21. }
  22. function mail_attachment($mailto, $subject, $message,$file) {
  23.     // Modified From http://www.finalwebsites.com/forums/topic/php-e-mail-attachment-script  Olafs Example
  24.     // Good for only one file at a time.
  25.     $fh = fopen($file, "r");
  26.     $content = chunk_split( base64_encode( fread($fh, filesize( $file ))));
  27.     fclose($fh);
  28.     $uid = uniqid(time(),true);
  29.     $filename = basename($file);
  30.     $header = "From: Website Form <form@website.com>\r\n";
  31.     $header .= "MIME-Version: 1.0\r\n";
  32.     $header .= "Content-Type: multipart/mixed; boundary=\"{$uid}\"\r\n\r\n";
  33.     $header .= "This is a multi-part message in MIME format.\r\n";
  34.     $header .= "--{$uid}\r\n";
  35.     $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
  36.     $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
  37.     $header .= "{$message}\r\n\r\n";
  38.     $header .= "--{$uid}\r\n";
  39.     $header .= "Content-Type: text/csv; name=\"{$filename}\"\r\n"; // use different content types here
  40.     $header .= "Content-Transfer-Encoding: base64\r\n";
  41.     $header .= "Content-Disposition: attachment; filename=\"{$filename}\"\r\n\r\n";
  42.     $header .= $content."\r\n\r\n";
  43.     $header .= "--".$uid."--";
  44.     return mail($mailto, $subject, "", $header);
  45. }
  46.  
  47. /*
  48. *        Variables
  49. */
  50. 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.
  51. $tempFilename = "{$forename} {$middlename} {$surname}.csv";
  52. $tempFile = tempnam("/tmp", $tempFilename);
  53. $fileContents = "{$surname},{$middlename},{$forename},{$type},{$area}" . PHP_EOL;
  54. $email = Array(
  55.     'recipient' => 'membership_secretary@company.com', // The Email Address of the recipient
  56.     'subject' => 'New Application For Membership', // Subject Line
  57.     'message' => 'Info For New Applicant' . PHP_EOL . $fileContents // Email Message Body
  58. );
  59.  
  60. /*
  61. *        Temp File Creation & Population
  62. */
  63.  
  64. $fh = fopen($tempFile,'w') or die('Couldn\'t create Temp file');
  65. fwrite($fh,$fileContents) or die('Couldn\'t write data to Temp file');
  66. fclose($fh) or die('Couldn\'t Close Temp File');
  67.  
  68. /*
  69. *        Mail Sending
  70. */
  71.  
  72. if( mail_attachment($email['recipient'], $email['subject'], $email['message'],$tempFile)){
  73.     // Email Was Successful
  74.     echo 'Email Sent';
  75. }else{
  76.     // Email Failled
  77.     echo 'Email Error. Email was not sent';
  78. }
  79. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement