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

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

Geoffrey

Replies are listed 'Best First'.
Re: Generating Message_id email headers
by haukex (Prior) 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 (Hermit) 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.

      crypt(localtime,'$5$')
      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: <24746fb3-8c48-1412-5e8b-bfdb6735b214@foken.de> Message-ID: <re-pLd1axQBsDx57b7oqugsM5Ofj-2078PSB9-205B0OGX-ZD2Q32@new +sletter.lieferando.de> Message-ID: <8f31036b-d534-2c6d-fde4-ec03aa28b355@gmx.de> Message-ID: <20170102172202.74342.42518@pde-live-app5> Message-ID: <y-b-vryxQaigSHMT84qhHg@ismtpd0001p1lon1.sendgrid.net> Message-ID: <fe996f03-5674-4184-321a-cc1a4a1e83b5@cXXXXXd.de> Message-ID: <53149ca4-dc49-1a83-2878-d5ac005bee1d@foken.de> Message-ID: <CAJ5Xak6KKnmo-4HxLeNg1aGyRTkmYGdPM6ph=tfCxpg0j_s9gw@mail. +gmail.com> Message-Id: <20170105030539.A68907C4A0@srv162.bader.de> Message-ID: <1bd0c853-751c-d823-8e3b-147d588c296f@foken.de> Message-ID: <286043904.95.XXXX7354XXXX.JavaMail.ggbader1@gtunxlvh00658 +.server.arvato-systems.de> Message-ID: <m0b8cdt0E34Nqdu.RZmta@smtp.rzone.de> Message-ID: <20170122_102124_047518.vroom@perlmonks.org> Message-ID: <1555628483.587.14XXX486XXX.JavaMail.amsap21@degtams2> Message-ID: <01020159f3ff5810-5fXXXXX-215f-4b49-b4dd-3a5eafXXXXX-00000 +0@eu-west-1.amazonses.com> Message-ID: <5890EE9C.2000604@foken.de> Message-ID: <E5-gkhm1jm5-nr113/XX/XXXXX-18f0a3bd@wrk-1505.artegic.net> Message-Id: <20170213123704.2723380261@web3.po23.spacenet.de> Message-ID: <17997d6269704c37af1f86b072d23dc6@pollin-exchange.pollin-d +e.local> Message-ID: <1043052060.59.1488540102150.JavaMail.lithium@sjc1papp91.s +j.lithium.com> Message-ID: <ic974ihadr6t5xjxk1mstfvr.1489696175625@email.android.com> Message-Id: <15b576a93ed-3749-7fc5@webstg-a10.mail.aol.com> Message-Id: <E1d2qiD-0008PH-8Y@enterprise.foken.wtm> Message-ID: <D5261642.F9DB%kontakt@juXXXXXka.de> Message-ID: <001201d2c27b$206d5dd0$61481970$@gmx.de> Message-Id: <20170503161455.17E47482CA@dvm034.uptrade.de> 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). kontakt@juXXXXXka.de 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.

      Alexander

      --
      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:

        <2017:21:May:Sun-07:36:11-57049@company_name.com>

        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.

        Geoffrey

        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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1190527]
Approved by marto
help
Chatterbox?
[hippo]: Marketing should not be allowed near such utilities.
[Corion]: I should tell them about the "dim+lock all monitors to show the immediate alert centered" feature of that software so they can announce the next intranet website feature even better ;)
[Eily]: This sounds like the perfect medium to send "don't let anything distract you"
[LanX]: hippo you asked about translating Asterix ?

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (14)
As of 2017-05-24 14:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?