I have coded a "follow post" script to which I wanna add the functionality of sending the user an email if a post they have followed has been updated. My script only sends an email if I hardcode an email into wp_mail(), e.g.

wp_mail('[email protected]', $subject, $message, $headers);

But when I when it dynamic with all the users who has followed the post, it doesn't send any email at all, my code looks like:

http://pastebin.com/yPirRwKn or:

function favorites_send_email($postid, $post) {    

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {

if ( wp_is_post_revision( $post_id ) ) {

$slug = 'journal';

if ( $slug != $post->post_type ) {

$posttitle = get_the_title($postid);
$posturl = get_permalink($postid);

$headers = "From: NAME <[email protected]>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

$subject = 'En af dine favoritter er blevet opdateret!';
$message = "Læs mere på nedenstående link (husk du skal logge på, hvis du er logget ud):\r\n";
$message .= "<a href='" . $posturl . "'>" . $posttitle . "</a>\r\n";

global $wpdb;

$table_name = get_table_name('favorite_posts');

$getusers = $wpdb->get_results("SELECT userid FROM " . $table_name . " WHERE postid = " . $postid);

foreach ($get_users as $get_user) {
    $theuser = get_userdata($get_user->userid);

    $email_data[] = array('name' => $theuser->display_name, 'email' => $theuser->user_email);

foreach($email_data as $data) {
    wp_mail($data["email"], $subject, $message, $headers);

add_action('save_post', 'favorites_send_email', 10, 3);

Any help is appreciated!

  • 2
    since you are not trying to send to the same people when you hardcode and when you generate dinamycally it is impossible to claim that there is any problem with the dynamic code. Most likely you try to send to many people and your host blocks it. Jan 29, 2015 at 5:21
  • Please do not use external links in your questions. When those are gone, the question will be hard to impossible to understand. File an edit and put the relevant parts here. If the content is the same, simply remove the link. Thanks.
    – kaiser
    Jan 30, 2015 at 7:24

1 Answer 1

$getusers -> $get_users

Simple mistake of wrong variable use!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.