You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

identity_select.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Identity selection based on additional message headers.
  4. *
  5. * On reply to a message user identity selection is based on
  6. * content of standard headers i.e. From, To, Cc and Return-Path.
  7. * Here you can add header(s) set by your SMTP server (e.g.
  8. * Delivered-To, Envelope-To, X-Envelope-To, X-RCPT-TO) to make
  9. * identity selection more accurate.
  10. *
  11. * Enable the plugin in config.inc.php and add your desired headers:
  12. * $config['identity_select_headers'] = array('Delivered-To');
  13. *
  14. * Note: 'Received' header is also supported, but has bigger impact
  15. * on performance, as it's body is potentially much bigger
  16. * than other headers used by Roundcube
  17. *
  18. * @author Aleksander Machniak <alec@alec.pl>
  19. * @license GNU GPLv3+
  20. */
  21. class identity_select extends rcube_plugin
  22. {
  23. public $task = 'mail';
  24. function init()
  25. {
  26. $this->add_hook('identity_select', array($this, 'select'));
  27. $this->add_hook('storage_init', array($this, 'storage_init'));
  28. }
  29. /**
  30. * Adds additional headers to supported headers list
  31. */
  32. function storage_init($p)
  33. {
  34. $rcmail = rcmail::get_instance();
  35. if ($add_headers = (array)$rcmail->config->get('identity_select_headers', array())) {
  36. $p['fetch_headers'] = trim($p['fetch_headers'] . ' ' . strtoupper(join(' ', $add_headers)));
  37. }
  38. return $p;
  39. }
  40. /**
  41. * Identity selection
  42. */
  43. function select($p)
  44. {
  45. if ($p['selected'] !== null || !is_object($p['message']->headers)) {
  46. return $p;
  47. }
  48. $rcmail = rcmail::get_instance();
  49. foreach ((array)$rcmail->config->get('identity_select_headers', array()) as $header) {
  50. if ($emails = $this->get_email_from_header($p['message'], $header)) {
  51. foreach ($p['identities'] as $idx => $ident) {
  52. if (in_array($ident['email_ascii'], $emails)) {
  53. $p['selected'] = $idx;
  54. break 2;
  55. }
  56. }
  57. }
  58. }
  59. return $p;
  60. }
  61. /**
  62. * Extract email address from specified message header
  63. */
  64. protected function get_email_from_header($message, $header)
  65. {
  66. $value = $message->headers->get($header, false);
  67. if (strtolower($header) == 'received') {
  68. // find first email address in all Received headers
  69. $email = null;
  70. foreach ((array) $value as $entry) {
  71. if (preg_match('/[\s\t]+for[\s\t]+<([^>]+)>/', $entry, $matches)) {
  72. $email = $matches[1];
  73. break;
  74. }
  75. }
  76. $value = $email;
  77. }
  78. return (array) $value;
  79. }
  80. }