Merge pull request #17583 from civicrm/5.27
[civicrm-core.git] / CRM / Mailing / BAO / Spool.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Mailing_BAO_Spool extends CRM_Mailing_DAO_Spool {
18
19 /**
fe482240 20 * Class constructor.
6a488035 21 */
00be9182 22 public function __construct() {
6a488035
TO
23 parent::__construct();
24 }
25
26 /**
27 * Store Mails into Spool table.
28 *
72b3a70c
CW
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.
90c8230e 35 * @param array $headers
72b3a70c 36 * The array of headers to send with the mail.
6a488035 37 *
90c8230e 38 * @param string $body
72b3a70c 39 * The full text of the message body, including any mime parts, etc.
6a488035 40 *
100fef9d 41 * @param int $job_id
77b97be7 42 *
72b3a70c
CW
43 * @return bool|CRM_Core_Error
44 * true if successful
6a488035 45 */
35f7561f 46 public function send($recipient, $headers, $body, $job_id = NULL) {
be2fb01f 47 $headerStr = [];
6a488035
TO
48 foreach ($headers as $name => $value) {
49 $headerStr[] = "$name: $value";
50 }
51 $headerStr = implode("\n", $headerStr);
52
481a74f4 53 if (is_null($job_id)) {
6a488035
TO
54 // This is not a bulk mailing. Create a dummy job for it.
55
56 $session = CRM_Core_Session::singleton();
be2fb01f 57 $params = [];
6a488035
TO
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'];
6a488035
TO
62 $params['is_completed'] = 1;
63 $params['is_archived'] = 1;
481a74f4 64 $params['body_html'] = htmlspecialchars($headerStr) . "\n\n" . $body;
6a488035
TO
65 $params['subject'] = $headers['Subject'];
66 $params['name'] = $headers['Subject'];
be2fb01f 67 $ids = [];
6a488035
TO
68 $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
69
481a74f4
TO
70 if (empty($mailing) || is_a($mailing, 'CRM_Core_Error')) {
71 return PEAR::raiseError('Unable to create spooled mailing.');
6a488035
TO
72 }
73
9da8dc8c 74 $job = new CRM_Mailing_BAO_MailingJob();
7e8c8317
SL
75 // if set to 1 it doesn't show in the UI
76 $job->is_test = 0;
6a488035
TO
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();
7e8c8317
SL
83 // need this for parent_id below
84 $job_id = $job->id;
6a488035 85
9da8dc8c 86 $job = new CRM_Mailing_BAO_MailingJob();
6a488035
TO
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();
7e8c8317
SL
96 // this is the one we want for the spool
97 $job_id = $job->id;
6a488035 98
481a74f4
TO
99 if (is_array($recipient)) {
100 $recipient = implode(';', $recipient);
6a488035
TO
101 }
102 }
103
104 $session = CRM_Core_Session::singleton();
105
be2fb01f 106 $params = [
6a488035
TO
107 'job_id' => $job_id,
108 'recipient_email' => $recipient,
109 'headers' => $headerStr,
110 'body' => $body,
111 'added_at' => date("YmdHis"),
112 'removed_at' => NULL,
be2fb01f 113 ];
6a488035
TO
114
115 $spoolMail = new CRM_Mailing_DAO_Spool();
116 $spoolMail->copyValues($params);
117 $spoolMail->save();
118
119 return TRUE;
120 }
96025800 121
6a488035 122}