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).
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');
43 if (empty($protocols[$dao->protocol
])) {
44 throw new Exception("Empty mail protocol");
47 switch ($protocols[$dao->protocol
]) {
49 return new CRM_Mailing_MailStore_Imap($dao->server
, $dao->username
, $dao->password
, (bool) $dao->is_ssl
, $dao->source
);
52 return new CRM_Mailing_MailStore_Imap($dao->server
, $dao->username
, $dao->password
, (bool) $dao->is_ssl
, $dao->source
, TRUE);
55 return new CRM_Mailing_MailStore_Pop3($dao->server
, $dao->username
, $dao->password
, (bool) $dao->is_ssl
);
58 return new CRM_Mailing_MailStore_Maildir($dao->source
);
61 return new CRM_Mailing_MailStore_Localdir($dao->source
);
63 // DO NOT USE the mbox transport for anything other than testing
64 // in particular, it does not clear the mbox afterwards
67 return new CRM_Mailing_MailStore_Mbox($dao->source
);
70 throw new Exception("Unknown protocol {$dao->protocol}");
75 * Return all emails in the mail store.
78 * array of ezcMail objects
80 public function allMails() {
81 return $this->fetchNext(0);
85 * Expunge the messages marked for deletion; stub function to be redefined by IMAP store.
87 public function expunge() {
91 * Return the next X messages from the mail store.
94 * Number of messages to fetch (0 to fetch all).
97 * array of ezcMail objects
99 public function fetchNext($count = 1) {
101 if (isset($this->_transport
->options
->uidReferencing
) and $this->_transport
->options
->uidReferencing
) {
102 $offset = $this->_transport
->listUniqueIdentifiers();
103 $offset = array_shift($offset);
106 $set = $this->_transport
->fetchFromOffset($offset, $count);
108 print "fetching $count messages\n";
111 catch (ezcMailOffsetOutOfRangeException
$e) {
113 print "got to the end of the mailbox\n";
118 $parser = new ezcMailParser();
119 //set property text attachment as file CRM-5408
120 $parser->options
->parseTextAttachmentsAsFiles
= TRUE;
122 foreach ($set->getMessageNumbers() as $nr) {
124 print "retrieving message $nr\n";
126 $single = $parser->parseMail($this->_transport
->fetchByMessageNr($nr));
127 $mails[$nr] = $single[0];
133 * Point to (and create if needed) a local Maildir for storing retrieved mail
135 * @param string $name
136 * Name of the Maildir.
140 * path to the Maildir's cur directory
142 public function maildir($name) {
143 $config = CRM_Core_Config
::singleton();
144 $dir = $config->customFileUploadDir
. DIRECTORY_SEPARATOR
. $name;
150 if (!file_exists($dir . DIRECTORY_SEPARATOR
. $sub)) {
152 print "creating $dir/$sub\n";
154 if (!mkdir($dir . DIRECTORY_SEPARATOR
. $sub, 0700, TRUE)) {
155 throw new Exception('Could not create ' . $dir . DIRECTORY_SEPARATOR
. $sub);
159 return $dir . DIRECTORY_SEPARATOR
. 'cur';