About this topic

  • Posted by rg00dman 2 months ago. There are 2 posts. The latest reply is from rg00dman.

Tags

  1. Hi All

    I am trying to get postie to choose the category depending on who the email is from, I have tried a couple of mods to the code but I cant seem to get it to work. The kicker is I also want the -CATID- still to work as well, so the subject gets checked for the catid if not present it looks at the email address if thats not valid it goes to the default, that way I could override the email setting by setting a catid or just let it default.

    Hope this all makes sense and someone can help me

    WPV 3.2.1
    POSTIE V 1.4.2
    No link to website as internal at the moment only

  2. Managed to fix this myself its very messy and not user friendly but if you happy editing source files it seems to work very well, with one big warning, if you set an email address it will ignore any messages in the subject to set a category I had to do this otherwise it would have set one post to multiple category.

    You need to edit postie-functions.php around line 1970 you will see this code

    'function GetPostCategories(&$subject, $defaultCategory) {
    global $wpdb;
    $post_categories = array();
    $matches = array();
    //try and determine category
    if ( preg_match('/(.+): (.*)/', $subject, $matches)) {
    $subject = trim($matches[2]);
    $matches[1] = array($matches[1]);
    }
    else if (preg_match_all('/\[(.[^\[]*)\]/', $subject, $matches)) {
    preg_match("/](.[^\[]*)$/",$subject,$subject_matches);
    $subject = trim($subject_matches[1]);
    }
    else if ( preg_match_all('/-(.[^-]*)-/', $subject, $matches) ) {
    preg_match("/-(.[^-]*)$/",$subject,$subject_matches);
    $subject = trim($subject_matches[1]);
    }
    if (count($matches)) {
    foreach($matches[1] as $match) {
    $match = trim($match);
    $category = NULL;
    print("Working on $match\n");

    $sql_name = 'SELECT term_id
    FROM ' . $wpdb->terms. '
    WHERE name=\'' . addslashes($match) . '\'';
    $sql_id = 'SELECT term_id
    FROM ' . $wpdb->terms. '
    WHERE term_id=\'' . addslashes($match) . '\'';
    $sql_sub_name = 'SELECT term_id
    FROM ' . $wpdb->terms. '
    WHERE name LIKE \'' . addslashes($match) . '%\' limit 1';

    if ( $category = $wpdb->get_var($sql_name) ) {
    //then category is a named and found
    } elseif ( $category = $wpdb->get_var($sql_id) ) {
    //then cateogry was an ID and found
    } elseif ( $category = $wpdb->get_var($sql_sub_name) ) {
    //then cateogry is a start of a name and found
    }
    if ($category) {
    $post_categories[] = $category;
    }
    }
    }
    if (!count($post_categories)) {
    $post_categories[] = $defaultCategory;
    }
    return($post_categories);
    }'

    I have changed it to this

    'function GetPostCategories(&$subject, $defaultCategory, $mimeDecodedEmail) {
    $var1 = RemoveExtraCharactersInEmailAddress(trim($mimeDecodedEmail->headers["from"]));
    global $wpdb;
    $post_categories = array();
    $matches = array();
    //try and determine category
    //if ( preg_match('/(.+): (.*)/', $subject, $matches)) {
    // $subject = trim($matches[2]);
    //$matches[1] = array($matches[1]);
    //}
    //else if (preg_match_all('/\[(.[^\[]*)\]/', $subject, $matches)) {
    // preg_match("/](.[^\[]*)$/",$subject,$subject_matches);
    // $subject = trim($subject_matches[1]);
    //}
    if ( preg_match_all('/-(.[^-]*)-/', $subject, $matches) ) {
    preg_match("/-(.[^-]*)$/",$subject,$subject_matches);
    $subject = trim($subject_matches[1]);
    }
    if (count($matches)) {
    foreach($matches[1] as $match) {
    $match = trim($match);
    $category = NULL;
    print("Working on $match\n");

    $sql_name = 'SELECT term_id
    FROM ' . $wpdb->terms. '
    WHERE name=\'' . addslashes($match) . '\'';
    $sql_id = 'SELECT term_id
    FROM ' . $wpdb->terms. '
    WHERE term_id=\'' . addslashes($match) . '\'';
    $sql_sub_name = 'SELECT term_id
    FROM ' . $wpdb->terms. '
    WHERE name LIKE \'' . addslashes($match) . '%\' limit 1';

    if ( $category = $wpdb->get_var($sql_name) ) {
    //then category is a named and found
    } elseif ( $category = $wpdb->get_var($sql_id) ) {
    //then cateogry was an ID and found
    } elseif ( $category = $wpdb->get_var($sql_sub_name) ) {
    //then cateogry is a start of a name and found
    }
    if ($category) {
    $post_categories[] = $category;
    }
    }
    }
    if (!count($post_categories)) {
    $post_categories[] = $defaultCategory;
    }
    //email address one
    if ( preg_match('/name@domain.com/', $var1))
    {
    unset($post_categories);
    $post_categories[] = 3;
    }
    //email address two
    elseif ( preg_match('/email@domain.com/', $var1))
    {
    unset($post_categories);
    $post_categories[] = 4;
    }
    return($post_categories);

    print ("it is from $var1 \n");
    return($post_categories);
    }'

    You will notice I also // a couple of the early name matches so it only looks for -number- in the subject as I wont be able to control the subject and some emails come in with : or other randoms it destroyed them so that was my way of avoiding this. I presume you can put as many elseif in as you want but I have only done the two so far.

    Hope this helps

    Rob

RSS feed for this topic

Have you read the FAQ?
Have you tried the latest development version (at the bottom)?

Reply

You must log in to post.