Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Generating Message_id email headers

by tobyink (Abbot)
on May 19, 2017 at 09:03 UTC ( #1190598=note: print w/replies, xml ) Need Help??

in reply to Generating Message_id email headers

Would be nice if one of the UUID modules shipped with core.

use UUID::Tiny qw/:std/; my $msgid = sprintf('<%s@%s>', create_uuid_as_string(), $ENV{HOST});

You might be able to shell out to uuidgen.

sub create_uuid_as_string { my $uuid = `uuidgen`; chomp $uuid; return $uuid; } my $msgid = sprintf('<%s@%s>', create_uuid_as_string(), $ENV{HOST});

Replies are listed 'Best First'.
Re^2: Generating Message_id email headers
by afoken (Abbot) on May 19, 2017 at 18:25 UTC

    No need to use UUIDs. Message IDs just have to be unique. Combining fully qualified hostname, timestamp (even if only with second resolution), process ID, and a random number should be sufficient.

    Let's look at some real-world data:

    Running grep -h -i --perl '^Message-ID' * in my inbox (in imapdir format, i.e. one file per mail, no special encoding) returns these (duplicate formats mostly removed, some names and numbers X-ed out):

    Message-ID: <> Message-ID: <re-pLd1axQBsDx57b7oqugsM5Ofj-2078PSB9-205B0OGX-ZD2Q32@new> Message-ID: <> Message-ID: <20170102172202.74342.42518@pde-live-app5> Message-ID: <> Message-ID: <> Message-ID: <> Message-ID: <CAJ5Xak6KKnmo-4HxLeNg1aGyRTkmYGdPM6ph=tfCxpg0j_s9gw@mail.> Message-Id: <> Message-ID: <> Message-ID: <286043904.95.XXXX7354XXXX.JavaMail.ggbader1@gtunxlvh00658> Message-ID: <> Message-ID: <> Message-ID: <1555628483.587.14XXX486XXX.JavaMail.amsap21@degtams2> Message-ID: <01020159f3ff5810-5fXXXXX-215f-4b49-b4dd-3a5eafXXXXX-00000> Message-ID: <> Message-ID: <E5-gkhm1jm5-nr113/XX/> Message-Id: <> Message-ID: <17997d6269704c37af1f86b072d23dc6@pollin-exchange.pollin-d +e.local> Message-ID: <1043052060.59.1488540102150.JavaMail.lithium@sjc1papp91.s> Message-ID: <> Message-Id: <> Message-Id: <E1d2qiD-0008PH-8Y@enterprise.foken.wtm> Message-ID: <> Message-ID: <001201d2c27b$206d5dd0$61481970$> Message-Id: <> Message-ID: <5.5e9852b0cf96ca350167@NY-WEB01> Message-ID: <8111f0b0-7d50-4564-b756-0a872e7361a5@chd1b02c-4776>

    Yes, there are UUIDs, almost always combined with a hostname. But there is also other more or less hashed data. Sometimes, its a plain timestamp (20170503161455 is obviously 2017-05-03 16:14:55) or a unix timestamp (1488540102150 is milliseconds since 1970 for 2017-03-03 11:21:42 UTC). is the address I wrote to, it was used as part of the message ID of the reply. And similar to boundary strings, you can find fragments of domain names, hostnames and user agent names in the message IDs, as well as bas64 and hex encoded "junk", and some seemingly random printable characters.


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Well, its turns out to be quite easy in the end.

      There is no required format for the Message Id, so it can be pretty much anything you like, but it does have to be unique. However, as has been noted, the general accepted format of a Message Id is $a(separator)$b@domain_name, where $a is essentially some form of timestamp and $b is some form of random element. So, here is what I created. The timestamp is generated from localtime and a random number from rand().

      my @dayofweek = (qw(Sun Mon Tue Wed Thu Fri Sat)); my @monthnames = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)) +; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday, $isdst) = localtime( +); $year += 1900; my $ran_num = int(rand(100000)); #This is a 5 figure random number whi +ch ensures the MID is really, really unique! # Create a Message Id my $MID = sprintf("<%04d:%02d:%s:%s-%02d:%02d:%02d-%05d\@company_name. +com>", $year, $mday, $monthnames[$mon], $dayofweek[$wday], $hour, $min, $sec, + $ran_num);

      And this is an example of what $MID looks like:


      spamassassin seemed to think this was ok and so lowered the spam score accordingly, so helping prevent my script-generated emails ending up in the spam folder.

      Many thanks to those who replied. I appreciated your comments and suggestions.


      Never said it needed to be a UUID. Using a UUID is just a very easy way to get an almost-certainly unique string.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1190598]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2018-04-21 23:44 GMT
Find Nodes?
    Voting Booth?