Skip to content

Instantly share code, notes, and snippets.

@imsoftware
Forked from jaltek/.a_readme.md
Last active January 18, 2017 01:35
Show Gist options
  • Select an option

  • Save imsoftware/20d77c586e8b5f928e39 to your computer and use it in GitHub Desktop.

Select an option

Save imsoftware/20d77c586e8b5f928e39 to your computer and use it in GitHub Desktop.

Revisions

  1. @jaltek jaltek revised this gist Oct 15, 2014. 2 changed files with 5 additions and 2 deletions.
    4 changes: 3 additions & 1 deletion .a_readme.md
    Original file line number Diff line number Diff line change
    @@ -33,4 +33,6 @@ Below you will find a single sample entry which will move e-mails from ```foobar
    It is important that you have read and understood the [basic configuration hints from the uberspace maildrop support wiki.](https://wiki.uberspace.de/mail:maildrop#basiskonfiguration)

    You can use the [below-mentioned script](https://gist.github.com/jaltek/799c913acb18e04809fb#file-myfilter) as your maildrop filter rule or enhance your existing script by adding the ```if ( /^From:\s*(.*)/ )``` code block. As mentioned before we are using the database corresponding to your uberspace account. So you have to change the variable ```DATABASE``` to your username.
    Last but not least be sure you set the correct rights to the filter rule (e.g. ```chmod 600 ~/.myfilter```).
    Last but not least be sure you set the correct rights to the filter rule (e.g. ```chmod 600 ~/.myfilter```).

    (Thx to Jonas from uberspace for his feedback)
    3 changes: 2 additions & 1 deletion .myfilter
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,8 @@ if ( /^From:\s*(.*)/ )
    {
    #filtering mail
    ADDRTMP=getaddr($MATCH1)
    ADDR=`echo $ADDRTMP | sed 's/^M//'`
    #escape address for security reasons
    ADDR=`printf '%q' $ADDRTMP | sed 's/^M//'`

    #mysql select
    RESULT=`echo -ne "select folder from mailfilter where email = '$ADDR';" | mysql $DATABASE --skip-column-names`
  2. @jaltek jaltek revised this gist Oct 14, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions .a_readme.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ This example is used in an [uberspace](https://uberspace.de) environment.

    ## Step 1: Create MySQL table

    For convenience we use the already existing database correspondig to your uberspace account (e.g. your uberspace username is *melanie* there will be already a database called *melanie*.
    For convenience we use the already existing database corresponding to your uberspace account (e.g. your uberspace username is *melanie* there will be already a database called *melanie*.

    mysql $USER << '__EOF__'
    CREATE TABLE `mailfilter` (
    @@ -32,5 +32,5 @@ Below you will find a single sample entry which will move e-mails from ```foobar

    It is important that you have read and understood the [basic configuration hints from the uberspace maildrop support wiki.](https://wiki.uberspace.de/mail:maildrop#basiskonfiguration)

    You can use the [below-mentioned script](https://gist.github.com/jaltek/799c913acb18e04809fb#file-myfilter) as your maildrop filter rule or enhance your existing script by adding the ```if ( /^From:\s*(.*)/ )``` code block. As mentioned before we are using the database correspondig to your uberspace account. So you have to change the variable ```DATABASE``` to your username.
    You can use the [below-mentioned script](https://gist.github.com/jaltek/799c913acb18e04809fb#file-myfilter) as your maildrop filter rule or enhance your existing script by adding the ```if ( /^From:\s*(.*)/ )``` code block. As mentioned before we are using the database corresponding to your uberspace account. So you have to change the variable ```DATABASE``` to your username.
    Last but not least be sure you set the correct rights to the filter rule (e.g. ```chmod 600 ~/.myfilter```).
  3. @jaltek jaltek revised this gist Oct 14, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .a_readme.md
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ Below you will find a single sample entry which will move e-mails from ```foobar

    ## Step 3: The maildrop rule

    It is important that you have read and understood the [basic configuration hints from the uberspace maildrop support wiki.](https://wiki.uberspace.de/mail:maildrop#basiskonfiguration).
    It is important that you have read and understood the [basic configuration hints from the uberspace maildrop support wiki.](https://wiki.uberspace.de/mail:maildrop#basiskonfiguration)

    You can use the [below-mentioned script](https://gist.github.com/jaltek/799c913acb18e04809fb#file-myfilter) as your maildrop filter rule or enhance your existing script by adding the ```if ( /^From:\s*(.*)/ )``` code block. As mentioned before we are using the database correspondig to your uberspace account. So you have to change the variable ```DATABASE``` to your username.
    Last but not least be sure you set the correct rights to the filter rule (e.g. ```chmod 600 ~/.myfilter```).
  4. @jaltek jaltek revised this gist Oct 14, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .a_readme.md
    Original file line number Diff line number Diff line change
    @@ -32,5 +32,5 @@ Below you will find a single sample entry which will move e-mails from ```foobar

    It is important that you have read and understood the [basic configuration hints from the uberspace maildrop support wiki.](https://wiki.uberspace.de/mail:maildrop#basiskonfiguration).

    You can use the [below-mentioned script](https://gist.github.com/jaltek/ecb7d89ace3666314081#file-myfilter) as your maildrop filter rule or enhance your existing script by adding the ```if ( /^From:\s*(.*)/ )``` code block. As mentioned before we are using the database correspondig to your uberspace account. So you have to change the variable ```DATABASE``` to your username.
    You can use the [below-mentioned script](https://gist.github.com/jaltek/799c913acb18e04809fb#file-myfilter) as your maildrop filter rule or enhance your existing script by adding the ```if ( /^From:\s*(.*)/ )``` code block. As mentioned before we are using the database correspondig to your uberspace account. So you have to change the variable ```DATABASE``` to your username.
    Last but not least be sure you set the correct rights to the filter rule (e.g. ```chmod 600 ~/.myfilter```).
  5. @jaltek jaltek created this gist Oct 14, 2014.
    36 changes: 36 additions & 0 deletions .a_readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    # uberspace, maildrop & MySQL
    ## A simple maildrop sender filter with MySQL backend

    This is a simple maildrop filter which filters incoming messages by the sender address and move it to the corresponding folder. E-Mail address and destination folder are both defined in a MySQL table.

    This example is used in an [uberspace](https://uberspace.de) environment.

    ## Step 1: Create MySQL table

    For convenience we use the already existing database correspondig to your uberspace account (e.g. your uberspace username is *melanie* there will be already a database called *melanie*.

    mysql $USER << '__EOF__'
    CREATE TABLE `mailfilter` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `folder` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    __EOF__


    ## Step 2: Create some entries

    Below you will find a single sample entry which will move e-mails from ```foobar@example.org``` to the folder ```Your_IMAP_Folder```:


    mysql $USER << '__EOF__'
    INSERT INTO mailfilter (email, folder) VALUES ('foobar@example.org', 'Your_IMAP_Folder');
    __EOF__

    ## Step 3: The maildrop rule

    It is important that you have read and understood the [basic configuration hints from the uberspace maildrop support wiki.](https://wiki.uberspace.de/mail:maildrop#basiskonfiguration).

    You can use the [below-mentioned script](https://gist.github.com/jaltek/ecb7d89ace3666314081#file-myfilter) as your maildrop filter rule or enhance your existing script by adding the ```if ( /^From:\s*(.*)/ )``` code block. As mentioned before we are using the database correspondig to your uberspace account. So you have to change the variable ```DATABASE``` to your username.
    Last but not least be sure you set the correct rights to the filter rule (e.g. ```chmod 600 ~/.myfilter```).
    46 changes: 46 additions & 0 deletions .myfilter
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    # set default Maildir
    MAILDIR="$HOME/Maildir"
    logfile "$HOME/mailfilter.log"

    #user settings
    DATABASE=<YOUR_UBERSPACE_USERNAME_HERE>

    # check if we're called from a .qmail-EXT instead of .qmail
    import EXT
    if ( $EXT )
    {
    # does a vmailmgr user named $EXT exist?
    # if yes, deliver mail to his Maildir instead
    CHECKMAILDIR = `dumpvuser $EXT | grep '^Directory' | awk '{ print $2 }'`
    if ( $CHECKMAILDIR )
    {
    MAILDIR="$HOME/$CHECKMAILDIR"
    }
    }

    # check from address against mysql
    if ( /^From:\s*(.*)/ )
    {
    #filtering mail
    ADDRTMP=getaddr($MATCH1)
    ADDR=`echo $ADDRTMP | sed 's/^M//'`

    #mysql select
    RESULT=`echo -ne "select folder from mailfilter where email = '$ADDR';" | mysql $DATABASE --skip-column-names`

    #folder for e-mail address found?
    if ($RESULT ne "")
    {
    MAILDIR="$MAILDIR/.$RESULT"
    `test -d "$MAILDIR"`
    if ( $RETURNCODE == 1 )
    {
    `maildirmake "$MAILDIR"`
    }

    to "$MAILDIR";
    }
    }

    # Standardregel:
    to "$MAILDIR"
    6 changes: 6 additions & 0 deletions mysql-init.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    CREATE TABLE `mailfilter` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `folder` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    1 change: 1 addition & 0 deletions mysql-sample.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    INSERT INTO mailfilter (email, folder) VALUES ('foobar@example.org', 'Your_IMAP_Folder');