| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Mailing_BAO_Spool extends CRM_Mailing_DAO_Spool { |
| 36 | |
| 37 | /** |
| 38 | * Class constructor |
| 39 | */ |
| 40 | public function __construct() { |
| 41 | parent::__construct(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Store Mails into Spool table. |
| 46 | * |
| 47 | * @param string|array $recipient |
| 48 | * Either a comma-seperated list of recipients |
| 49 | * (RFC822 compliant), or an array of recipients, |
| 50 | * each RFC822 valid. This may contain recipients not |
| 51 | * specified in the headers, for Bcc:, resending |
| 52 | * messages, etc. |
| 53 | * @param array $headers |
| 54 | * The array of headers to send with the mail. |
| 55 | * |
| 56 | * @param string $body |
| 57 | * The full text of the message body, including any mime parts, etc. |
| 58 | * |
| 59 | * @param int $job_id |
| 60 | * |
| 61 | * @return bool|CRM_Core_Error |
| 62 | * true if successful |
| 63 | */ |
| 64 | public function send($recipient, $headers, $body, $job_id = NULL) { |
| 65 | $headerStr = array(); |
| 66 | foreach ($headers as $name => $value) { |
| 67 | $headerStr[] = "$name: $value"; |
| 68 | } |
| 69 | $headerStr = implode("\n", $headerStr); |
| 70 | |
| 71 | if (is_null($job_id)) { |
| 72 | // This is not a bulk mailing. Create a dummy job for it. |
| 73 | |
| 74 | $session = CRM_Core_Session::singleton(); |
| 75 | $params = array(); |
| 76 | $params['created_id'] = $session->get('userID'); |
| 77 | $params['created_date'] = date('YmdHis'); |
| 78 | $params['scheduled_id'] = $params['created_id']; |
| 79 | $params['scheduled_date'] = $params['created_date']; |
| 80 | $params['is_completed'] = 1; |
| 81 | $params['is_archived'] = 1; |
| 82 | $params['body_html'] = htmlspecialchars($headerStr) . "\n\n" . $body; |
| 83 | $params['subject'] = $headers['Subject']; |
| 84 | $params['name'] = $headers['Subject']; |
| 85 | $ids = array(); |
| 86 | $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids); |
| 87 | |
| 88 | if (empty($mailing) || is_a($mailing, 'CRM_Core_Error')) { |
| 89 | return PEAR::raiseError('Unable to create spooled mailing.'); |
| 90 | } |
| 91 | |
| 92 | $job = new CRM_Mailing_BAO_MailingJob(); |
| 93 | $job->is_test = 0; // if set to 1 it doesn't show in the UI |
| 94 | $job->status = 'Complete'; |
| 95 | $job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s')); |
| 96 | $job->start_date = $job->scheduled_date; |
| 97 | $job->end_date = $job->scheduled_date; |
| 98 | $job->mailing_id = $mailing->id; |
| 99 | $job->save(); |
| 100 | $job_id = $job->id; // need this for parent_id below |
| 101 | |
| 102 | $job = new CRM_Mailing_BAO_MailingJob(); |
| 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(); |
| 112 | $job_id = $job->id; // this is the one we want for the spool |
| 113 | |
| 114 | if (is_array($recipient)) { |
| 115 | $recipient = implode(';', $recipient); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | $session = CRM_Core_Session::singleton(); |
| 120 | |
| 121 | $params = array( |
| 122 | 'job_id' => $job_id, |
| 123 | 'recipient_email' => $recipient, |
| 124 | 'headers' => $headerStr, |
| 125 | 'body' => $body, |
| 126 | 'added_at' => date("YmdHis"), |
| 127 | 'removed_at' => NULL, |
| 128 | ); |
| 129 | |
| 130 | $spoolMail = new CRM_Mailing_DAO_Spool(); |
| 131 | $spoolMail->copyValues($params); |
| 132 | $spoolMail->save(); |
| 133 | |
| 134 | return TRUE; |
| 135 | } |
| 136 | } |