2

We exchange emails with various clients and vendors. I am trying to implement a solution where all the email communications between our rep and the client or vendor is saved in our database so in our admin system we have a record of the communications. I have seen similar system for some of the project sites like Guru.com or elance.com. You communicate back and forth via email, but the system logs the conversation.

I have been searching for a couple of days to try and figure out how to implement this. In studying emails form Guru.com I see they all go to a common email address. The subject contains an encrypted ID and "From: (freelancer's name or project owner's name)" So it seem that there is a milter on that box that parsers the body of that email, saves it to the DB, and forwards the email on to the recipient (from the subject line).

I use a vendor for my email, but run my own web server (EC2, AWS Linux), so I am thinking I can just create a second MX record for say "mail2" and use that for this application. Then I could use something like "parseme@mail2.mydomain.com" and have my server process those emails. I am thinking this can be done with a sendmail milter, but am not exactly sure.

My search for assistance or instruction on this has not turned up anything. I may not know the right term to search for. I have read about many milters, but none that seem to solve this. Can anyone offer any help or point me in the right direction?

Thanks

Thom

Thom
  • 35
  • 6
  • Have you checked the archive milters here? https://www.milter.org/milters/archiving/alphabetical/1 – Mike B Aug 06 '14 at 01:15
  • Yes, I have read through all those and most seem to parse emails for SPAM or indexing in a mail DB. I am needing to parse the email body for the current text, insert it into a mySQL db, then send the message on to the real recipient, making the "FROM" the same server based email address. I did not find any of the milters on that site that sounded like they did this. There is a definite possibility that the milter language/speak is over my head and that those do what I need, but I did not find one. – Thom Aug 06 '14 at 02:22

1 Answers1

1

The typical flow is rather one of delivery. Your question is tagged so I assume a Procmail solution is acceptable.

You should not need any additional MX records or other shenanigans. Sendmail (or any modern MTA; I would recommend Postfix if you are not particularly married to Sendmail for legacy reasons) can run a script on an incoming message quite trivially.

When a message is accepted for delivery, the MDA (in your case, Sendmail) looks for any client hooks such as a .forward file. If one is found, the file is parsed, and any pipeline in the file is executed. This is how Procmail is typically invoked on legacy systems (although more recently, a canned recipe to read the user's .procmailrc and invoke Procmail if one is found is part of the standard Sendmail feature set).

Instead of Procmail, you could run a script of your own; or you could run the script from your .procmailrc (which is beneficial because Procmail takes care of a number of pesky error scenarios).

:0
| /path/to/script

Now, Procmail will open /path/to/script with the message as standard input, and assume that the script takes care of processing it (delivering it, and/or parsing and then discarding it).

Add a :c flag if you want Procmail to also save to $DEFAULT:

:0c
| /path/to/script

Maybe add the recipient address as a parameter to the script:

ADDR=`formail -rtzxTo:`
:0c
| /path/to/script "$ADDR"

Your script could be quite simple; I leave the database details to you, and show a simple Perl script to log each incoming message to a file.

#!/usr/bin/perl

use strict;
use warnings;

open (DB, ">>", "/path/to/file.db") or die "Complication: $!";
print DB "Correspondent: $ARGV[1]\n";
while (<>) {
    print DB;
}
close DB;

This is ideal for incoming email, but the simple and straightforward way to also get your outbound communications logged is simply to Bcc: the account address on outgoing messages. On the other hand, if you use a web interface for the interactions, you could do the writing from the web scripts, then generate an email and send it, and use Procmail (or something similar) only for the incoming part of the conversation. This would simplify the system somewhat, at the expense of sacrificing the possibility of using the system simply by writing emails, as would come quite naturally from this sort of system.

The above Perl script is simple enough -- and Procmail is versatile enough -- that you could do all of this from your .procmailrc, but for actual database processing, you will need a small external script in any event.

tripleee
  • 1,324
  • 3
  • 14
  • 24
  • Wow, triplee. Thanks for the great answer. I did intact mean postfix and not procmail. Since it is the web server, no one uses it for a mail server. I need this to run the script as you mention, figure out the recipient and send the mail on to them. Can I invoke the PERL script from postfix and then put it back in the queue to be sent? – Thom Aug 06 '14 at 21:29
  • That's not how you do it. You'd run the script on whichever box is already processing your email. Send the email (with the proper `Bcc:` or similar to make sure it reaches the correct inbox) and the script will run. – tripleee Aug 06 '14 at 21:33
  • From the admin system, I could send al emails with a bcc, but when the two users reply back and for those emails will not have the bcc. – Thom Aug 09 '14 at 20:02
  • No, but it will have `To:` set up to reply back to your system; that's the whole point of this set-up. Have the email delivered to the database, then maybe run a trigger of some sort to have the system process it. – tripleee Aug 10 '14 at 06:33