Setting up DomainKeys/DKIM in a PHP-based SMTP client [closed]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







23















It looks like there are some great libraries out there to do DomainKeys signing of emails on C#/.NET, but I'm having a really hard time finding the same kind of support for PHP. Maybe I'm not looking in the right place?



The only one I found is http://php-dkim.sourceforge.net/; it looks incredibly hacky and supports PHP4 only. Considering how popular PHP is, and how critical DomainKeys are for email classification as non-spam, I'd expect better tools; do you know of any? Any other tricks you'd recommend?



Extra info: I'm using an external SMTP provider because I need to send out thousands of emails per day.










share|improve this question















closed as off-topic by Cindy Meister, ayaio, Tunaki, Mogsdad, Wai Ha Lee Apr 12 '16 at 22:43


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Cindy Meister, ayaio, Tunaki, Mogsdad, Wai Ha Lee

If this question can be reworded to fit the rules in the help center, please edit the question.

















  • down vote Here is example functions how to send emails with dkim and php mail() function: github.com/breakermind/PHP-DKIM

    – user8660272
    Sep 26 '17 at 12:07


















23















It looks like there are some great libraries out there to do DomainKeys signing of emails on C#/.NET, but I'm having a really hard time finding the same kind of support for PHP. Maybe I'm not looking in the right place?



The only one I found is http://php-dkim.sourceforge.net/; it looks incredibly hacky and supports PHP4 only. Considering how popular PHP is, and how critical DomainKeys are for email classification as non-spam, I'd expect better tools; do you know of any? Any other tricks you'd recommend?



Extra info: I'm using an external SMTP provider because I need to send out thousands of emails per day.










share|improve this question















closed as off-topic by Cindy Meister, ayaio, Tunaki, Mogsdad, Wai Ha Lee Apr 12 '16 at 22:43


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Cindy Meister, ayaio, Tunaki, Mogsdad, Wai Ha Lee

If this question can be reworded to fit the rules in the help center, please edit the question.

















  • down vote Here is example functions how to send emails with dkim and php mail() function: github.com/breakermind/PHP-DKIM

    – user8660272
    Sep 26 '17 at 12:07














23












23








23


20






It looks like there are some great libraries out there to do DomainKeys signing of emails on C#/.NET, but I'm having a really hard time finding the same kind of support for PHP. Maybe I'm not looking in the right place?



The only one I found is http://php-dkim.sourceforge.net/; it looks incredibly hacky and supports PHP4 only. Considering how popular PHP is, and how critical DomainKeys are for email classification as non-spam, I'd expect better tools; do you know of any? Any other tricks you'd recommend?



Extra info: I'm using an external SMTP provider because I need to send out thousands of emails per day.










share|improve this question
















It looks like there are some great libraries out there to do DomainKeys signing of emails on C#/.NET, but I'm having a really hard time finding the same kind of support for PHP. Maybe I'm not looking in the right place?



The only one I found is http://php-dkim.sourceforge.net/; it looks incredibly hacky and supports PHP4 only. Considering how popular PHP is, and how critical DomainKeys are for email classification as non-spam, I'd expect better tools; do you know of any? Any other tricks you'd recommend?



Extra info: I'm using an external SMTP provider because I need to send out thousands of emails per day.







php smtp email-spam dkim domainkeys






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 28 '10 at 18:58









Alix Axel

109k72345461




109k72345461










asked May 9 '10 at 23:43









Alex WeinsteinAlex Weinstein

7,78883358




7,78883358




closed as off-topic by Cindy Meister, ayaio, Tunaki, Mogsdad, Wai Ha Lee Apr 12 '16 at 22:43


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Cindy Meister, ayaio, Tunaki, Mogsdad, Wai Ha Lee

If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by Cindy Meister, ayaio, Tunaki, Mogsdad, Wai Ha Lee Apr 12 '16 at 22:43


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Cindy Meister, ayaio, Tunaki, Mogsdad, Wai Ha Lee

If this question can be reworded to fit the rules in the help center, please edit the question.













  • down vote Here is example functions how to send emails with dkim and php mail() function: github.com/breakermind/PHP-DKIM

    – user8660272
    Sep 26 '17 at 12:07



















  • down vote Here is example functions how to send emails with dkim and php mail() function: github.com/breakermind/PHP-DKIM

    – user8660272
    Sep 26 '17 at 12:07

















down vote Here is example functions how to send emails with dkim and php mail() function: github.com/breakermind/PHP-DKIM

– user8660272
Sep 26 '17 at 12:07





down vote Here is example functions how to send emails with dkim and php mail() function: github.com/breakermind/PHP-DKIM

– user8660272
Sep 26 '17 at 12:07












4 Answers
4






active

oldest

votes


















21














I'd recommend DKIM support at the MTA level so all your server generated email for a given domain is signed by default. (unless you have a really good reason to not sign all server generated email for a domain).



The best starting point in my googling to get DKIM setup on LAMP with dkim-milter and sendmail (on CentOS 5.2 in my case) was Jeff Atwood's post about sending emails through code.



I would agree with him that the first 2 things you should address are reverse PTR record and DKIM signing.



Also very important:




  1. IP address of the box to send email not already being blacklisted.

  2. make sure postmaster@emailsendingdomain.com is a valid email box

  3. if your server generated email needs to appear to come from somewhere else (like a contact form needing to come from name/email provided in a form) follow these guidelines for email headers.


Here is the email ip address blacklist checker that I used.



Those 5 things will solve perhaps 95% of your email deliverability issues.



This Guide for Fedora/dkim-milter/postfix is also very good.



The PHP mail library I use for my app is PHPMailer 5.1 which has DKIM support (and is PHP 5 only), but after doing the research, I decided implementing at the sendmail level was a better solution. As you can see, even the author of PHPMailer 5.1 does not suggest DKIM at the PHP mail library level is the best solution http://dkim.worxware.com/.



Best of luck to you.






share|improve this answer


























  • Useful info, but not for me, sorry. I'm using an external SMTP email provider and have no control over their code. I have to use them as i'm sending thousands of emails. The rest of the suggestions do not directly apply to my question.

    – Alex Weinstein
    Jun 12 '10 at 7:04











  • @Alex: I was about to code my own DKIM implementation for PHP but I'm with jigglee: the MTA should handle it. Imagine that the MTA alters / reformats the email content, there is no way for PHP to know that à priori and that would make the private/public keys to fall apart, it would have the adverse effect.

    – Alix Axel
    Jul 28 '10 at 18:55











  • @AlexWeinstein if this solution is not for you, why is marked as the answer? I'm too working with a third-party email provider and this doesn't seems to be the right answer...

    – Alejandro García Iglesias
    Apr 12 '12 at 17:09



















14














This is one that has been on my radar for a while and could not find a definitive answer to the original question in this thread anywhere on the web. I have now been able to implement sending DKIM signed SMTP email with PHP/Pear. Below are the steps required.




  1. I found a modified version of the DKIM from http://www.ra726.net/blog/2010/07/20/sending-email-to-gmail-from-php-without-being-marked-as-spam/ (you can download it via http://www.ra726.net/php-dkim.zip). If you have already implemented DKIM and just need to make it work with SMP mail then all you need from this is the dkim.php file which, as the blog says, is slightly modified to handle headers passed as an array. In my code, I have named it dkimNEW.php.



  2. Ensure you include most headers so that the MTA does not modify the message after you have signed it. In my limited research, the most added headers are the Date and Message-ID headers, thus my header array looks like this: Note: I used this for sending an html email, change to suit! Also, add your domain as the last part of the Message-ID



    $headers = array(
    'Subject' => $subject,
    'From' => $from,
    'To' => $to,
    'MIME-Version' => '1.0',
    'Date' => date('r'),
    'Message-ID' => '<'.sha1(microtime(true)).'@yourdomain.com>',
    'Content-Type' => 'text/html',
    'Content-Transfer-Encoding' => 'quoted-printable',
    ); // end $headers



  3. You will then get to utilize the modified dkim.php mentioned above to sign your email AND add the signature to the headers array, aka



    require 'dkimNEW.php';
    $dkim = AddDKIM($headers, $subject, $body);
    $headers['DKIM-Signature'] = $dkim;



The rest of the code is the normal code to send email via SMTP with PHP/Pear. The full working code is:



<?php
require_once 'Mail.php';
require_once 'Mail/mime.php';
// set all of the parameters
$subject = 'Test of DKIM';
$from = 'My Name <myname@mydomain.com>';
$to = 'First Recipient <recipient1@domain.com>';
$pbody ='<html><head></head><body><h1>Done! DKIM test</h1>Result, next?</body></html>';
$text = strip_tags($pbody);

// create the headers
$headers = array(
'Subject' => $subject,
'From' => $from,
'To' => $to,
'MIME-Version' => '1.0',
'Date' => date('r'),
'Message-ID' => '<'.sha1(microtime(true)).'@mydomain.com>',
'Content-Type' => 'text/html',
'Content-Transfer-Encoding' => 'quoted-printable',
); // end $headers


// create the message
$mime = new Mail_mime("n");
$mime->setTXTBody($text);
$mime->setHTMLBody($pbody);

// always call these methods in this order
$body = $mime->get();
$headers = $mime->headers($headers);

require 'dkimNEW.php' ;
$dkim = AddDKIM($headers, $subject, $body);
$headers['DKIM-Signature'] = $dkim;

// create the smtp mail object
$smtp_params = array(
'host' => 'mail.mydomain.com',
'auth' => true,
'username' => 'myUserName',
'password' => 'myPassWord',
); // end $smtp_params
$smtp = Mail::factory('smtp', $smtp_params);

// send the message

$recipients = array('recipient1@domain.com', 'recipient2@domain.com');
$mail = $smtp->send($recipients, $headers, $body);

?>


PS. Just in case you did not notice, replace values with your own!



Therefore, all that is essentially needed to make DKIM to work with SMTP email (or indeed the PHP mail) is to ensure that you specify all the headers that are added to your email by your MTA, then sign the headers, subject and body of the message, and finally include that signed portion with your header.






share|improve this answer

































    11














    Have you try : phpMailDomainSigner It support DKIM-Signature and DomainKey-Signature in Object Oriented Style.



    Here some example:



    // Create mailDomainSigner Object
    include_once './lib/class.mailDomainSigner.php';

    $mds = &new mailDomainSigner($domain_priv,$domain_d,$domain_s);
    $new_data = $mds->sign(
    $mail_data,
    "Message-ID:Subject:From:Content-Type:MIME-Version:Content-Transfer-Encoding:Received:To:Date",
    true,true,false);





    share|improve this answer
























    • This will take you to the download for the latest version: code.google.com/a/apache-extras.org/p/phpmailer/downloads/list Also, the original project admin for PHPMailer created a a website for creating the DKIM files needed to sign emails with PHPMailer: dkim.worxware.com

      – Cameron
      Sep 19 '11 at 18:52





















    10














    A class solely for DKIM which is a spin-off from PHPMailer, but with improvements regarding the respect of the RFC and nice-and-clean code :



    https://sourceforge.net/projects/dkim-class-php/



    Example :



    include_once('dkim.class.php');
    $dkim = new DKIM();
    $dkim_header = $dkim -> get_DKIM_header($to, $subject, $message, $headers);
    mail($to, $subject, $message, $dkim_header.$headers);





    share|improve this answer
























    • The project moved to GitHub: github.com/louisameline/php-mail-signature

      – Gerald
      Mar 17 '17 at 1:08


















    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    21














    I'd recommend DKIM support at the MTA level so all your server generated email for a given domain is signed by default. (unless you have a really good reason to not sign all server generated email for a domain).



    The best starting point in my googling to get DKIM setup on LAMP with dkim-milter and sendmail (on CentOS 5.2 in my case) was Jeff Atwood's post about sending emails through code.



    I would agree with him that the first 2 things you should address are reverse PTR record and DKIM signing.



    Also very important:




    1. IP address of the box to send email not already being blacklisted.

    2. make sure postmaster@emailsendingdomain.com is a valid email box

    3. if your server generated email needs to appear to come from somewhere else (like a contact form needing to come from name/email provided in a form) follow these guidelines for email headers.


    Here is the email ip address blacklist checker that I used.



    Those 5 things will solve perhaps 95% of your email deliverability issues.



    This Guide for Fedora/dkim-milter/postfix is also very good.



    The PHP mail library I use for my app is PHPMailer 5.1 which has DKIM support (and is PHP 5 only), but after doing the research, I decided implementing at the sendmail level was a better solution. As you can see, even the author of PHPMailer 5.1 does not suggest DKIM at the PHP mail library level is the best solution http://dkim.worxware.com/.



    Best of luck to you.






    share|improve this answer


























    • Useful info, but not for me, sorry. I'm using an external SMTP email provider and have no control over their code. I have to use them as i'm sending thousands of emails. The rest of the suggestions do not directly apply to my question.

      – Alex Weinstein
      Jun 12 '10 at 7:04











    • @Alex: I was about to code my own DKIM implementation for PHP but I'm with jigglee: the MTA should handle it. Imagine that the MTA alters / reformats the email content, there is no way for PHP to know that à priori and that would make the private/public keys to fall apart, it would have the adverse effect.

      – Alix Axel
      Jul 28 '10 at 18:55











    • @AlexWeinstein if this solution is not for you, why is marked as the answer? I'm too working with a third-party email provider and this doesn't seems to be the right answer...

      – Alejandro García Iglesias
      Apr 12 '12 at 17:09
















    21














    I'd recommend DKIM support at the MTA level so all your server generated email for a given domain is signed by default. (unless you have a really good reason to not sign all server generated email for a domain).



    The best starting point in my googling to get DKIM setup on LAMP with dkim-milter and sendmail (on CentOS 5.2 in my case) was Jeff Atwood's post about sending emails through code.



    I would agree with him that the first 2 things you should address are reverse PTR record and DKIM signing.



    Also very important:




    1. IP address of the box to send email not already being blacklisted.

    2. make sure postmaster@emailsendingdomain.com is a valid email box

    3. if your server generated email needs to appear to come from somewhere else (like a contact form needing to come from name/email provided in a form) follow these guidelines for email headers.


    Here is the email ip address blacklist checker that I used.



    Those 5 things will solve perhaps 95% of your email deliverability issues.



    This Guide for Fedora/dkim-milter/postfix is also very good.



    The PHP mail library I use for my app is PHPMailer 5.1 which has DKIM support (and is PHP 5 only), but after doing the research, I decided implementing at the sendmail level was a better solution. As you can see, even the author of PHPMailer 5.1 does not suggest DKIM at the PHP mail library level is the best solution http://dkim.worxware.com/.



    Best of luck to you.






    share|improve this answer


























    • Useful info, but not for me, sorry. I'm using an external SMTP email provider and have no control over their code. I have to use them as i'm sending thousands of emails. The rest of the suggestions do not directly apply to my question.

      – Alex Weinstein
      Jun 12 '10 at 7:04











    • @Alex: I was about to code my own DKIM implementation for PHP but I'm with jigglee: the MTA should handle it. Imagine that the MTA alters / reformats the email content, there is no way for PHP to know that à priori and that would make the private/public keys to fall apart, it would have the adverse effect.

      – Alix Axel
      Jul 28 '10 at 18:55











    • @AlexWeinstein if this solution is not for you, why is marked as the answer? I'm too working with a third-party email provider and this doesn't seems to be the right answer...

      – Alejandro García Iglesias
      Apr 12 '12 at 17:09














    21












    21








    21







    I'd recommend DKIM support at the MTA level so all your server generated email for a given domain is signed by default. (unless you have a really good reason to not sign all server generated email for a domain).



    The best starting point in my googling to get DKIM setup on LAMP with dkim-milter and sendmail (on CentOS 5.2 in my case) was Jeff Atwood's post about sending emails through code.



    I would agree with him that the first 2 things you should address are reverse PTR record and DKIM signing.



    Also very important:




    1. IP address of the box to send email not already being blacklisted.

    2. make sure postmaster@emailsendingdomain.com is a valid email box

    3. if your server generated email needs to appear to come from somewhere else (like a contact form needing to come from name/email provided in a form) follow these guidelines for email headers.


    Here is the email ip address blacklist checker that I used.



    Those 5 things will solve perhaps 95% of your email deliverability issues.



    This Guide for Fedora/dkim-milter/postfix is also very good.



    The PHP mail library I use for my app is PHPMailer 5.1 which has DKIM support (and is PHP 5 only), but after doing the research, I decided implementing at the sendmail level was a better solution. As you can see, even the author of PHPMailer 5.1 does not suggest DKIM at the PHP mail library level is the best solution http://dkim.worxware.com/.



    Best of luck to you.






    share|improve this answer















    I'd recommend DKIM support at the MTA level so all your server generated email for a given domain is signed by default. (unless you have a really good reason to not sign all server generated email for a domain).



    The best starting point in my googling to get DKIM setup on LAMP with dkim-milter and sendmail (on CentOS 5.2 in my case) was Jeff Atwood's post about sending emails through code.



    I would agree with him that the first 2 things you should address are reverse PTR record and DKIM signing.



    Also very important:




    1. IP address of the box to send email not already being blacklisted.

    2. make sure postmaster@emailsendingdomain.com is a valid email box

    3. if your server generated email needs to appear to come from somewhere else (like a contact form needing to come from name/email provided in a form) follow these guidelines for email headers.


    Here is the email ip address blacklist checker that I used.



    Those 5 things will solve perhaps 95% of your email deliverability issues.



    This Guide for Fedora/dkim-milter/postfix is also very good.



    The PHP mail library I use for my app is PHPMailer 5.1 which has DKIM support (and is PHP 5 only), but after doing the research, I decided implementing at the sendmail level was a better solution. As you can see, even the author of PHPMailer 5.1 does not suggest DKIM at the PHP mail library level is the best solution http://dkim.worxware.com/.



    Best of luck to you.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 '10 at 2:31









    The Pixel Developer

    9,87183859




    9,87183859










    answered May 22 '10 at 2:32









    codercakecodercake

    93756




    93756













    • Useful info, but not for me, sorry. I'm using an external SMTP email provider and have no control over their code. I have to use them as i'm sending thousands of emails. The rest of the suggestions do not directly apply to my question.

      – Alex Weinstein
      Jun 12 '10 at 7:04











    • @Alex: I was about to code my own DKIM implementation for PHP but I'm with jigglee: the MTA should handle it. Imagine that the MTA alters / reformats the email content, there is no way for PHP to know that à priori and that would make the private/public keys to fall apart, it would have the adverse effect.

      – Alix Axel
      Jul 28 '10 at 18:55











    • @AlexWeinstein if this solution is not for you, why is marked as the answer? I'm too working with a third-party email provider and this doesn't seems to be the right answer...

      – Alejandro García Iglesias
      Apr 12 '12 at 17:09



















    • Useful info, but not for me, sorry. I'm using an external SMTP email provider and have no control over their code. I have to use them as i'm sending thousands of emails. The rest of the suggestions do not directly apply to my question.

      – Alex Weinstein
      Jun 12 '10 at 7:04











    • @Alex: I was about to code my own DKIM implementation for PHP but I'm with jigglee: the MTA should handle it. Imagine that the MTA alters / reformats the email content, there is no way for PHP to know that à priori and that would make the private/public keys to fall apart, it would have the adverse effect.

      – Alix Axel
      Jul 28 '10 at 18:55











    • @AlexWeinstein if this solution is not for you, why is marked as the answer? I'm too working with a third-party email provider and this doesn't seems to be the right answer...

      – Alejandro García Iglesias
      Apr 12 '12 at 17:09

















    Useful info, but not for me, sorry. I'm using an external SMTP email provider and have no control over their code. I have to use them as i'm sending thousands of emails. The rest of the suggestions do not directly apply to my question.

    – Alex Weinstein
    Jun 12 '10 at 7:04





    Useful info, but not for me, sorry. I'm using an external SMTP email provider and have no control over their code. I have to use them as i'm sending thousands of emails. The rest of the suggestions do not directly apply to my question.

    – Alex Weinstein
    Jun 12 '10 at 7:04













    @Alex: I was about to code my own DKIM implementation for PHP but I'm with jigglee: the MTA should handle it. Imagine that the MTA alters / reformats the email content, there is no way for PHP to know that à priori and that would make the private/public keys to fall apart, it would have the adverse effect.

    – Alix Axel
    Jul 28 '10 at 18:55





    @Alex: I was about to code my own DKIM implementation for PHP but I'm with jigglee: the MTA should handle it. Imagine that the MTA alters / reformats the email content, there is no way for PHP to know that à priori and that would make the private/public keys to fall apart, it would have the adverse effect.

    – Alix Axel
    Jul 28 '10 at 18:55













    @AlexWeinstein if this solution is not for you, why is marked as the answer? I'm too working with a third-party email provider and this doesn't seems to be the right answer...

    – Alejandro García Iglesias
    Apr 12 '12 at 17:09





    @AlexWeinstein if this solution is not for you, why is marked as the answer? I'm too working with a third-party email provider and this doesn't seems to be the right answer...

    – Alejandro García Iglesias
    Apr 12 '12 at 17:09













    14














    This is one that has been on my radar for a while and could not find a definitive answer to the original question in this thread anywhere on the web. I have now been able to implement sending DKIM signed SMTP email with PHP/Pear. Below are the steps required.




    1. I found a modified version of the DKIM from http://www.ra726.net/blog/2010/07/20/sending-email-to-gmail-from-php-without-being-marked-as-spam/ (you can download it via http://www.ra726.net/php-dkim.zip). If you have already implemented DKIM and just need to make it work with SMP mail then all you need from this is the dkim.php file which, as the blog says, is slightly modified to handle headers passed as an array. In my code, I have named it dkimNEW.php.



    2. Ensure you include most headers so that the MTA does not modify the message after you have signed it. In my limited research, the most added headers are the Date and Message-ID headers, thus my header array looks like this: Note: I used this for sending an html email, change to suit! Also, add your domain as the last part of the Message-ID



      $headers = array(
      'Subject' => $subject,
      'From' => $from,
      'To' => $to,
      'MIME-Version' => '1.0',
      'Date' => date('r'),
      'Message-ID' => '<'.sha1(microtime(true)).'@yourdomain.com>',
      'Content-Type' => 'text/html',
      'Content-Transfer-Encoding' => 'quoted-printable',
      ); // end $headers



    3. You will then get to utilize the modified dkim.php mentioned above to sign your email AND add the signature to the headers array, aka



      require 'dkimNEW.php';
      $dkim = AddDKIM($headers, $subject, $body);
      $headers['DKIM-Signature'] = $dkim;



    The rest of the code is the normal code to send email via SMTP with PHP/Pear. The full working code is:



    <?php
    require_once 'Mail.php';
    require_once 'Mail/mime.php';
    // set all of the parameters
    $subject = 'Test of DKIM';
    $from = 'My Name <myname@mydomain.com>';
    $to = 'First Recipient <recipient1@domain.com>';
    $pbody ='<html><head></head><body><h1>Done! DKIM test</h1>Result, next?</body></html>';
    $text = strip_tags($pbody);

    // create the headers
    $headers = array(
    'Subject' => $subject,
    'From' => $from,
    'To' => $to,
    'MIME-Version' => '1.0',
    'Date' => date('r'),
    'Message-ID' => '<'.sha1(microtime(true)).'@mydomain.com>',
    'Content-Type' => 'text/html',
    'Content-Transfer-Encoding' => 'quoted-printable',
    ); // end $headers


    // create the message
    $mime = new Mail_mime("n");
    $mime->setTXTBody($text);
    $mime->setHTMLBody($pbody);

    // always call these methods in this order
    $body = $mime->get();
    $headers = $mime->headers($headers);

    require 'dkimNEW.php' ;
    $dkim = AddDKIM($headers, $subject, $body);
    $headers['DKIM-Signature'] = $dkim;

    // create the smtp mail object
    $smtp_params = array(
    'host' => 'mail.mydomain.com',
    'auth' => true,
    'username' => 'myUserName',
    'password' => 'myPassWord',
    ); // end $smtp_params
    $smtp = Mail::factory('smtp', $smtp_params);

    // send the message

    $recipients = array('recipient1@domain.com', 'recipient2@domain.com');
    $mail = $smtp->send($recipients, $headers, $body);

    ?>


    PS. Just in case you did not notice, replace values with your own!



    Therefore, all that is essentially needed to make DKIM to work with SMTP email (or indeed the PHP mail) is to ensure that you specify all the headers that are added to your email by your MTA, then sign the headers, subject and body of the message, and finally include that signed portion with your header.






    share|improve this answer






























      14














      This is one that has been on my radar for a while and could not find a definitive answer to the original question in this thread anywhere on the web. I have now been able to implement sending DKIM signed SMTP email with PHP/Pear. Below are the steps required.




      1. I found a modified version of the DKIM from http://www.ra726.net/blog/2010/07/20/sending-email-to-gmail-from-php-without-being-marked-as-spam/ (you can download it via http://www.ra726.net/php-dkim.zip). If you have already implemented DKIM and just need to make it work with SMP mail then all you need from this is the dkim.php file which, as the blog says, is slightly modified to handle headers passed as an array. In my code, I have named it dkimNEW.php.



      2. Ensure you include most headers so that the MTA does not modify the message after you have signed it. In my limited research, the most added headers are the Date and Message-ID headers, thus my header array looks like this: Note: I used this for sending an html email, change to suit! Also, add your domain as the last part of the Message-ID



        $headers = array(
        'Subject' => $subject,
        'From' => $from,
        'To' => $to,
        'MIME-Version' => '1.0',
        'Date' => date('r'),
        'Message-ID' => '<'.sha1(microtime(true)).'@yourdomain.com>',
        'Content-Type' => 'text/html',
        'Content-Transfer-Encoding' => 'quoted-printable',
        ); // end $headers



      3. You will then get to utilize the modified dkim.php mentioned above to sign your email AND add the signature to the headers array, aka



        require 'dkimNEW.php';
        $dkim = AddDKIM($headers, $subject, $body);
        $headers['DKIM-Signature'] = $dkim;



      The rest of the code is the normal code to send email via SMTP with PHP/Pear. The full working code is:



      <?php
      require_once 'Mail.php';
      require_once 'Mail/mime.php';
      // set all of the parameters
      $subject = 'Test of DKIM';
      $from = 'My Name <myname@mydomain.com>';
      $to = 'First Recipient <recipient1@domain.com>';
      $pbody ='<html><head></head><body><h1>Done! DKIM test</h1>Result, next?</body></html>';
      $text = strip_tags($pbody);

      // create the headers
      $headers = array(
      'Subject' => $subject,
      'From' => $from,
      'To' => $to,
      'MIME-Version' => '1.0',
      'Date' => date('r'),
      'Message-ID' => '<'.sha1(microtime(true)).'@mydomain.com>',
      'Content-Type' => 'text/html',
      'Content-Transfer-Encoding' => 'quoted-printable',
      ); // end $headers


      // create the message
      $mime = new Mail_mime("n");
      $mime->setTXTBody($text);
      $mime->setHTMLBody($pbody);

      // always call these methods in this order
      $body = $mime->get();
      $headers = $mime->headers($headers);

      require 'dkimNEW.php' ;
      $dkim = AddDKIM($headers, $subject, $body);
      $headers['DKIM-Signature'] = $dkim;

      // create the smtp mail object
      $smtp_params = array(
      'host' => 'mail.mydomain.com',
      'auth' => true,
      'username' => 'myUserName',
      'password' => 'myPassWord',
      ); // end $smtp_params
      $smtp = Mail::factory('smtp', $smtp_params);

      // send the message

      $recipients = array('recipient1@domain.com', 'recipient2@domain.com');
      $mail = $smtp->send($recipients, $headers, $body);

      ?>


      PS. Just in case you did not notice, replace values with your own!



      Therefore, all that is essentially needed to make DKIM to work with SMTP email (or indeed the PHP mail) is to ensure that you specify all the headers that are added to your email by your MTA, then sign the headers, subject and body of the message, and finally include that signed portion with your header.






      share|improve this answer




























        14












        14








        14







        This is one that has been on my radar for a while and could not find a definitive answer to the original question in this thread anywhere on the web. I have now been able to implement sending DKIM signed SMTP email with PHP/Pear. Below are the steps required.




        1. I found a modified version of the DKIM from http://www.ra726.net/blog/2010/07/20/sending-email-to-gmail-from-php-without-being-marked-as-spam/ (you can download it via http://www.ra726.net/php-dkim.zip). If you have already implemented DKIM and just need to make it work with SMP mail then all you need from this is the dkim.php file which, as the blog says, is slightly modified to handle headers passed as an array. In my code, I have named it dkimNEW.php.



        2. Ensure you include most headers so that the MTA does not modify the message after you have signed it. In my limited research, the most added headers are the Date and Message-ID headers, thus my header array looks like this: Note: I used this for sending an html email, change to suit! Also, add your domain as the last part of the Message-ID



          $headers = array(
          'Subject' => $subject,
          'From' => $from,
          'To' => $to,
          'MIME-Version' => '1.0',
          'Date' => date('r'),
          'Message-ID' => '<'.sha1(microtime(true)).'@yourdomain.com>',
          'Content-Type' => 'text/html',
          'Content-Transfer-Encoding' => 'quoted-printable',
          ); // end $headers



        3. You will then get to utilize the modified dkim.php mentioned above to sign your email AND add the signature to the headers array, aka



          require 'dkimNEW.php';
          $dkim = AddDKIM($headers, $subject, $body);
          $headers['DKIM-Signature'] = $dkim;



        The rest of the code is the normal code to send email via SMTP with PHP/Pear. The full working code is:



        <?php
        require_once 'Mail.php';
        require_once 'Mail/mime.php';
        // set all of the parameters
        $subject = 'Test of DKIM';
        $from = 'My Name <myname@mydomain.com>';
        $to = 'First Recipient <recipient1@domain.com>';
        $pbody ='<html><head></head><body><h1>Done! DKIM test</h1>Result, next?</body></html>';
        $text = strip_tags($pbody);

        // create the headers
        $headers = array(
        'Subject' => $subject,
        'From' => $from,
        'To' => $to,
        'MIME-Version' => '1.0',
        'Date' => date('r'),
        'Message-ID' => '<'.sha1(microtime(true)).'@mydomain.com>',
        'Content-Type' => 'text/html',
        'Content-Transfer-Encoding' => 'quoted-printable',
        ); // end $headers


        // create the message
        $mime = new Mail_mime("n");
        $mime->setTXTBody($text);
        $mime->setHTMLBody($pbody);

        // always call these methods in this order
        $body = $mime->get();
        $headers = $mime->headers($headers);

        require 'dkimNEW.php' ;
        $dkim = AddDKIM($headers, $subject, $body);
        $headers['DKIM-Signature'] = $dkim;

        // create the smtp mail object
        $smtp_params = array(
        'host' => 'mail.mydomain.com',
        'auth' => true,
        'username' => 'myUserName',
        'password' => 'myPassWord',
        ); // end $smtp_params
        $smtp = Mail::factory('smtp', $smtp_params);

        // send the message

        $recipients = array('recipient1@domain.com', 'recipient2@domain.com');
        $mail = $smtp->send($recipients, $headers, $body);

        ?>


        PS. Just in case you did not notice, replace values with your own!



        Therefore, all that is essentially needed to make DKIM to work with SMTP email (or indeed the PHP mail) is to ensure that you specify all the headers that are added to your email by your MTA, then sign the headers, subject and body of the message, and finally include that signed portion with your header.






        share|improve this answer















        This is one that has been on my radar for a while and could not find a definitive answer to the original question in this thread anywhere on the web. I have now been able to implement sending DKIM signed SMTP email with PHP/Pear. Below are the steps required.




        1. I found a modified version of the DKIM from http://www.ra726.net/blog/2010/07/20/sending-email-to-gmail-from-php-without-being-marked-as-spam/ (you can download it via http://www.ra726.net/php-dkim.zip). If you have already implemented DKIM and just need to make it work with SMP mail then all you need from this is the dkim.php file which, as the blog says, is slightly modified to handle headers passed as an array. In my code, I have named it dkimNEW.php.



        2. Ensure you include most headers so that the MTA does not modify the message after you have signed it. In my limited research, the most added headers are the Date and Message-ID headers, thus my header array looks like this: Note: I used this for sending an html email, change to suit! Also, add your domain as the last part of the Message-ID



          $headers = array(
          'Subject' => $subject,
          'From' => $from,
          'To' => $to,
          'MIME-Version' => '1.0',
          'Date' => date('r'),
          'Message-ID' => '<'.sha1(microtime(true)).'@yourdomain.com>',
          'Content-Type' => 'text/html',
          'Content-Transfer-Encoding' => 'quoted-printable',
          ); // end $headers



        3. You will then get to utilize the modified dkim.php mentioned above to sign your email AND add the signature to the headers array, aka



          require 'dkimNEW.php';
          $dkim = AddDKIM($headers, $subject, $body);
          $headers['DKIM-Signature'] = $dkim;



        The rest of the code is the normal code to send email via SMTP with PHP/Pear. The full working code is:



        <?php
        require_once 'Mail.php';
        require_once 'Mail/mime.php';
        // set all of the parameters
        $subject = 'Test of DKIM';
        $from = 'My Name <myname@mydomain.com>';
        $to = 'First Recipient <recipient1@domain.com>';
        $pbody ='<html><head></head><body><h1>Done! DKIM test</h1>Result, next?</body></html>';
        $text = strip_tags($pbody);

        // create the headers
        $headers = array(
        'Subject' => $subject,
        'From' => $from,
        'To' => $to,
        'MIME-Version' => '1.0',
        'Date' => date('r'),
        'Message-ID' => '<'.sha1(microtime(true)).'@mydomain.com>',
        'Content-Type' => 'text/html',
        'Content-Transfer-Encoding' => 'quoted-printable',
        ); // end $headers


        // create the message
        $mime = new Mail_mime("n");
        $mime->setTXTBody($text);
        $mime->setHTMLBody($pbody);

        // always call these methods in this order
        $body = $mime->get();
        $headers = $mime->headers($headers);

        require 'dkimNEW.php' ;
        $dkim = AddDKIM($headers, $subject, $body);
        $headers['DKIM-Signature'] = $dkim;

        // create the smtp mail object
        $smtp_params = array(
        'host' => 'mail.mydomain.com',
        'auth' => true,
        'username' => 'myUserName',
        'password' => 'myPassWord',
        ); // end $smtp_params
        $smtp = Mail::factory('smtp', $smtp_params);

        // send the message

        $recipients = array('recipient1@domain.com', 'recipient2@domain.com');
        $mail = $smtp->send($recipients, $headers, $body);

        ?>


        PS. Just in case you did not notice, replace values with your own!



        Therefore, all that is essentially needed to make DKIM to work with SMTP email (or indeed the PHP mail) is to ensure that you specify all the headers that are added to your email by your MTA, then sign the headers, subject and body of the message, and finally include that signed portion with your header.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 23 '12 at 1:40

























        answered Oct 23 '12 at 0:45









        NepaluzNepaluz

        4511818




        4511818























            11














            Have you try : phpMailDomainSigner It support DKIM-Signature and DomainKey-Signature in Object Oriented Style.



            Here some example:



            // Create mailDomainSigner Object
            include_once './lib/class.mailDomainSigner.php';

            $mds = &new mailDomainSigner($domain_priv,$domain_d,$domain_s);
            $new_data = $mds->sign(
            $mail_data,
            "Message-ID:Subject:From:Content-Type:MIME-Version:Content-Transfer-Encoding:Received:To:Date",
            true,true,false);





            share|improve this answer
























            • This will take you to the download for the latest version: code.google.com/a/apache-extras.org/p/phpmailer/downloads/list Also, the original project admin for PHPMailer created a a website for creating the DKIM files needed to sign emails with PHPMailer: dkim.worxware.com

              – Cameron
              Sep 19 '11 at 18:52


















            11














            Have you try : phpMailDomainSigner It support DKIM-Signature and DomainKey-Signature in Object Oriented Style.



            Here some example:



            // Create mailDomainSigner Object
            include_once './lib/class.mailDomainSigner.php';

            $mds = &new mailDomainSigner($domain_priv,$domain_d,$domain_s);
            $new_data = $mds->sign(
            $mail_data,
            "Message-ID:Subject:From:Content-Type:MIME-Version:Content-Transfer-Encoding:Received:To:Date",
            true,true,false);





            share|improve this answer
























            • This will take you to the download for the latest version: code.google.com/a/apache-extras.org/p/phpmailer/downloads/list Also, the original project admin for PHPMailer created a a website for creating the DKIM files needed to sign emails with PHPMailer: dkim.worxware.com

              – Cameron
              Sep 19 '11 at 18:52
















            11












            11








            11







            Have you try : phpMailDomainSigner It support DKIM-Signature and DomainKey-Signature in Object Oriented Style.



            Here some example:



            // Create mailDomainSigner Object
            include_once './lib/class.mailDomainSigner.php';

            $mds = &new mailDomainSigner($domain_priv,$domain_d,$domain_s);
            $new_data = $mds->sign(
            $mail_data,
            "Message-ID:Subject:From:Content-Type:MIME-Version:Content-Transfer-Encoding:Received:To:Date",
            true,true,false);





            share|improve this answer













            Have you try : phpMailDomainSigner It support DKIM-Signature and DomainKey-Signature in Object Oriented Style.



            Here some example:



            // Create mailDomainSigner Object
            include_once './lib/class.mailDomainSigner.php';

            $mds = &new mailDomainSigner($domain_priv,$domain_d,$domain_s);
            $new_data = $mds->sign(
            $mail_data,
            "Message-ID:Subject:From:Content-Type:MIME-Version:Content-Transfer-Encoding:Received:To:Date",
            true,true,false);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 29 '11 at 7:06









            amarullzamarullz

            32644




            32644













            • This will take you to the download for the latest version: code.google.com/a/apache-extras.org/p/phpmailer/downloads/list Also, the original project admin for PHPMailer created a a website for creating the DKIM files needed to sign emails with PHPMailer: dkim.worxware.com

              – Cameron
              Sep 19 '11 at 18:52





















            • This will take you to the download for the latest version: code.google.com/a/apache-extras.org/p/phpmailer/downloads/list Also, the original project admin for PHPMailer created a a website for creating the DKIM files needed to sign emails with PHPMailer: dkim.worxware.com

              – Cameron
              Sep 19 '11 at 18:52



















            This will take you to the download for the latest version: code.google.com/a/apache-extras.org/p/phpmailer/downloads/list Also, the original project admin for PHPMailer created a a website for creating the DKIM files needed to sign emails with PHPMailer: dkim.worxware.com

            – Cameron
            Sep 19 '11 at 18:52







            This will take you to the download for the latest version: code.google.com/a/apache-extras.org/p/phpmailer/downloads/list Also, the original project admin for PHPMailer created a a website for creating the DKIM files needed to sign emails with PHPMailer: dkim.worxware.com

            – Cameron
            Sep 19 '11 at 18:52













            10














            A class solely for DKIM which is a spin-off from PHPMailer, but with improvements regarding the respect of the RFC and nice-and-clean code :



            https://sourceforge.net/projects/dkim-class-php/



            Example :



            include_once('dkim.class.php');
            $dkim = new DKIM();
            $dkim_header = $dkim -> get_DKIM_header($to, $subject, $message, $headers);
            mail($to, $subject, $message, $dkim_header.$headers);





            share|improve this answer
























            • The project moved to GitHub: github.com/louisameline/php-mail-signature

              – Gerald
              Mar 17 '17 at 1:08
















            10














            A class solely for DKIM which is a spin-off from PHPMailer, but with improvements regarding the respect of the RFC and nice-and-clean code :



            https://sourceforge.net/projects/dkim-class-php/



            Example :



            include_once('dkim.class.php');
            $dkim = new DKIM();
            $dkim_header = $dkim -> get_DKIM_header($to, $subject, $message, $headers);
            mail($to, $subject, $message, $dkim_header.$headers);





            share|improve this answer
























            • The project moved to GitHub: github.com/louisameline/php-mail-signature

              – Gerald
              Mar 17 '17 at 1:08














            10












            10








            10







            A class solely for DKIM which is a spin-off from PHPMailer, but with improvements regarding the respect of the RFC and nice-and-clean code :



            https://sourceforge.net/projects/dkim-class-php/



            Example :



            include_once('dkim.class.php');
            $dkim = new DKIM();
            $dkim_header = $dkim -> get_DKIM_header($to, $subject, $message, $headers);
            mail($to, $subject, $message, $dkim_header.$headers);





            share|improve this answer













            A class solely for DKIM which is a spin-off from PHPMailer, but with improvements regarding the respect of the RFC and nice-and-clean code :



            https://sourceforge.net/projects/dkim-class-php/



            Example :



            include_once('dkim.class.php');
            $dkim = new DKIM();
            $dkim_header = $dkim -> get_DKIM_header($to, $subject, $message, $headers);
            mail($to, $subject, $message, $dkim_header.$headers);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 29 '12 at 19:14









            Louis AmelineLouis Ameline

            1,6691517




            1,6691517













            • The project moved to GitHub: github.com/louisameline/php-mail-signature

              – Gerald
              Mar 17 '17 at 1:08



















            • The project moved to GitHub: github.com/louisameline/php-mail-signature

              – Gerald
              Mar 17 '17 at 1:08

















            The project moved to GitHub: github.com/louisameline/php-mail-signature

            – Gerald
            Mar 17 '17 at 1:08





            The project moved to GitHub: github.com/louisameline/php-mail-signature

            – Gerald
            Mar 17 '17 at 1:08



            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'