Support » Plugins and Hacks » Passing $post_ID to wp_mail(), please help!

  • I’m trying to cook up a small utility function in functions.php that will send an e-mail notification to a small set of defined addresses whenever a new post is published. Here’s what I have so far:

    if (!function_exists('email_notification')) {
    
    	function email_notification($post_ID) {
    
    		if (function_exists('similar_posts')) {
    
    			ob_start();
    			similar_posts();
    			$email_notification_similar = "<h3>Similar Posts</h3>".ob_get_contents();
    			$email_notification_similar = preg_replace('<code>[\r\n]+</code>', '', $email_notification_similar);
    			ob_end_clean();
    
    		}
    
    		$email_notification_to = "me@website.com";
    		$email_notification_subject = "New Article at ".get_bloginfo('name');
    		$email_notification_message = "This is an automated message notifying you that there has been a new article posted at ".get_bloginfo('name').".\n\nTitle: ".the_title('', '', FALSE)."\nAuthor: ".get_the_author()."\nURL: ".get_permalink()."\n\nPlease copy/paste the following HTML (the code in between the \"<!-- article -->\" and \"<!-- /article -->\" tags) into your web publishing software:\n\n\n\n<!-- article -->\n\n".wpautop(wptexturize(get_the_content()))."<blockquote><small>\n".$email_notification_similar."\n<p>This article is &copy; ".$copyright." by <a href=\"".$blog_owner_url."\" target=\"_blank\">".$blog_owner."</a> and was originally published at <a href=\"".get_permalink()."\" target=\"_blank\">".get_bloginfo('name')."</a>. This article may not be copied, distributed, or transmitted without attribution. Additionally, you may not use this article for commercial purposes or to generate derivative works without explicit written permission. Please <a href=\"mailto:".get_bloginfo('admin_email')."?subject=License%20Request%20for%20".str_replace(' ', '%20', the_title('','',FALSE))."\">contact us</a> if you wish to license this content for your own use.</p>\n</small></blockquote>\n\n<!-- /article -->\n\n\n\nLet us know if you have any questions or comments.\n\nThanks!\n\n-- \n\nMe\nWebsite\nme@website.com";
    
    		wp_mail($email_notification_to, $email_notification_subject, $email_notification_message);
    
       	}
    
    }
    
    add_action('new_to_publish', 'email_notification');
    add_action('draft_to_publish', 'email_notification');
    add_action('pending_to_publish', 'email_notification');
    add_action('future_to_publish', 'email_notification');

    I know I am not passing the $post_ID correctly and that is why the article-specific content isn’t being displayed in the resulting e-mail.

    Can anyone help me figure out how to clean up this function to make it work properly? Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Anyone?

    Latest iteration that’s not working:

    if (!function_exists('email_notification')) {
    
    	function email_notification($post_ID) {
    
    		global $wpdb;
    		$post_ID = $post->ID;
    
    		if (function_exists('similar_posts')) {
    
    			ob_start();
    			similar_posts();
    			$email_notification_similar = "<h3>Similar Posts</h3>".ob_get_contents();
    			$email_notification_similar = preg_replace('<code>[\r\n]+</code>', '', $email_notification_similar);
    			ob_end_clean();
    
    		}
    
    		$email_notification_to = "me@website.com";
    		$email_notification_subject = "New Article at ".get_bloginfo('name');
    		$email_notification_message = "This is an automated message notifying you that there has been a new article posted at ".get_bloginfo('name').".\n\nTitle: ".the_title('', '', FALSE)."\nAuthor: ".get_the_author()."\nURL: ".get_permalink()."\n\nPlease copy/paste the following HTML (the code in between the \"<!-- article -->\" and \"<!-- /article -->\" tags) into your web publishing software:\n\n\n\n<!-- article -->\n\n".wpautop(wptexturize(get_the_content()))."<blockquote><small>\n".$email_notification_similar."\n<p>This article is &copy; ".$copyright." by <a href=\"".$blog_owner_url."\" target=\"_blank\">".$blog_owner."</a> and was originally published at <a href=\"".get_permalink()."\" target=\"_blank\">".get_bloginfo('name')."</a>. This article may not be copied, distributed, or transmitted without attribution. Additionally, you may not use this article for commercial purposes or to generate derivative works without explicit written permission. Please <a href=\"mailto:".get_bloginfo('admin_email')."?subject=License%20Request%20for%20".str_replace(' ', '%20', the_title('','',FALSE))."\">contact us</a> if you wish to license this content for your own use.</p>\n</small></blockquote>\n\n<!-- /article -->\n\n\n\nLet us know if you have any questions or comments.\n\nThanks!\n\n-- \n\nMe\nWebsite\nme@website.com";
    
    		wp_mail($email_notification_to, $email_notification_subject, $email_notification_message);
    
       	}
    
    }
    
    add_action('new_to_publish', 'email_notification');
    add_action('draft_to_publish', 'email_notification');
    add_action('pending_to_publish', 'email_notification');
    add_action('future_to_publish', 'email_notification');

    i am trying this also

    Try these:

    add_action('new_to_publish', 'email_notification', 10, 1);
    add_action('draft_to_publish', 'email_notification', 10, 1);
    add_action('pending_to_publish', 'email_notification', 10, 1);
    add_action('future_to_publish', 'email_notification', 10, 1);

    Also, your actions are passed the post object, not just the ID.

    andrewkhunn, you should use

    function email_notification($post)

    and post id would be $post->ID

    It works for me.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Passing $post_ID to wp_mail(), please help!’ is closed to new replies.