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