3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Mailing_MailStore
{
36 // flag to decide whether to print debug messages
40 * Return the proper mail store implementation, based on config settings
42 * @param string $name name of the settings set from civimail_mail_settings to use (null for default)
45 * @return object mail store implementation for processing CiviMail-bound emails
47 function getStore($name = NULL) {
48 $dao = new CRM_Core_DAO_MailSettings
;
49 $dao->domain_id
= CRM_Core_Config
::domainID();
50 $name ?
$dao->name
= $name : $dao->is_default
= 1;
51 if (!$dao->find(TRUE)) {
52 throw new Exception("Could not find entry named $name in civicrm_mail_settings");
55 $protocols = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_MailSettings', 'protocol');
57 switch ($protocols[$dao->protocol
]) {
59 return new CRM_Mailing_MailStore_Imap($dao->server
, $dao->username
, $dao->password
, (bool) $dao->is_ssl
, $dao->source
);
62 return new CRM_Mailing_MailStore_Pop3($dao->server
, $dao->username
, $dao->password
, (bool) $dao->is_ssl
);
65 return new CRM_Mailing_MailStore_Maildir($dao->source
);
68 return new CRM_Mailing_MailStore_Localdir($dao->source
);
70 // DO NOT USE the mbox transport for anything other than testing
71 // in particular, it does not clear the mbox afterwards
74 return new CRM_Mailing_MailStore_Mbox($dao->source
);
77 throw new Exception("Unknown protocol {$dao->protocol}");
82 * Return all emails in the mail store
84 * @return array array of ezcMail objects
87 return $this->fetchNext(0);
91 * Expunge the messages marked for deletion; stub function to be redefined by IMAP store
96 * Return the next X messages from the mail store
98 * @param int $count number of messages to fetch (0 to fetch all)
100 * @return array array of ezcMail objects
102 function fetchNext($count = 1) {
103 if (isset($this->_transport
->options
->uidReferencing
) and $this->_transport
->options
->uidReferencing
) {
104 $offset = array_shift($this->_transport
->listUniqueIdentifiers());
110 $set = $this->_transport
->fetchFromOffset($offset, $count);
112 print "fetching $count messages\n";
115 catch(ezcMailOffsetOutOfRangeException
$e) {
117 print "got to the end of the mailbox\n";
122 $parser = new ezcMailParser
;
123 //set property text attachment as file CRM-5408
124 $parser->options
->parseTextAttachmentsAsFiles
= TRUE;
126 foreach ($set->getMessageNumbers() as $nr) {
128 print "retrieving message $nr\n";
130 $single = $parser->parseMail($this->_transport
->fetchByMessageNr($nr));
131 $mails[$nr] = $single[0];
137 * Point to (and create if needed) a local Maildir for storing retrieved mail
139 * @param string $name name of the Maildir
142 * @return string path to the Maildir's cur directory
144 function maildir($name) {
145 $config = CRM_Core_Config
::singleton();
146 $dir = $config->customFileUploadDir
. DIRECTORY_SEPARATOR
. $name;
148 'cur', 'new', 'tmp') as $sub) {
149 if (!file_exists($dir . DIRECTORY_SEPARATOR
. $sub)) {
151 print "creating $dir/$sub\n";
153 if (!mkdir($dir . DIRECTORY_SEPARATOR
. $sub, 0700, TRUE)) {
154 throw new Exception('Could not create ' . $dir . DIRECTORY_SEPARATOR
. $sub);
158 return $dir . DIRECTORY_SEPARATOR
. 'cur';