You certainly have the right hook, but keep in mind that you are hooking your functionality specifically to the draft_to_publish action, i.e. to the specific case of a post-object pre-existing in the database with draft status being updated to publish. Note that this action ignores drafts which are automatically saved by Wordpress when a new post is created - these "drafts" have a post_status of auto-draft.
I'm not exactly sure how you've been debugging the issue up to this point, but if you haven't already, I would recommend verifying that the action itself is firing when you expect it to, perhaps by attaching some simple, arbitrary, and obvious function to it:
function kill_wp( $post ) {
die( 'draft_to_publish fired for post #' . $post['ID'] . ' entitled, "' . $post['post_title'] . '"' );
}
add_action( 'draft_to_publish', 'kill_wp' );
That said, part of your problem may lie in capitalization - the action callback in your example references the function myFunction while the function that is defined has been named myfunction.
Though I am not sure as to what you are trying to accomplish, you could alternately attempt to attach your functionality to the generic action transition_post_status which gets passed the old status of the post, the new status of the post, and the post object such that you would have something similar to
function my_function( $strOldStatus, $strNewStatus, $post ) {
if( $post['post_type'] !== 'post' )
return; //Don't touch anything that's not a post (i.e. ignore links and attachments and whatnot )
//If some variety of a draft is being published, dispatch an email
if( ( $strOldStatus === 'draft' || $strOldStatus === 'auto-draft' ) && $strNewStatus === 'publish' ) {
$to = 'recipient@example.com';
$subject = 'Hi!';
$body = 'Hi,' . chr(10) . chr(10) . 'How are you?';
if( wp_mail($to, $subject, $body ) ) {
echo('<p>Message successfully sent!</p>');
} else {
echo('<p>Message delivery failed...</p>');
}
}
}
add_action('transition_post_status', 'my_function');
There are also a number of tools available that might grant you more insight into Wordpress' action execution such as the action hooks inspector for the Debug Bar plugin.
wp_mail()as you should? – toscho♦ Dec 28 '12 at 18:38