Commit | Line | Data |
---|---|---|
dee6e582 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
10 | */ | |
11 | ||
12 | /** | |
13 | * An attachment to PEAR Mail which logs emails to files based on | |
14 | * the CIVICRM_MAIL_LOG configuration. | |
15 | * | |
16 | * (Produced by refactoring; specifically, extracting log-related functions | |
17 | * from CRM_Utils_Mail.) | |
18 | * | |
19 | * @package CRM | |
20 | * @copyright CiviCRM LLC https://civicrm.org/licensing | |
21 | */ | |
22 | class CRM_Utils_Mail_Logger { | |
23 | ||
24 | /** | |
25 | * @param CRM_Utils_Mail_FilteredPearMailer $mailer | |
26 | * @param mixed $recipients | |
27 | * @param array $headers | |
28 | * @param string $body | |
29 | * @return mixed | |
30 | * Normally returns null/void. But if the filter process is to be | |
31 | * short-circuited, then returns a concrete value. | |
32 | */ | |
33 | public static function filter($mailer, &$recipients, &$headers, &$body) { | |
34 | if (defined('CIVICRM_MAIL_LOG')) { | |
35 | static::log($recipients, $headers, $body); | |
36 | if (!defined('CIVICRM_MAIL_LOG_AND_SEND') && !defined('CIVICRM_MAIL_LOG_AND SEND')) { | |
37 | return TRUE; | |
38 | } | |
39 | } | |
40 | } | |
41 | ||
42 | /** | |
43 | * @param $to | |
44 | * @param $headers | |
45 | * @param $message | |
46 | */ | |
47 | public static function log(&$to, &$headers, &$message) { | |
48 | if (is_array($to)) { | |
49 | $toString = implode(', ', $to); | |
50 | $fileName = $to[0]; | |
51 | } | |
52 | else { | |
53 | $toString = $fileName = $to; | |
54 | } | |
55 | $content = "To: " . $toString . "\n"; | |
56 | foreach ($headers as $key => $val) { | |
57 | $content .= "$key: $val\n"; | |
58 | } | |
59 | $content .= "\n" . $message . "\n"; | |
60 | ||
61 | if (is_numeric(CIVICRM_MAIL_LOG)) { | |
62 | $config = CRM_Core_Config::singleton(); | |
63 | // create the directory if not there | |
64 | $dirName = $config->configAndLogDir . 'mail' . DIRECTORY_SEPARATOR; | |
65 | CRM_Utils_File::createDir($dirName); | |
66 | $fileName = md5(uniqid(CRM_Utils_String::munge($fileName))) . '.txt'; | |
67 | file_put_contents($dirName . $fileName, | |
68 | $content | |
69 | ); | |
70 | } | |
71 | else { | |
72 | file_put_contents(CIVICRM_MAIL_LOG, $content, FILE_APPEND); | |
73 | } | |
74 | } | |
75 | ||
76 | } |