Sending DKIM signed e-mail to Outlook.COM via PHP
Asked Answered
B

4

10

I have a mail server working well with SPF, DKIM and reverse DNS configured. I can send e-mails to Outlook.com using something like:

echo "This is only a test" | mail [email protected]

The problem occurs when I try to send e-mails via PHP using the same server:

$header .= "Return-Path: Some User <[email protected]>\r\n";
$header .= "From: Some User <[email protected]>\r\n";
$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "User-Agent: Some User Mail Sender\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n";

mail("[email protected]","My title", "Message body", $header);

I tried to verify my message using appmaildev.com and the report says:

DKIM result: fail (wrong body hash: <*** body hash ***>)

Even with this error, Outlook.com says it passed DKIM verification, but all messages sent by PHP mail function go to junk folder. Here are examples of messages sent directly via Bash and via PHP: http://pastebin.com/ndXJszic

Can anyone help me?

Thanks.

EDIT After removing \r from headers, the DKIM body hash error is gone. But I still can't send e-mails to Outlook...

Birdella answered 27/11, 2012 at 23:22 Comment(3)
You wish to install SpamAssassin and run your message through it to see if there are any red flags. DKIM generally doesn't help get past spam filters.Hymenium
You might also want to try sending emails to different domains. Some domains may drop the emails and others may accept.Diverting
Also review below one to get some more ideas.>>serverfault.com/questions/505182/…Jegger
P
1

This could be a permission issue.

Your web server normally runs as a different user than when you use mail on the command line, so setting the From: header will create additional warning headers in the outgoing email.

There's a file you can modify on your server called /etc/mail/trusted-users. Make sure that the user apache (or whatever user your php script is running as) appears in that file; if not, add the user name and then reload sendmail.

Example contents of /etc/mail/trusted-users:

# trusted-users - users that can send mail as others without a warning
# apache, mailman, majordomo, uucp, are good candidates
apache
Pester answered 26/12, 2012 at 9:59 Comment(0)
C
1

First is impossible to be sure that an email will not be marked as spam, the only way is that the person who receive the email add the sender address in a white list.

SPF and DKIM are only to guarantee that the email comes from that domain or email, but not to guarantee that it is not spam.

The antispam system in outlook.com as many others check a lot of things, for example the sender ip address, how many email comes from that ip per hour, the content of the email (text, links), reputation, etc.

In your example you don't show the body content, maybe it's different and for this reason one email is marked as spam and the other it's not.

Cancellation answered 14/5, 2013 at 18:37 Comment(1)
I think there is a message body: "Message Body" ;)Incubator
L
0

Try using Pear mail, and making a wrapper class around that. I'm using that with DKIM, and have no problems. I should mention that I am also using SpamAssassin (as previously mentioned), and ClamAV.

<?php

// Include the Pear Mail header
require_once '/usr/share/php/Mail.php';


class MailWrapper {
    public static function Send($to, $subject, $body) {
        // Email details
        $from = 'No Reply <[email protected]>';
        $server = 'mail.yourdomain.com';
        $port = 25;
        $username = '[email protected]';
        $password = 'yourp4ssw0rd';
        // Formalize mail server connection info
        $headers = array('From' => $from,
                 'To' => $to,
                 'Subject' => $subject,
                 'Date' => date('r'),
                 'Return-Path' => $from,
                 'Content-Type' => 'text/html; charset=UTF-8',
                 'Content-Transfer-Encoding' => '7bit',
                 'Mime-Version' => '1.0',
                 'X-Mailer' => 'Your Company (https://yourdomain.com)',
                 'X-Accept-Language' => 'en',
                 'Message-ID' => sha1($body).'@yourdomain.com'
        );
        $connection = array('host' => $server,
                    'auth' => true,
                    'username' => $username,
                    'password' => $password
        );
        // Create the mail server connection 
        $smtp = Mail::factory('smtp', $connection);
        // Send the message
        $mail = $smtp->send($to, $headers, $body);
        // Check for errors
        if (PEAR::isError($mail)) {
            echo '! [email] ['.time().'] Failed sending mail to "'.$to.'"'.PHP_EOL;
            $result = false;
        } else {
            echo '  [email] ['.time().'] Mail sent to "'.$to.'"'.PHP_EOL;
            $result = true;
        }
        return $result;
    }
}

?>
Longlegged answered 22/3, 2013 at 23:23 Comment(0)
S
0

When troubleshooting email deliverability issues, I use Port 25's email check.

It will tell you what passes / does not pass and how SpamAssasin ranks your message.

The URL is: http://www.port25.com/support/authentication-center/email-verification/

To receive the results directly to any address, the address need to be added to the check-auth address. For example, to send the results to: [email protected] the sample message should be sent to [email protected].

Using this you can find out if your DKIM is working properly/validated, and what your SpamAssasin score is.

Seaquake answered 23/4, 2013 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.