I'm building a small tracker and need to send mail notifications on specific actions.
Thought-out notifications:
- Administrator gets a notification email when a new ticket is published
- Author gets a notification email when the ticket is updated
- Author gets a confirmation mail when the ticket is published
Each notification email will have different content. I've got one of these three notifications to work, the one where the author gets a notification when a ticket is updated (code below). The other two I can't figure out.
Some background on posting in my tracker. The posting takes place in a front end form. If a user is logged in, his user details is used as the ticket author meta. If the user is not logged in, the name and email will be saved as custom fields.
//**************************************************
// Send mail to user when a ticket is updated
//**************************************************
function trac_authorEditNotification($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
if ($author->ID == '2') {
$message = "Hej ".get_post_meta($post->ID, 'ticket_poster', true).",\nDin rapport '".$post->post_title."' har just uppdaterats.\n\nDu kan besöka den uppdaterade rapporten på: ".get_permalink( $post->ID )."\n\nMvh\nTeamet";
wp_mail(get_post_meta($post->ID, 'ticket_poster_mail', true), "[Trac] Rapport uppdaterad (#".$post->ID.")", $message);
} else {
$message = "Hej ".$author->display_name.",\nDin rapport '".$post->post_title."' har just uppdaterats.\n\nDu kan besöka den uppdaterade rapporten på: ".get_permalink( $post->ID )."\n\nMvh\nTeamet";
wp_mail($author->user_email, "[Trac] Rapport uppdaterad (#".$post->ID.")", $message);
}
}
add_action('edit_post', 'trac_authorEditNotification');
Basically what I need is something similar to the above code for the other two notifications. I've tried a bunch of action hooks but nothings seems to do the trick. I'm getting no email at all or multiple hooks gets triggered and I'm ending up with to many emails.
Suggestions?
echo 'WORKS!';exit;
in your hooked functions to see if they work on update/edit, etc.