Commit | Line | Data |
---|---|---|
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 | */ |
17 | class CRM_Mailing_BAO_Spool extends CRM_Mailing_DAO_Spool { | |
18 | ||
6a488035 TO |
19 | /** |
20 | * Store Mails into Spool table. | |
21 | * | |
72b3a70c CW |
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. | |
90c8230e | 28 | * @param array $headers |
72b3a70c | 29 | * The array of headers to send with the mail. |
6a488035 | 30 | * |
90c8230e | 31 | * @param string $body |
72b3a70c | 32 | * The full text of the message body, including any mime parts, etc. |
6a488035 | 33 | * |
100fef9d | 34 | * @param int $job_id |
77b97be7 | 35 | * |
72b3a70c CW |
36 | * @return bool|CRM_Core_Error |
37 | * true if successful | |
6a488035 | 38 | */ |
35f7561f | 39 | public function send($recipient, $headers, $body, $job_id = NULL) { |
be2fb01f | 40 | $headerStr = []; |
6a488035 TO |
41 | foreach ($headers as $name => $value) { |
42 | $headerStr[] = "$name: $value"; | |
43 | } | |
44 | $headerStr = implode("\n", $headerStr); | |
45 | ||
481a74f4 | 46 | if (is_null($job_id)) { |
6a488035 TO |
47 | // This is not a bulk mailing. Create a dummy job for it. |
48 | ||
49 | $session = CRM_Core_Session::singleton(); | |
be2fb01f | 50 | $params = []; |
6a488035 TO |
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']; | |
6a488035 TO |
55 | $params['is_completed'] = 1; |
56 | $params['is_archived'] = 1; | |
481a74f4 | 57 | $params['body_html'] = htmlspecialchars($headerStr) . "\n\n" . $body; |
6a488035 TO |
58 | $params['subject'] = $headers['Subject']; |
59 | $params['name'] = $headers['Subject']; | |
be2fb01f | 60 | $ids = []; |
6a488035 TO |
61 | $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids); |
62 | ||
481a74f4 TO |
63 | if (empty($mailing) || is_a($mailing, 'CRM_Core_Error')) { |
64 | return PEAR::raiseError('Unable to create spooled mailing.'); | |
6a488035 TO |
65 | } |
66 | ||
9da8dc8c | 67 | $job = new CRM_Mailing_BAO_MailingJob(); |
7e8c8317 SL |
68 | // if set to 1 it doesn't show in the UI |
69 | $job->is_test = 0; | |
6a488035 TO |
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(); | |
7e8c8317 SL |
76 | // need this for parent_id below |
77 | $job_id = $job->id; | |
6a488035 | 78 | |
9da8dc8c | 79 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
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(); | |
7e8c8317 SL |
89 | // this is the one we want for the spool |
90 | $job_id = $job->id; | |
6a488035 | 91 | |
481a74f4 TO |
92 | if (is_array($recipient)) { |
93 | $recipient = implode(';', $recipient); | |
6a488035 TO |
94 | } |
95 | } | |
96 | ||
97 | $session = CRM_Core_Session::singleton(); | |
98 | ||
be2fb01f | 99 | $params = [ |
6a488035 TO |
100 | 'job_id' => $job_id, |
101 | 'recipient_email' => $recipient, | |
102 | 'headers' => $headerStr, | |
103 | 'body' => $body, | |
104 | 'added_at' => date("YmdHis"), | |
105 | 'removed_at' => NULL, | |
be2fb01f | 106 | ]; |
6a488035 TO |
107 | |
108 | $spoolMail = new CRM_Mailing_DAO_Spool(); | |
109 | $spoolMail->copyValues($params); | |
110 | $spoolMail->save(); | |
111 | ||
112 | return TRUE; | |
113 | } | |
96025800 | 114 | |
6a488035 | 115 | } |