Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Generating Message_id email headers

by astrobal (Acolyte)
on May 18, 2017 at 12:19 UTC ( #1190527=perlquestion: print w/replies, xml ) Need Help??
astrobal has asked for the wisdom of the Perl Monks concerning the following question:

I crave the wisdom of the Monks.

I am using NET::SMTP script to send emails in HTML format, but as this is one of the tests of spamassassin I am getting close to or exceeding the 5.00 score to be classed as "spam". (One problem is that the date in the proper format is required these days, so that has to be generated and put into the headers. Doing so brings the score down by 0.6 it seems.)

Anyway, one header that we "should" have in the emails is the Message_ID. I cannot find a way to generate a Message_ID header and I wondered if anyone out there can help? My ISP do not have Email::Message_ID module on their list of modules

Thanks a lot


Replies are listed 'Best First'.
Re: Generating Message_id email headers
by haukex (Abbot) on May 18, 2017 at 12:49 UTC

    I would strongly recommend you look into getting the module installed, either locally into your account, or perhaps even by your hosting provider. See Yes, even you can use CPAN and local::lib. It's a pure-Perl module consisting of a single file, so in the worst case you could even just copy the file over.

    And in the very worst case, note that the guts of the module Email::MessageID are actually quite short. Although as I said I strongly suggest getting it installed, if you really have no choice, here is a boiled-down version:

      I would strongly recommend you look into getting the module installed

      Why would he even consider such "work" when you can feed him the pieces he's missing pea by pea?

        The module is pure-Perl, has no non-core dependencies, its core functionality is only those 11 lines of code I showed, and it's not doing anything magical or even all too complicated. Copying things over like that should be done very rarely, but in this case I consider it a pragmatic (but obviously not optimal) solution.

        "Nicht schön, aber selten."

Re: Generating Message_id email headers
by trippledubs (Chaplain) on May 18, 2017 at 13:11 UTC
    This may work
    $smtp->data(); $smtp->datasend("Message-ID: <".crypt(localtime,'$5$').">\n");

    See the end of RFC 2822 section 3.6.4.

    The message identifier (msg-id) itself MUST be a globally unique identifier for a message. The generator of the message identifier MUST guarantee that the msg-id is unique. There are several algorithms that can be used to accomplish this. Since the msg-id has a similar syntax to angle-addr (identical except that comments and folding white space are not allowed), a good method is to put the domain name (or a domain literal IP address) of the host on which the message identifier was created on the right hand side of the "@", and put a combination of the current absolute date and time along with some other currently unique (perhaps sequential) identifier available on the system (for example, a process id number) on the left hand side. Using a date on the left hand side and a domain name or domain literal on the right hand side makes it possible to guarantee uniqueness since no two hosts use the same domain name or IP address at the same time. Though other algorithms will work, it is RECOMMENDED that the right hand side contain some domain identifier (either of the host itself or otherwise) such that the generator of the message identifier can guarantee the uniqueness of the left hand side within the scope of that domain.

      Not all perls support sha256-based crypt, so actually, don't do this. Old crypt truncates the input at 8 characters, so you're actually only using the day-of-week and month.
Re: Generating Message_id email headers
by tobyink (Abbot) on May 19, 2017 at 09:03 UTC

    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});

      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.

Re: Generating Message_id email headers
by Anonymous Monk on May 18, 2017 at 23:21 UTC

    I crave the wisdom of the Monks.

    Obvious lie is obvious

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1190527]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2018-06-23 08:48 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.