Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-07-15-32-51
[civicrm-core.git] / CRM / Mailing / BAO / Spool.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35 class CRM_Mailing_BAO_Spool extends CRM_Mailing_DAO_Spool {
36
37 /**
38 * class constructor
39 */
40 function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Store Mails into Spool table.
46 *
47 * @param mixed $recipients Either a comma-seperated list of recipients
48 * (RFC822 compliant), or an array of recipients,
49 * each RFC822 valid. This may contain recipients not
50 * specified in the headers, for Bcc:, resending
51 * messages, etc.
52 *
53 * @param array $headers The string of headers to send with the mail.
54 *
55 * @param string $body The full text of the message body, including any
56 * Mime parts, etc.
57 *
58 * @return mixed Returns true on success, or a CRM_Eore_Error
59 * containing a descriptive error message on
60 * failure.
61 * @access public
62 */
63 function send($recipient, $headers, $body, $job_id = null) {
64 $headerStr = array();
65 foreach ($headers as $name => $value) {
66 $headerStr[] = "$name: $value";
67 }
68 $headerStr = implode("\n", $headerStr);
69
70 if ( is_null( $job_id ) ) {
71 // This is not a bulk mailing. Create a dummy job for it.
72
73 $session = CRM_Core_Session::singleton();
74 $params = array();
75 $params['created_id'] = $session->get('userID');
76 $params['created_date'] = date('YmdHis');
77 $params['scheduled_id'] = $params['created_id'];
78 $params['scheduled_date'] = $params['created_date'];
79 $params['is_completed'] = 1;
80 $params['is_archived'] = 1;
81 $params['body_html'] = htmlspecialchars( $headerStr ) . "\n\n" . $body;
82 $params['subject'] = $headers['Subject'];
83 $params['name'] = $headers['Subject'];
84 $ids = array();
85 $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
86
87 if ( empty( $mailing ) || is_a( $mailing, 'CRM_Core_Error' ) ) {
88 return PEAR::raiseError( 'Unable to create spooled mailing.' );
89 }
90
91 $job = new CRM_Mailing_BAO_MailingJob();
92 $job->is_test = 0; // if set to 1 it doesn't show in the UI
93 $job->status = 'Complete';
94 $job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s'));
95 $job->start_date = $job->scheduled_date;
96 $job->end_date = $job->scheduled_date;
97 $job->mailing_id = $mailing->id;
98 $job->save();
99 $job_id = $job->id; // need this for parent_id below
100
101 $job = new CRM_Mailing_BAO_MailingJob();
102 $job->is_test = 0;
103 $job->status = 'Complete';
104 $job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s'));
105 $job->start_date = $job->scheduled_date;
106 $job->end_date = $job->scheduled_date;
107 $job->mailing_id = $mailing->id;
108 $job->parent_id = $job_id;
109 $job->job_type = 'child';
110 $job->save();
111 $job_id = $job->id; // this is the one we want for the spool
112
113 if ( is_array( $recipient ) ) {
114 $recipient = implode( ';', $recipient );
115 }
116 }
117
118 $session = CRM_Core_Session::singleton();
119
120 $params = array(
121 'job_id' => $job_id,
122 'recipient_email' => $recipient,
123 'headers' => $headerStr,
124 'body' => $body,
125 'added_at' => date("YmdHis"),
126 'removed_at' => NULL,
127 );
128
129 $spoolMail = new CRM_Mailing_DAO_Spool();
130 $spoolMail->copyValues($params);
131 $spoolMail->save();
132
133 return TRUE;
134 }
135 }
136