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