Postfix Change Header From: outgoing mail using altermime

Biasanya From: header akan di isi oleh nilai dari settingan client masing2 user.

contoh: di thunderbird From: header akan di isi settingan dari Your Name:.

untuk email2 official From header bisa di paksakan supaya menggunakan nama user yg ada di database postfix user.
dengan bantuan altermime dan sedikit coding(c dan bash script).

altermime dapat di download disini:

www.pldaniels.com/altermime/

Coding c (access mysql db):

fungsinya untuk query field “name” (nama lengkap email user) di database postfix

paste code ini di console editor.(vi atau pico)

#include 
#include 
#include 
#include 

main(int argc,char *argv[]) {
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;

   char *server = "localhost";
   char *user = "user";
   char *password = "password";
   char *database = "db";
   char strsql[512];

   if(argc != 2)
   {
       printf("Usage: %s ’string query’\n", argv[0]);
       exit(EXIT_FAILURE);
   }

   snprintf(strsql, 512, "SELECT REPLACE(TRIM(name),’\n’,”) FROM mailbox WHERE username=TRIM(’%s’)", argv[1]);

   conn = mysql_init(NULL);

   /* Connect to database */
   if (!mysql_real_connect(conn, server,
         user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(EXIT_FAILURE);
   }

   /* send SQL query */
   if (mysql_query(conn, strsql)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(EXIT_FAILURE);
   }

   res = mysql_use_result(conn);

     while ( (row = mysql_fetch_row(res))  !=  NULL )
      printf("%s\n", row[0]);
   return(0);

   /* Release memory used to store results and close connection */
   mysql_free_result(res);
   mysql_close(conn);
}

compile source code c yg kita buat

# gcc -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient -L/usr/lib64 getname.c -o getname

-L dan -I path sesuaikan di path environtment mysql include dan libs masing2 system.

# strip getname

untuk membuang symbol symbol dari object file(supaya ukuran binary sizenya jadi lebih kecil)

# mv getname /etc/postfix/

test

# ./getname hari.h -at- csmcom.com
Hari Hendaryanto

dari program yg di jalankan berhasil meng query nama lengkap dari email hari.h -at- csmcom.com

note: sebenarnya bisa di query langsung dari command line/bash script yg akan di buat di bawah, tapi saya senang coba2

create directory dan user untuk filter yg akan kita buat

# adduser filter
# mkdir -p /var/spool/filter
# chown -R filter:filter /var/spool/filter/

selanjutnya buat bash script seperti ini

    #!/bin/sh
    # Localize these.
    INSPECT_DIR=/var/spool/filter
    SENDMAIL=/usr/sbin/sendmail.postfix

    # Exit codes from 
    EX_TEMPFAIL=75
    EX_UNAVAILABLE=69

    # Clean up when done or when aborting.
    trap "rm -f in.$$" 0 1 2 3 15

    # Start processing.
    cd $INSPECT_DIR || { echo $INSPECT_DIR does not exist; exit
    $EX_TEMPFAIL; }

    name="`/etc/postfix/getname $2 | sed -e ’s/^[ \t\n\r\f\v]//g’ | sed -e ’s/[ \t\n\r\f\v]$//g’`"
    email="`echo $2 | sed -e ’s/^[ \t\n\r\f\v]//g’ | sed -e ’s/[ \t\n\r\f\v]$//g’`"
    fheader="$name <$email>"

    cat >in.$$ || { echo Cannot save mail to file; exit $EX_TEMPFAIL; }

      /usr/bin/altermime –input=in.$$ \
                    –verbose \
                    –log-syslog \
                    –altersigned \
                    –alter-header="From: " \
                    –alter-with="$fheader" \
                    –alter-mode=replace || \
                    { echo Message content rejected; exit $EX_UNAVAILABLE; }

    $SENDMAIL "$@" 

save as chgname.sh

# chmod 755 chgname.sh
# mv chgname.sh /etc/postfix/

konfigur postfix.

create chgname transport di master.cf

chgname     unix  -       n       n       -       -       pipe
      flags=Rq user=filter argv=/etc/postfix/chgname.sh  -f ${sender} — ${recipient}

di main.cf

smtpd_recipient_restrictions =
            check_sender_access hash:/etc/postfix/sender_access
            permit_mynetworks,
            reject_unauth_destination

create file /etc/postfix/sender_access

ourdomain.com      FILTER chgname:
# postmap sender_access

pastikan semua urutan2 yg telah kita buat.

# postfix reload

selesai

3 Comments

  1. admin admin

    include sections:

    #include <mysql.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>

  2. eric

    i found attached xls file lost .  but it works well when i change the attached file name.
    old name: `chinese.xls` (used chinese as filename)
    new name: 1.xls
    can u fix it  ?   thanks.

    • admin admin

      well, it supposed not to strip anything from email body AFAIK.altermime only altering header. maybe you have another content filter stripping the attachment.

Leave a Reply

Your email address will not be published. Required fields are marked *