Advertisement
tareq1988

Send file by mail

May 10th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.92 KB | None | 0 0
  1. <?php
  2. if( isset( $_POST['submit_apply'] ) ) {
  3.     global $post, $userdata;
  4.    
  5.     //echo '<pre>'; print_r( $_POST ); echo '</pre>';
  6.     //echo '<pre>'; print_r( $_FILES ); echo '</pre>';
  7.    
  8.     $error = false;
  9.     $attachments = array();
  10.     $to = array();
  11.     $uploads = wp_upload_dir();
  12.     $uploads_dir = $uploads['path'];
  13.     $mime = array(
  14.         'application/pdf', 'application/msword',
  15.         'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  16.     );
  17.    
  18.     if( count( $_FILES ) ) {
  19.         foreach( $_FILES as $file ) {
  20.             if( $file ) {
  21.                
  22.                 if( !in_array( $file['type'], $mime ) ) {
  23.                     $error = true;
  24.                     break;
  25.                 }
  26.                
  27.                 //echo '<pre>'; print_r( $file ); echo '</pre>';
  28.                 //upload the files temporary and save the path in array
  29.                
  30.                 $path = $uploads_dir . '/' . $file['name'];
  31.                 if( move_uploaded_file( $file['tmp_name'], $path ) ) {
  32.                     $attachments[] = $path;
  33.                 }
  34.             }
  35.         }
  36.        
  37.         //exit;
  38.     }
  39.    
  40.     if( $error == false ) {
  41.        
  42.         if( $to_author = get_post_meta( $post->ID, 'job-email', true ) ) {
  43.             $to[] = $to_author;
  44.         }
  45.        
  46.         if( $_POST['send_copy'] == 'yes' ) {
  47.             $to[] = $userdata->user_email;
  48.         }
  49.        
  50.         $message = 'Iemand heeft zojuist gesolliciteerd op uw vacature:
  51.        "%s"
  52.        
  53.        In de bijlage van deze e-mail staan de documenten van de kandidaat. U kunt deze vacature op elk ogenblik sluiten of verwijderen door naar uw dashboard te gaan.
  54.        
  55.        De vacature zelf kunt hier bekijken: "%s"
  56.        ';
  57.        
  58.         $message = sprintf( $message, $post->post_title, get_permalink( $post->ID ) );
  59.        
  60.         $redir_url = '';
  61.         if( $attachments ) {
  62.             wp_mail( $to, 'Nieuwe sollicitant op Freelance.sr', $message, '', $attachments);
  63.             $redir_url = add_query_arg( array( 'msg' => 'yes' ) );
  64.            
  65.             //update the applicant list
  66.             $applicants = get_post_meta( $post->ID, 'applicants', true );
  67.             if( $applicants == '' ) {
  68.                 $user_list = "{$userdata->ID}";
  69.             } else {
  70.                 $user_list = $applicants . ",{$userdata->ID}";
  71.             }
  72.            
  73.             update_post_meta( $post->ID, 'applicants', $user_list);
  74.         }
  75.        
  76.         //echo '<pre>'; print_r( $to ); echo '</pre>';
  77.         //echo '<pre>'; print_r( $attachments ); echo '</pre>';
  78.        
  79.         //now delete the uploaded files
  80.         foreach( $attachments as $file ) {
  81.             if( file_exists( $file ) ) {
  82.                 unlink( $file );
  83.             }
  84.         }
  85.        
  86.         if( $redir_url ) {
  87.             wp_redirect( $redir_url );
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement