Я пытаюсь отправить электронное письмо с приложением PDF, созданного с помощью dompdf. Я создал функцию для отправки одного за другим, чтобы отправить их всех сразу. Проблема в том, что при функции их одновременной отправки вложения не имеют данных из базы данных.
Ниже приведен код моей функции:
function cust_finance_send_invoice_callback(&$context){
$batch_limit = 10;
global $language;
if (empty($context['sandbox'])) {
$query_rs = db_select("my_invoices", "i");
$query_rs->join("node", "n", "i.nid = n.nid");
$query_rs->join("field_email_invoice", "e", "n.nid = e.entity_id AND e.entity_type = 'node'");
$query_rs->fields("i", array("nid"));
$query_rs->condition("i.sent", 0, "=");
$query_rs->condition("e.field_email_invoice", "", "!=");
$query_rs->condition("e.bundle", "page", "=");
$total_results = $query_rs->countQuery()->execute()->fetchField();
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = $total_results;
}
if( !is_array($context["results"]) ) {
$context["results"] = array();
}
if ( $context['sandbox']['max'] > 0 ) {
$invoices_rs = db_select("finance_invoices", "i");
$invoices_rs->join("node", "n", "i.nid = n.nid");
$invoices_rs->join("field_email_invoice", "e", "n.nid = e.entity_id AND e.entity_type = 'node'");
$invoices_rs->fields("i", array("id", "nid"));
$invoices_rs->fields("n", array("title"));
$invoices_rs->fields("e", array("field_email_invoice"));
$invoices_rs->condition("i.sent", 0, "=");
$invoices_rs->condition("e.field_email_invoice", "", "!=");
$invoices_rs->condition("e.bundle", "prd", "=");
$invoices_rs->orderBy("i.id", "desc");
$invoices_rs->range(0, $batch_limit);
$invoices = $invoices_rs->execute()->fetchAll();
foreach( $invoices as $invoice ) {
$txn = db_transaction();
try {
$send_invoice_id = db_update("my_invoices")->condition("id", $invoice->id)->fields(array("sent" => 1))->execute();
$context['results'][] = $invoice->title;
$context['message'] = t("Sent invoices so far: @nr_invoices.", array("@nr_invoices" => count($context['results'])) );
$context['message'] .= "<br />" . t("Last sent invoice was to @someone", array("@someone" => $invoice->title) );
$params = array(
"subject" => variable_get("cust_mail_invoice_generated_sent_title", "My subject"),
"body" => variable_get("cust_mail_invoice_generated_sent_body", t("Body with the link: \n\n[invoice_link].\n\nThanks,\nTest Team")),
"to" => "my-email@hotmail.com"
);
$salt = "fsdfsdge4456546467"; //test salt
$hash = md5($invoice->nid . $invoice->id . $salt);
$invoice_link = " " . url("node/" . $invoice->nid . "/finance/pdf/" . $invoice->id . "/" . $hash, array("absolute" => true)) . " ";
$params["body"] = str_replace("[invoice_link]", $invoice_link, $params["body"]);
$prd = node_load($invoice->nid);
if ($dompdf = cust_finance_render_invoice_pdf($prd, $invoice)) {
$params['attachment'] = array('filename' => "invoice-$invoice->id.pdf", 'filecontent' => $dompdf->output(), 'filemime' => 'application/pdf');
}
drupal_mail("cust_function", "cust_function_invoice_generated_sent_mail", $params["to"], $language, $params);
} catch (Exception $e) {
$txn->rollback();
$context['message'] = t("Sent invoices so far: @nr_invoices.", array("@nr_invoices" => count($context['results'])) );
$context['message'] .= "<br />" . t("Last processed item: Failed to send invoice to @someone", array("@someone" => $invoice->title) );
}
$context['sandbox']['progress']++;
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
} else {
$context['finished'] = 1;
}
} else {
$context['finished'] = 1;
}
}