3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2020
33 class CRM_Mailing_MailStore
{
35 * flag to decide whether to print debug messages
38 public $_debug = FALSE;
41 * Return the proper mail store implementation, based on config settings.
44 * Name of the settings set from civimail_mail_settings to use (null for default).
48 * mail store implementation for processing CiviMail-bound emails
50 public static function getStore($name = NULL) {
51 $dao = new CRM_Core_DAO_MailSettings();
52 $dao->domain_id
= CRM_Core_Config
::domainID();
53 $name ?
$dao->name
= $name : $dao->is_default
= 1;
54 if (!$dao->find(TRUE)) {
55 throw new Exception("Could not find entry named $name in civicrm_mail_settings");
58 $protocols = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_MailSettings', 'protocol');
59 if (empty($protocols[$dao->protocol
])) {
60 throw new Exception("Empty mail protocol");
63 switch ($protocols[$dao->protocol
]) {
65 return new CRM_Mailing_MailStore_Imap($dao->server
, $dao->username
, $dao->password
, (bool) $dao->is_ssl
, $dao->source
);
68 return new CRM_Mailing_MailStore_Pop3($dao->server
, $dao->username
, $dao->password
, (bool) $dao->is_ssl
);
71 return new CRM_Mailing_MailStore_Maildir($dao->source
);
74 return new CRM_Mailing_MailStore_Localdir($dao->source
);
76 // DO NOT USE the mbox transport for anything other than testing
77 // in particular, it does not clear the mbox afterwards
80 return new CRM_Mailing_MailStore_Mbox($dao->source
);
83 throw new Exception("Unknown protocol {$dao->protocol}");
88 * Return all emails in the mail store.
91 * array of ezcMail objects
93 public function allMails() {
94 return $this->fetchNext(0);
98 * Expunge the messages marked for deletion; stub function to be redefined by IMAP store.
100 public function expunge() {
104 * Return the next X messages from the mail store.
107 * Number of messages to fetch (0 to fetch all).
110 * array of ezcMail objects
112 public function fetchNext($count = 1) {
114 if (isset($this->_transport
->options
->uidReferencing
) and $this->_transport
->options
->uidReferencing
) {
115 $offset = $this->_transport
->listUniqueIdentifiers();
116 $offset = array_shift($offset);
119 $set = $this->_transport
->fetchFromOffset($offset, $count);
121 print "fetching $count messages\n";
124 catch (ezcMailOffsetOutOfRangeException
$e) {
126 print "got to the end of the mailbox\n";
131 $parser = new ezcMailParser();
132 //set property text attachment as file CRM-5408
133 $parser->options
->parseTextAttachmentsAsFiles
= TRUE;
135 foreach ($set->getMessageNumbers() as $nr) {
137 print "retrieving message $nr\n";
139 $single = $parser->parseMail($this->_transport
->fetchByMessageNr($nr));
140 $mails[$nr] = $single[0];
146 * Point to (and create if needed) a local Maildir for storing retrieved mail
148 * @param string $name
149 * Name of the Maildir.
153 * path to the Maildir's cur directory
155 public function maildir($name) {
156 $config = CRM_Core_Config
::singleton();
157 $dir = $config->customFileUploadDir
. DIRECTORY_SEPARATOR
. $name;
163 if (!file_exists($dir . DIRECTORY_SEPARATOR
. $sub)) {
165 print "creating $dir/$sub\n";
167 if (!mkdir($dir . DIRECTORY_SEPARATOR
. $sub, 0700, TRUE)) {
168 throw new Exception('Could not create ' . $dir . DIRECTORY_SEPARATOR
. $sub);
172 return $dir . DIRECTORY_SEPARATOR
. 'cur';