How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site. Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Steve Your Own Question
Steve, Consultant
Category: Programming
Satisfied Customers: 290
Experience:  Steve is a consultant in the areas of computer software and programming, information management and networking.
Type Your Programming Question Here...
Steve is online now
A new question is answered every 9 seconds

I'm looking at this page about rewriting header fields

Customer Question

I'm looking at this page about rewriting header fields (Postfix) with pcre and header_checks and I have an unusual situation. VSIFax is sending a To: ***** ***** the server incorrectly formatted. For multiple addresses, it should be ,, etc. But VSIfax is sending it as (ellipses shown to illustrate additional email addresses). I need some kind of header_checks syntax that can convert this broken syntax to proper syntax either with ZERO angle brackets or having them properly placed around the email addresses. Is this possible?
Submitted: 1 year ago.
Category: Programming
Expert:  Bhavik Joshi replied 1 year ago.

Hi, I'm Bhavik. Welcome to JustAnswer. I'm reviewing your question now, and will reply back ASAP.

Expert:  Bhavik Joshi replied 1 year ago.

Yes, I can help you in doing this. Send me some sample data which you need to correct or take care and let me know in which language you need me to code for doing edit check ?

Customer: replied 1 year ago.
This is Postfix configuration. There are multiple methods for correcting headers, from pcre to regex, not specifically a "language". I have a vendor program (VSI-Fax from Esker Software) that cannot be modified. When it sends the "To:" header to Postfix, it sends the header as:To: <*****@******.***,*****@******.***,*****@******.***>When it should be:To: <*****@******.***>,<*****@******.***>,<*****@******.***>I need a pcre expression that can be inserted into header_checks that will convert the bad header into the right format.
Expert:  Bhavik Joshi replied 1 year ago.

okay, Got it ! Can you send me current pcre expression ? I would need to make couple of modifications in it.

Expert:  Bhavik Joshi replied 1 year ago.

okay, Got it ! Can you send me current pcre expression ? I would need to make couple of modifications in it.

Customer: replied 1 year ago.
There isn't a current PCRE expression. That's what I need to add to header_checks to solve this problem.
Expert:  Bhavik Joshi replied 1 year ago.

Okay, If you need a new code for this then I would say just add below line of code to the string which is accepting the header record :-

$str =~ s/,/>,/g;

This code will replace all the occurrence of , with >, which will ultimately help us in formatting the data.

Let me know how if you have any question or concerns.

Customer: replied 1 year ago.
It looks like you're writing perl, not header_checks configuration for Postfix. Do you know anything about Postfix? If not please step aside and let someone who knows something about Postfix answer this question.
Expert:  Bhavik Joshi replied 1 year ago.

Yes, I think I should opt out.

Expert:  Steve replied 12 months ago.

Hi there, my name is*****'m assuming that in you have:

header_checks = regexp:/etc/postfix/header_checks

Then you could use the following to fix the addresses in your header_checks file:

/<?([\w+-.%][email protected][\w\-\.]+\.[A-Za-z]{2,4})(,|>)/ REPLACE <$1>,

The above leaves a comma after the last email which I don't think causes a problem, but if it does, just add another line:

/(.*)(,$)/ REPLACE $1

If I've helped you solve your issue, can you please accept my answer and rate me accordingly? I don't get paid anything for helping you unless you accept my answer. Thanks!

Customer: replied 12 months ago.
This regex will strip out all but the first email address. If an MTA sends in multiple to addresses, it will not apply the < and > to each one. So this isn't really much of a solution, sorry.If postfix receives this:To: <*****@******.***,*****@******.***>I need it to be this:To:<*****@******.***>,<*****@******.***>
Expert:  Steve replied 12 months ago.

I think you're mistaken. Did you try it as a REPLACE?

The Regex I provided will turn this:

<*****@******.***,*****@******.***,*****@******.***> into this:


Using it as a REPLACE with the PCRE compatibility.

Expert:  Steve replied 12 months ago.

These two lines go into your header_checks file:

/<?([\w+-.%][email protected][\w\-\.]+\.[A-Za-z]{2,4})(,|>)/ REPLACE <$1>,

/(.*)(,$)/ REPLACE $1

If it doesn't work, can you please show me the log file or what the code produces instead of what you want?

Customer: replied 12 months ago.
Here's header_checks, copied from your solution:[email protected]:/etc/postfix# ***** header_checks
/<?([\w+-.%][email protected][\w\-\.]+\.[A-Za-z]{2,4})(,|>)/ REPLACE To: <$1>,
/(.*)(,$)/ REPLACE $1And here's how it works:# postmap -fq "To: <*****@******.***,*****@******.***>" pcre:header_checks
REPLACE To: <*****@******.***>,Note how everything following the first To: ***** ***** being lost.The second issue I have with this is that it's not specifically tied to the To: line; so it could have negative impacts on other, (potentially properly formatted) headers.If I knew that it was always <*****@******.***,*****@******.***,...> Then I can use this:/^To: <(.*)>/ REPLACE To: $(1)...which will strip out the leading < and trailing > and the end result works as expected. However, if I get a properly formatted To: ***** *****:To: <*****@******.***>,<*****@******.***> simplified expression will break the header. The end game here is this:To: <*****@******.***>,<*****@******.***> -- properly formatted, should be left alone
To: <*****@******.***,*****@******.***> -- improperly formatted, needs to be converted
To:***@******.***,*****@******.*** -- improperly formatted, but workable
To: <*****@******.***>,*****@******.*** -- improperly formatted, but workableThe only case where I need the line fixed is when there is one and only one set of angle braces. Perhaps this?/^To: <(.*)>$/ REPLACE To: $(1)This will strip the angles from single recipient emails, but that's still workable with Postfix.
Customer: replied 12 months ago.
Actually, this may be better:/^To: <([^<>]*)>$/ REPLACE To: $(1)
Expert:  Steve replied 12 months ago.

Is header_checks bound as pcre: (not regexp:) in

If it works just without the angle brackets as in your third example, above you can do this:

/^To:\s?<(.*)>/ REPLACE To: $(1)

The \s? is a safeguard in case there is no space or more than one space between the colon and the starting address.

Note that this will remove all your angle brackets, for all messages.

Customer: replied 12 months ago.
I appreciate your assistance, I truly do. But with all due respect, I think we're having a failure to communicate. In my earlier response I wrote:# postmap -fq "To: <*****@******.***,*****@******.***>" pcre:header_checks
REPLACE To: <*****@******.***>,And you asked "Is header_checks bound as pcre: (not regexp:) in" Had you considered my response you would have seen that I'm testing this, in fact, using pcre.In reference to this: /^To:\s?<(.*)>/ REPLACE To: $(1)You wrote "Note that this will remove all your angle brackets, for all messages." I appreciate the \s tip, but your pcre regex (which I do understand was my original regex) will *not* remove all angle brackets. Hence the problem. It will remove the first set of angle brackets, that's all.I'm starting to believe that what I really want to achieve isn't possible.
Expert:  Steve replied 12 months ago.

I'm sorry, I was just trying to double check that something wasn't overlooked.