2016-09-29 12 views
0

電子メールと添付ファイルを処理するためにwp_mail()を使用します。アップロードされた添付ファイルが一時的に保存され、電子メールが送信された後で削除される場合は、私が好むでしょう。現在、アップロード/添付ファイルはサーバーに保存されます。これを防ぐ方法は?Wordpress wp_mailは一時的に添付ファイルを保存し、電子メールを送信後に削除します

if(isset($_POST[ 'Save' ])) { 

function my_custom_email_content_type($content_type) { 
    return 'text/html'; 
} 

if (! function_exists('wp_handle_upload')) { 
    require_once(ABSPATH . 'wp-admin/includes/file.php'); 
} 

$files = $_FILES[ 'my_files' ]; 
$upload_overrides = array('test_form' => false); 

$attachments = array(); 

foreach ($files['name'] as $key => $value) { 
    if ($files[ 'name' ][ $key ]) { 
     $file = array(
      'name'  => $files[ 'name' ][ $key ], 
      'type'  => $files[ 'type' ][ $key ], 
      'tmp_name' => $files[ 'tmp_name' ][ $key ], 
      'error' => $files[ 'error' ][ $key ], 
      'size'  => $files[ 'size' ][ $key ] 
     ); 
     $movefile = wp_handle_upload(
      $file, 
      $upload_overrides 
     ); 
     $attachments[] = $movefile[ 'file' ]; 
    } 
} 

$to  = '[email protected]'; 
$subject = 'Contact Us'; 
$message = 'Haiii'; 
$headers[] = 'From: ' . get_option('blogname') . ' <[email protected]>'; 

add_filter(
    'wp_mail_content_type', 
    'my_custom_email_content_type' 
); 
$wp_mail_return = wp_mail(
    $to, 
    $subject, 
    $message, 
    $headers, 
    $attachments 
); 
if($wp_mail_return) { 
    echo 'Mail send'; 
} else { 
    echo 'Failed'; 
} 
remove_filter(
    'wp_mail_content_type', 
    'my_custom_email_content_type' 
); 
} 

答えて

1

正常に送信するかどうか気にしますか?

ない場合は、ちょうどこの操作を行います。

if($wp_mail_return) { 
    echo 'Mail send'; 
} else { 
    echo 'Failed'; 
} 

// Loops over the attachments (per your array), and removes the file 
foreach ((array)$attachments AS $file) { 
    unlink($file); 
} 

あなただけそれが正常に送信されます場合はこれが実行したい場合は、単にあなたの状態ならば内側に移動します。

if($wp_mail_return) { 
    echo 'Mail send'; 
    // Loops over the attachments (per your array), and removes the file 
    foreach ((array)$attachments AS $file) { 
     unlink($file); 
    } 
} else { 
    echo 'Failed'; 
} 
+0

リンクを解除しないだけでDELETファイル名?たぶん私は間違っています。 –

+0

です。しかし、私は、ドキュメントに基づいて、ファイルに関連付けられた投稿はないと思う:https://codex.wordpress.org/Function_Reference/wp_handle_upload –

関連する問題