Wordpress wp_mail临时保存附件并在发送电子邮件后删除
问题描述:
使用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'
);
}
答
你关心它是否成功发送?
如果没有,那么就这样做:
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';
}
不取消链接只是DELET文件名?也许我错了。 –
它的确如此。但是,我不认为有文章与文档相关,基于文档:https://codex.wordpress.org/Function_Reference/wp_handle_upload –