Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | class CRM_Mailing_BAO_Spool extends CRM_Mailing_DAO_Spool { | |
34 | ||
35 | /** | |
fe482240 | 36 | * Class constructor. |
6a488035 | 37 | */ |
00be9182 | 38 | public function __construct() { |
6a488035 TO |
39 | parent::__construct(); |
40 | } | |
41 | ||
42 | /** | |
43 | * Store Mails into Spool table. | |
44 | * | |
72b3a70c CW |
45 | * @param string|array $recipient |
46 | * Either a comma-seperated list of recipients | |
47 | * (RFC822 compliant), or an array of recipients, | |
48 | * each RFC822 valid. This may contain recipients not | |
49 | * specified in the headers, for Bcc:, resending | |
50 | * messages, etc. | |
90c8230e | 51 | * @param array $headers |
72b3a70c | 52 | * The array of headers to send with the mail. |
6a488035 | 53 | * |
90c8230e | 54 | * @param string $body |
72b3a70c | 55 | * The full text of the message body, including any mime parts, etc. |
6a488035 | 56 | * |
100fef9d | 57 | * @param int $job_id |
77b97be7 | 58 | * |
72b3a70c CW |
59 | * @return bool|CRM_Core_Error |
60 | * true if successful | |
6a488035 | 61 | */ |
35f7561f | 62 | public function send($recipient, $headers, $body, $job_id = NULL) { |
be2fb01f | 63 | $headerStr = []; |
6a488035 TO |
64 | foreach ($headers as $name => $value) { |
65 | $headerStr[] = "$name: $value"; | |
66 | } | |
67 | $headerStr = implode("\n", $headerStr); | |
68 | ||
481a74f4 | 69 | if (is_null($job_id)) { |
6a488035 TO |
70 | // This is not a bulk mailing. Create a dummy job for it. |
71 | ||
72 | $session = CRM_Core_Session::singleton(); | |
be2fb01f | 73 | $params = []; |
6a488035 TO |
74 | $params['created_id'] = $session->get('userID'); |
75 | $params['created_date'] = date('YmdHis'); | |
76 | $params['scheduled_id'] = $params['created_id']; | |
77 | $params['scheduled_date'] = $params['created_date']; | |
6a488035 TO |
78 | $params['is_completed'] = 1; |
79 | $params['is_archived'] = 1; | |
481a74f4 | 80 | $params['body_html'] = htmlspecialchars($headerStr) . "\n\n" . $body; |
6a488035 TO |
81 | $params['subject'] = $headers['Subject']; |
82 | $params['name'] = $headers['Subject']; | |
be2fb01f | 83 | $ids = []; |
6a488035 TO |
84 | $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids); |
85 | ||
481a74f4 TO |
86 | if (empty($mailing) || is_a($mailing, 'CRM_Core_Error')) { |
87 | return PEAR::raiseError('Unable to create spooled mailing.'); | |
6a488035 TO |
88 | } |
89 | ||
9da8dc8c | 90 | $job = new CRM_Mailing_BAO_MailingJob(); |
7e8c8317 SL |
91 | // if set to 1 it doesn't show in the UI |
92 | $job->is_test = 0; | |
6a488035 TO |
93 | $job->status = 'Complete'; |
94 | $job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s')); | |
95 | $job->start_date = $job->scheduled_date; | |
96 | $job->end_date = $job->scheduled_date; | |
97 | $job->mailing_id = $mailing->id; | |
98 | $job->save(); | |
7e8c8317 SL |
99 | // need this for parent_id below |
100 | $job_id = $job->id; | |
6a488035 | 101 | |
9da8dc8c | 102 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
103 | $job->is_test = 0; |
104 | $job->status = 'Complete'; | |
105 | $job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s')); | |
106 | $job->start_date = $job->scheduled_date; | |
107 | $job->end_date = $job->scheduled_date; | |
108 | $job->mailing_id = $mailing->id; | |
109 | $job->parent_id = $job_id; | |
110 | $job->job_type = 'child'; | |
111 | $job->save(); | |
7e8c8317 SL |
112 | // this is the one we want for the spool |
113 | $job_id = $job->id; | |
6a488035 | 114 | |
481a74f4 TO |
115 | if (is_array($recipient)) { |
116 | $recipient = implode(';', $recipient); | |
6a488035 TO |
117 | } |
118 | } | |
119 | ||
120 | $session = CRM_Core_Session::singleton(); | |
121 | ||
be2fb01f | 122 | $params = [ |
6a488035 TO |
123 | 'job_id' => $job_id, |
124 | 'recipient_email' => $recipient, | |
125 | 'headers' => $headerStr, | |
126 | 'body' => $body, | |
127 | 'added_at' => date("YmdHis"), | |
128 | 'removed_at' => NULL, | |
be2fb01f | 129 | ]; |
6a488035 TO |
130 | |
131 | $spoolMail = new CRM_Mailing_DAO_Spool(); | |
132 | $spoolMail->copyValues($params); | |
133 | $spoolMail->save(); | |
134 | ||
135 | return TRUE; | |
136 | } | |
96025800 | 137 | |
6a488035 | 138 | } |