Question

Mail::DKIM how to sign emails before sending?

Asked by: itcdr

I'm trying to setup DKIM to verify the emails I send.

1) I installed the dkim project [ http://sourceforge.net/projects/dkim-milter/ ]

2) I setup the public key in DNS and started it up

3) then I installed the perl module Mail::DKIM. I believe that's what I need to sign the emails.

now what? I just want to be able to send emails with PHP without it ending up in the user's spam folder. I think I'm close just not sure what's the last step

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2007-09-17 at 04:25:06ID22832832
Tags

dkim

,

dns

,

emails

,

how

,

signing

Topics

Perl Programming Language

,

SendMail Email Server

,

PHP Scripting Language

Participating Experts
2
Points
500
Comments
18

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. sendmail Milter
    Dear experts I have installed sendmail 8.12.3 on Sun 8 system. It seems to be fine in the sending functions. Does any know any the email blocking ? As far as I learn that the Milter function in sendmail can do the email blocking. Please advise the setup / configuration c...
  2. Spam Assassin
    Okay i have two server, both running Spamassassin, with two different configure files. One server handles Spam GREAT! I mean it is really good. But it is running MailScanner with Spamassassin So i am not sure if MailScanner is stopping all the spam or if it is Spamassassin...
  3. PHP Mail - get past spam filters with domainkeys
    I'm sending a verification email with PHP each time a new user creates an account. problem is only about 80% of the users are getting the email. I'm guessing for the others it's getting caught in their bulk/spam folders. (many of the users saying they didn't receive it are us...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Adam314Posted on 2007-09-17 at 08:29:52ID: 19905713

The Mail::DKIM module is for use in Perl.  Do you want to use perl to send the e-mail, or PHP?

 

by: itcdrPosted on 2007-09-17 at 08:37:32ID: 19905769

I just have to call it from a PHP page, so can be either.

the only emails that will be sent from the server is for new members and newsletters. the server won't receive email. am I on the right path?

 

by: Adam314Posted on 2007-09-17 at 09:16:09ID: 19906069

Well, you said you just wanted to not have the e-mails end up in a users spam folder.  Whether a message is flagged as spam or not is up to the client.  I'm not familiar with the dkim project, and don't know if any mail clients are either, so this may or may not help solve that problem.

Either way, there is an example of how to send a signed message on the Mail::DKIM::Signer documentation page:
http://search.cpan.org/~jaslong/Mail-DKIM-0.28/lib/Mail/DKIM/Signer.pm


 

by: MasonWolfPosted on 2007-09-17 at 10:21:06ID: 19906581

As you have found out yourself, implementing DKIM is hard. For this reason, very few email senders have implemented it. I tried to do this myself and eventually realized it was going to be too difficult.

That in mind, however, it's worth mentioning that few email recipients verify messages for DKIM signatures. And even with those that do, a lack of DKIM signature is not going to be enough to stick a message in the spam box. If it were, their customers would lose about 90% of their legitimate email, and that's a conservative estimate.

DKIM is a great idea in theory, because not only does it verify the identity of the sender, it also verifies that the entire message, including headers, has arrived without modification. But with header modification still not uncommon through entirely legitimate mechanisms, such as at an ISP relay, then when a DKIM signature actually is present, even a failed authentication is not sufficient cause to a message to be sent to the spam folder.

The point I'm trying to make is that, although DKIM may one day become a standard litmus test on all email messages, that day is still well in the future. When free, open-source DKIM authentication tools are widely available, when ISP's stop modifying headers, and when the majority of senders are finally signing their outgoing messages, that's when you'll want to make sure you're using it too. But when that day arrives, it'll be easy to do, and you won't need to ask the people at EE how to do it.

Getting back to your original problem, it sounds like your ip address has been blacklisted. This is the first thing you need to address. There are a number of free and paid services you can use to monitor whether your sending ip is blacklisted, and provide you with information to contact the list administrators to find out how to get off. If you're on a shared ip, this will be something you'll need to take up with your host provider. When we had a similar situation, we were able to research and find specific details of how and why our ip was blacklisted (one of the other customers on our server sent out a bunch of "phishing" emails - yuck).

Even once you're no longer blacklisted, you'll still want to make sure that people receiving your messages know that they're really from you and not from someone spoofing your domain. For this, use Sender-ID. It's the Microsoft approach to authentication, and while less secure, it's also really easy and most senders have implemented it.

Sender-ID works by publishing a TXT record in your DNS entry. It tells the email handler on your customer's end that "if my domain really sent an email to you, then it would have come from this IP address: xxx.xxx.xxx.xxx." The email handler then checks to see whether the IP address the message originated with matches the address your policy says it should. If not, then the policy will either state "It still might be from me" or "It definitely isn't from me". In the second case, the email will be discarded. In the first case, other rules come into effect as if the Sender-ID policy weren't there.

But the point is that if it really is from you, then the email handler looks up your reputation profile. If your messages routinely get retrieved from spam folders and marked "Not Spam" then you're going to get through. If you messages are routinely marked as "This is Spam" then you won't. And that's the way it should be. If you're sending out good content-filled newsletters that your customers want to receive, then you're going to be just fine.

Here's the definitive guide to implementing Sender-ID:
http://www.microsoft.com/mscorp/safety/technologies/senderid/resources.mspx

Good luck

 

by: itcdrPosted on 2007-09-17 at 11:29:59ID: 19907057

thanks for the detailed responses. and you're right DKIM is a HUGE pain to setup.

1) I had already setup Sender-ID. all I did was add the following SPF record to say that my IP for my server can send email. that's all I needed, right?
@
TXT
v=spf1 a ?all


2) I'm NOT on a shared IP. I've recently moved to a new server with a new IP and still many users with Hotmail, Yahoo and AOL complain they're not receiving it or it's in their spam folder. so don't think it's the IP. although how can I confirm?

do you still think DKIM won't solve the problem? I read Yahoo supports it, which is why I originally starting trying to set it up.

 

by: MasonWolfPosted on 2007-09-17 at 12:03:00ID: 19907351

Here's one resource for checking whether your ip is blacklisted: http://www.mxtoolbox.com/blacklists.aspx

There are others though. I found that one in Google with the query "blacklist ip check"

Did you test that your Sender-ID policy passed? The link I gave previously should have helped with that. I can't read a sender id policy record from memory, but if the wizard says it's right I'd trust it.

As for the others, it's tricky to get through to AOL, no matter what you do. I share your frustration on that score. But Yahoo and Hotmail ought to let you through so long as their users aren't marking your emails as spam.

Still, I want to avoid sounding like an authority on this subject, because I'm not. I'm just a fellow webmaster who had to deal with similar issues. I tried the DKIM approach and finally gave up. But our delivery rates went up noticeably when I implemented Sender-ID, which is why I advised you how I did.

And by the way, in case you find the answer on your own without the help of an expert, please be sure to let us know how you did it. It still bothers me that I never succeeded.

 

by: itcdrPosted on 2007-09-17 at 13:43:32ID: 19908261

thanks MasonWolf for the continued help

1) I checked the blacklists, they all shows as "OK"

2) I sent an email to my gmail account and in the header it shows "Received-SPF: pass (google.com: domain of my_email designates my_server_ip as permitted sender)". so then I think that confirms Sender-ID works

3) my only idea left is to try and get DKIM working. After tons of problems I finally got it installed and setup in DNS without errors. now I think I just need to somehow sign the emails. I don't see how to use Mail::DKIM to do it with perl. and I couldn't find any PHP solutions. any ideas?

I have been putting off this problem for sometime. I would like to once and for all get this solved. I'm sure most webmasters have frustrations with this

 

by: itcdrPosted on 2007-09-17 at 13:51:14ID: 19908324

I was able to send an email with perl

#!/usr/bin/perl

open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL "To: $to\nFrom: $from\nSubject: $subject\n$msg\n";
close(MAIL);

but I don't see where to implement the DKIM signer.

 

by: Adam314Posted on 2007-09-17 at 14:15:44ID: 19908485

use Mail::DKIM::Signer;

# create a signer object
my $dkim = Mail::DKIM::Signer->new(
                  Algorithm => "rsa-sha1",
                  Method => "relaxed",
                  Domain => "example.org",
                  Selector => "selector1",
                  KeyFile => "private.key");
             );

$dkim->PRINT("To: $to\n");
$dkim->PRINT("From: $from\n");
$dkim->PRINT("Subject: $subject\n");
$dkim->PRINT("$msg\n");
$dkim->CLOSE;

# what is the signature result?
my $signature = $dkim->signature;

 

by: itcdrPosted on 2007-09-17 at 14:21:06ID: 19908513

but how do I send the $signature in the email:

open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL "To: $to\nFrom: $from\nSubject: $subject\n$msg\n$signature\n"; #just add it to the end like this?
close(MAIL);

 

by: Adam314Posted on 2007-09-17 at 14:39:30ID: 19908624

my $signature = $dkim->signature, returns a Mail::DKIM::Signature object.
To get a signature string:
my $sig_string = $signature->as_string();

The signature should be prepended to the message to make the resulting message. At the very least, it should precede any headers that were signed.

open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL "$sig_string\nTo: $to\nFrom: $from\nSubject: $subject\n$msg\n";
close(MAIL);

 

by: itcdrPosted on 2007-09-17 at 15:47:32ID: 19909020

thanks Adam314. I was able to send an email with the signature, but it didn't verify right. I sent an email to an address which verifies DKIM. it sent back:

DKIM Test: permerror (verification error: key DNS reply corrupt)

do you see anything I'm missing? we're so close

--------------------------------------------
Original message:
Date: Mon, 17 Sep 2007 15:36:09 -0700
Message-Id: <200709172236.l8HMa94E018519@mydomain.com>
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=mydomain.com; h=to; q=dns/txt; s=dkim; bh=uoq1oCgLlTqpdDX/iUbLy7J1Wic=; b=c8v60Z56qFH2yeHCc6tdUtbMQS6qFqyJfvixNlut999mQsjLgIj5GuYAw0+/MZ8N9Ks9KfjGN8ATQnjWOG/jH12bc67vhpWLX1v7VE2wyy49EjDskJQls5ylZT580UbIq9yDGW1BGXMkpzrCbESwUGXkwr3WW+xrAs29azFtgOs=
To: autorespond+dkim@dk.elandsys.com
From: support@mydomain.com
Subject: Testing ...

This is a test message

 

by: Adam314Posted on 2007-09-18 at 08:48:59ID: 19913944

Are all of these parameters correct:
my $dkim = Mail::DKIM::Signer->new(
                  Algorithm => "rsa-sha1",
                  Method => "relaxed",
                  Domain => "example.org",
                  Selector => "selector1",
                  KeyFile => "private.key");
             );

Also, is the info that is PRINTed exactly the same as the info in the message?
$dkim->PRINT("To: $to\n");
$dkim->PRINT("From: $from\n");
$dkim->PRINT("Subject: $subject\n");
$dkim->PRINT("$msg\n");

 

by: itcdrPosted on 2007-09-18 at 08:56:17ID: 19914045

yes, tripled checked. all parameters are correct. I didn't get any errors sending the email. but the DKIM verifier couldn't validate the email

so I'm guessing either there's something else that needs to go in the header or something else I need to install. the error said "key DNS reply corrupt". so maybe the problem is after it receives the email and sending a command back to us to verify isn't working. what do you think?

 

by: Adam314Posted on 2007-09-18 at 10:48:36ID: 19915052

I haven't ever used DKIM, so I'm going only from the documentation.  

Is your public key properly setup in DNS?
If it was recently changed, it is possible your old DNS entry is still being used, and will be for a short time (because of DNS cacheing).  

 

by: itcdrPosted on 2007-09-18 at 11:40:48ID: 19915497

Here's my perl script. I tried a different dkim email verifier it came back with: "ERROR: Signed headers don't contain From header". not sure why such a different error than the other verifier. as for the DNS I added the single key to my domain as a TXT record when I installed DKIM like it said. Doesn't it also have to send a command back to the server to verify? If so, am I missing something?

---------------------------
#!/usr/bin/perl
use Mail::DKIM::Signer;
use warnings;
use strict;

##Email vars
my $to="dkim-test\@testing.dkim.org";
my $from= "support\@mydomain.org";
my $subject="Testing dkim";
my $msg="This is a test message";

##DKIM Processing
my $dkim = Mail::DKIM::Signer->new(Algorithm=>"rsa-sha1", Method=>"relaxed", Domain=>"mydomain.org", Selector=>"dkim", KeyFile=>"/usr/local/src/dkim-milter-2.2.1/dkim.private");
$dkim->PRINT("To: $to\nFrom: $from\nSubject: $subject\n$msg\n");
$dkim->CLOSE;
my $signature = $dkim->signature;
my $sig = $signature->as_string();


##Send Mail
open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL "$sig\nTo: $to\nFrom: $from\nSubject: $subject\n$msg\n";
close(MAIL);

print "done";

 

by: Adam314Posted on 2007-09-18 at 12:18:40ID: 19915775

The way it works:
    Setup (one time):
        You create a public/private key.
        You put your public key in your DNS entry.
        You keep your private key.
    When sending:
        You use your private key to generate a signature.
        You prepend your signature to your e-mail.
        You send the e-mail.
    When receiving:
        Receiver gets the message, and checks for a DKIM signature.
        If found, it gets your public key from your DNS record.
        Using your public key and signature on the e-mail, it verifies if the From in the e-mail is correct.

What is the output from this (put it just before the Send Mail, after you get the signature):

print "Headers=" . $dkim->headers() . "\n";
print "Originator=" . $dkim->message_originator->address . "\n";
print "Signature=$sig\n";

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...