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