Merge pull request #22538 from masetto/pdfletter
[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 $ids = [];
61 $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
62
63 if (empty($mailing) || is_a($mailing, 'CRM_Core_Error')) {
64 return PEAR::raiseError('Unable to create spooled mailing.');
65 }
66
67 $job = new CRM_Mailing_BAO_MailingJob();
68 // if set to 1 it doesn't show in the UI
69 $job->is_test = 0;
70 $job->status = 'Complete';
71 $job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s'));
72 $job->start_date = $job->scheduled_date;
73 $job->end_date = $job->scheduled_date;
74 $job->mailing_id = $mailing->id;
75 $job->save();
76 // need this for parent_id below
77 $job_id = $job->id;
78
79 $job = new CRM_Mailing_BAO_MailingJob();
80 $job->is_test = 0;
81 $job->status = 'Complete';
82 $job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s'));
83 $job->start_date = $job->scheduled_date;
84 $job->end_date = $job->scheduled_date;
85 $job->mailing_id = $mailing->id;
86 $job->parent_id = $job_id;
87 $job->job_type = 'child';
88 $job->save();
89 // this is the one we want for the spool
90 $job_id = $job->id;
91
92 if (is_array($recipient)) {
93 $recipient = implode(';', $recipient);
94 }
95 }
96
97 $session = CRM_Core_Session::singleton();
98
99 $params = [
100 'job_id' => $job_id,
101 'recipient_email' => $recipient,
102 'headers' => $headerStr,
103 'body' => $body,
104 'added_at' => date("YmdHis"),
105 'removed_at' => NULL,
106 ];
107
108 $spoolMail = new CRM_Mailing_DAO_Spool();
109 $spoolMail->copyValues($params);
110 $spoolMail->save();
111
112 return TRUE;
113 }
114
115 }