Php Wordpress wp_邮件发送后临时保存附件并删除
使用wp_mail()处理电子邮件和附件。我更喜欢上传的附件被临时存储,并且在发送电子邮件后被删除。目前,上传/附件保存在服务器上。如何预防Php Wordpress wp_邮件发送后临时保存附件并删除,php,wordpress,Php,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
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 = 'exampleadmin@gmail.com';
$subject = 'Contact Us';
$message = 'Haiii';
$headers[] = 'From: ' . get_option( 'blogname' ) . ' <fromid@gmail.com>';
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(设置($\u POST['Save'])){
功能我的\自定义\电子邮件\内容\类型($content\类型){
返回“text/html”;
}
如果(!function_存在('wp_handle_upload')){
require_once(ABSPATH.'wp admin/includes/file.php');
}
$files=$\u files['my\u files'];
$upload\u overrides=数组('test\u form'=>false);
$attachments=array();
foreach($files['name']作为$key=>$value){
如果($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\u handle\u上传(
$file,
$upload\u覆盖
);
$attachments[]=$movefile['file'];
}
}
$to$exampleadmin@gmail.com';
$subject='联系我们';
$message='haii';
$headers[]=“From:”。获取_选项('blogname');
添加过滤器(
“wp_邮件内容_类型”,
'我的\自定义\电子邮件\内容\类型'
);
$wp\u mail\u return=wp\u mail(
美元至,
$subject,
$message,
$headers,
$attachments
);
如果($wp\U邮件\U返回){
回音“邮件发送”;
}否则{
echo“失败”;
}
卸下过滤器(
“wp_邮件内容_类型”,
'我的\自定义\电子邮件\内容\类型'
);
}
您是否关心它是否成功发送
如果没有,则只需执行以下操作:
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条件内:
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';
}
取消链接不就是删除文件名吗?也许我错了,是的。然而,我不认为有一个帖子与文件相关,基于文档: