3

I am trying to add a DKIM for the emails which are sent from my server. I added the public key on the DNS records (as a TXT records) of my domain, but I cannot find where to add the private key. It seems it should go into the email headers. This is how the code looks like:

    $_headers = array(
        'From' => $config['smtp_from'],
        'To' => $to,
        'Subject' => $subject
    );

    if ($html) {
        $_headers['MIME-Version'] = '1.0';
        $_headers['Content-type'] = 'text/html; charset=UTF-8';
    }

    if ($headers) {
        foreach ($headers as $key => $val) {
            $_headers[$key] = $val;
        }
    }

    $smtp = Mail::factory('smtp', array(
        'host' => $config['smtp_host'],
        'auth' => false,
        'username' => $config['smtp_user'],
        'password' => $config['smtp_pass']
    ));

    $mail = $smtp->send($to, $_headers, $message);

    if (PEAR::isError($mail)) {
        return false;
    } else {
        log_add('', 'To: ' . $to . '<br />Subject: ' . $subject, 'email');
        return true;
     }

Can someone please advise where the private key should be added? The problem is that the emails sent to Gmail recipients are going to Spam, so I understand that a DKIM should fix this.

I am using PEAR email to send emails from the server.

decebal
  • 1,151
  • 3
  • 18
  • 38
  • Please look at this link : https://stackoverflow.com/questions/2799611/setting-up-domainkeys-dkim-in-a-php-based-smtp-client – devseo Sep 13 '17 at 07:39
  • Thanks @Melvita ! I managed to create private and public keys, but I had a problem so I will mention the workaround here, in case someone else will have the same problem: In dkim-test.php I replaced the content for the $headers variable with $headers = Array( "From" => "Fresh DKIM Manager <$sender>\r\n", "To" => "$to\r\n", "Reply-To" => "$sender\r\n", "Content-Type" => "text/html\r\n", "MIME-Version" => "1.0" ); because I got an error from a foreach loop which didn't receive an array as argument. – decebal Sep 13 '17 at 10:54
  • try to add : require 'dkim.php'; $dkim = AddDKIM($headers, $subject, $message); $headers['DKIM-Signature'] = $dkim; //link to lib http://www.ra726.net/php-dkim.zip //source : http://www.ra726.net/2010/07/20/sending-email-to-gmail-from-php-without-being-marked-as-spam/ //hope it helps, sorry i have no time to test it right now – devseo Sep 13 '17 at 11:58
  • https://github.com/breakermind/PHP-DKIM –  Sep 26 '17 at 12:10
  • @Melvita - Thanks for your suggestions. I configurated everything like in the post you mentioned, but when sending the email from the contact form, I get this error: `Cannot sign`. I see this is related to `openssl_sign($s, $signature, $open_SSL_priv)`. Can you please advise what may be wrong? Thanks! – decebal Oct 10 '17 at 09:33
  • I found a solution here: https://stackoverflow.com/questions/8672177/cannot-sign-anything-with-php-openssl-sign/24784708#24784708 I moved the inclusion of the DKIM config file from inside, to outside the mail function. Now the error disappeared, but the emails are still being sent to Spam on Gmail (which is my main problem). Anyway, now I know the DKIM is set properly :-) – decebal Oct 10 '17 at 09:45

0 Answers0