wordpress在发布后发送多封邮件

时间:2017-04-17 13:44:40

标签: wordpress

我在尝试什么

当文件上传到该用户时,我需要向用户发送邮件。我使用自定义开发的插件来实现这一目标。我使用postman SMTP发送邮件。

此处将文件添加为帖子。问题是当我发布它时,它会向分配的用户发送大约80多封邮件,其中只有1封邮件包含正确的信息。其他邮件都是空白的,只有静态的预定义邮件内容。 如何解决这个问题?

代码

add_action('save_post', 'upf_save_post');
function upf_save_post($post_id, $post = null) {
global $post;

/* --- security verification --- */  
if(!wp_verify_nonce($_POST['wp_upf_nonce'], plugin_basename(__FILE__)))
    return $post_id;  

if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    return $post_id;  

// if invalid $post object or post type is not 'userfile', return
if(!$post || get_post_type($post->ID) != 'userfile') return;

$user_info = get_userdata($_POST['upf_user']);
add_post_meta($post_id, 'upf_user', $user_info->user_login);
update_post_meta($post_id, 'upf_user', $user_info->user_login);

// Make sure the file array isn't empty
if(!empty($_FILES['upf_file']['name'])) {
    // Setup the array of supported file types. In this case, it's just PDF.
    $supported_types = array('application/pdf','image/png','image/jpg','image/jpeg','text/plain','text/html','application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/vnd.ms-excel','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.ms-powerpoint','application/vnd.openxmlformats-officedocument.presentationml.presentation',);

    // Get the file type of the upload
    $arr_file_type = wp_check_filetype(basename($_FILES['upf_file']['name']));

    $uploaded_type = $arr_file_type['type'];
    // Check if the type is supported. If not, throw an error.

    if(in_array($uploaded_type, $supported_types)) {
        $upf_file = get_post_meta($post_id, 'upf_file', true);
        if ($upf_file) {
            $upf_file_path = WP_CONTENT_DIR.'/userfiles/'.$upf_file['file'];
            if (file_exists($upf_file_path)) unlink($upf_file_path);
        }

        // Use the WordPress API to upload the file
        $upload = wp_handle_upload( $_FILES['upf_file'], array( 'test_form' => false ) );

        if(isset($upload['error']) && $upload['error'] != 0) {
            wp_die(__('There was an error uploading your file. The error is: ' . $upload['error'], 'user-private-files'));
        } else {
            // Update custom field
            $upload['file'] = substr($upload['file'],stripos($upload['file'],'wp-content/userfiles/')+21);
            add_post_meta($post_id, 'upf_file', $upload);
            update_post_meta($post_id, 'upf_file', $upload);
        } // end if/else
    } else {
        wp_die(__("The file type that you've uploaded is not supported.", 'user-private-files'));
    } // end if/else
} // end if


if ($_POST['upf_notify'] == '1') {
    if( get_post_meta($post_id, 'email_sent', 'true') != 'yes' ) {
    $upf_file = get_post_meta($post_id, 'upf_file', true);

    $email_subject = get_option('upf_email_subject');
    $email_msg = get_option('upf_email_message');

    $email_msg = str_replace('%blogname%', get_bloginfo('name'), $email_msg);
    $email_msg = str_replace('%siteurl%', get_bloginfo('url'), $email_msg);
    $email_msg = str_replace('%user_login%', $user_info->user_login, $email_msg);
    $email_msg = str_replace('%filename%', basename($upf_file['file']), $email_msg);
    $email_msg = str_replace('%download_url%', get_bloginfo('url').'/?upf=dl&id='.$post_id, $email_msg);

    $cats = wp_get_post_terms($post_id, 'file_categories', array("fields" => "names"));
    $email_msg = str_replace('%category%', implode(", ", $cats), $email_msg); 

    $headers[] ='From: "'.htmlspecialchars_decode(get_bloginfo('name'), ENT_QUOTES).'" <'.get_option('admin_email').'>';

    wp_mail($user_info->user_email, $email_subject, $email_msg, $headers);
    add_post_meta($post_id, 'email_sent', 'yes', true);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

这可能是因为在实际保存帖子之前,WordPress本身会触发save_post操作来保存帖子修订版。

  // If this is just a revision, don't send the email.
  if ( wp_is_post_revision( $post_id ) )
       return;

Wordpress在每个页面状态上触发此save_post操作 例如revisiondraftpublishpending 您应该根据工作情况在save_post行动中检查这些情况。 在这里查看代码 https://codex.wordpress.org/Plugin_API/Action_Reference/save_post 所以你应该检查