3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Mailing_MailStore
{
19 * flag to decide whether to print debug messages
22 public $_debug = FALSE;
25 * Return the proper mail store implementation, based on config settings.
28 * Name of the settings set from civimail_mail_settings to use (null for default).
31 * @return CRM_Mailing_MailStore
32 * mail store implementation for processing CiviMail-bound emails
34 public static function getStore($name = NULL) {
35 $dao = new CRM_Core_DAO_MailSettings();
36 $dao->domain_id
= CRM_Core_Config
::domainID();
37 $name ?
$dao->name
= $name : $dao->is_default
= 1;
38 if (!$dao->find(TRUE)) {
39 throw new Exception("Could not find entry named $name in civicrm_mail_settings");
42 $protocols = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_MailSettings', 'protocol', [], 'validate');
44 // Prepare normalized/hookable representation of the mail settings.
45 $mailSettings = $dao->toArray();
46 $mailSettings['protocol'] = $protocols[$mailSettings['protocol']] ??
NULL;
47 $protocolDefaults = self
::getProtocolDefaults($mailSettings['protocol']);
48 $mailSettings = array_merge($protocolDefaults, $mailSettings);
50 CRM_Utils_Hook
::alterMailStore($mailSettings);
52 if (!empty($mailSettings['factory'])) {
53 return call_user_func($mailSettings['factory'], $mailSettings);
56 throw new Exception("Unknown protocol {$mailSettings['protocol']}");
61 * @param string $protocol
62 * Ex: 'IMAP', 'Maildir'
64 * List of properties to merge into the $mailSettings.
65 * The most important property is 'factory' with signature:
67 * function($mailSettings): CRM_Mailing_MailStore
69 private static function getProtocolDefaults($protocol) {
74 'factory' => function($mailSettings) {
75 $useXOAuth2 = ($mailSettings['auth'] === 'XOAuth2');
76 return new CRM_Mailing_MailStore_Imap($mailSettings['server'], $mailSettings['username'], $mailSettings['password'], (bool) $mailSettings['is_ssl'], $mailSettings['source'], $useXOAuth2);
82 'factory' => function ($mailSettings) {
83 return new CRM_Mailing_MailStore_Pop3($mailSettings['server'], $mailSettings['username'], $mailSettings['password'], (bool) $mailSettings['is_ssl']);
89 'factory' => function ($mailSettings) {
90 return new CRM_Mailing_MailStore_Maildir($mailSettings['source']);
96 'factory' => function ($mailSettings) {
97 return new CRM_Mailing_MailStore_Localdir($mailSettings['source']);
101 // DO NOT USE the mbox transport for anything other than testing
102 // in particular, it does not clear the mbox afterwards
105 'factory' => function ($mailSettings) {
106 return new CRM_Mailing_MailStore_Mbox($mailSettings['source']);
116 * Return all emails in the mail store.
119 * array of ezcMail objects
121 public function allMails() {
122 return $this->fetchNext(0);
126 * Expunge the messages marked for deletion; stub function to be redefined by IMAP store.
128 public function expunge() {
132 * Return the next X messages from the mail store.
135 * Number of messages to fetch (0 to fetch all).
138 * array of ezcMail objects
140 public function fetchNext($count = 1) {
142 if (isset($this->_transport
->options
->uidReferencing
) and $this->_transport
->options
->uidReferencing
) {
143 $offset = $this->_transport
->listUniqueIdentifiers();
144 $offset = array_shift($offset);
147 $set = $this->_transport
->fetchFromOffset($offset, $count);
149 print "fetching $count messages\n";
152 catch (ezcMailOffsetOutOfRangeException
$e) {
154 print "got to the end of the mailbox\n";
159 $parser = new ezcMailParser();
160 //set property text attachment as file CRM-5408
161 $parser->options
->parseTextAttachmentsAsFiles
= TRUE;
163 foreach ($set->getMessageNumbers() as $nr) {
165 print "retrieving message $nr\n";
167 $single = $parser->parseMail($this->_transport
->fetchByMessageNr($nr));
168 $mails[$nr] = $single[0];
174 * Point to (and create if needed) a local Maildir for storing retrieved mail
176 * @param string $name
177 * Name of the Maildir.
181 * path to the Maildir's cur directory
183 public function maildir($name) {
184 $config = CRM_Core_Config
::singleton();
185 $dir = $config->customFileUploadDir
. DIRECTORY_SEPARATOR
. $name;
191 if (!file_exists($dir . DIRECTORY_SEPARATOR
. $sub)) {
193 print "creating $dir/$sub\n";
195 if (!mkdir($dir . DIRECTORY_SEPARATOR
. $sub, 0700, TRUE)) {
196 throw new Exception('Could not create ' . $dir . DIRECTORY_SEPARATOR
. $sub);
200 return $dir . DIRECTORY_SEPARATOR
. 'cur';