commerce_invoice_mail.module in Commerce Invoice 7.2
The Commerce Invoice PDF module.
File
modules/mail/commerce_invoice_mail.moduleView source
<?php
/**
* @file
* The Commerce Invoice PDF module.
*/
use Drupal\commerce_invoice\Entity\Invoice;
function commerce_invoice_mail_send(Invoice $invoice, $to = '') {
$subject = variable_get('commerce_invoice_mail_subject', '');
$body = variable_get('commerce_invoice_mail_body', '');
$attachments = [];
if (variable_get('commerce_invoice_mail_attach_pdf_invoice', TRUE)) {
$attachments = commerce_invoice_mail_pdf_prepare($invoice);
}
// Get the invoice owner
// @todo: Do I want this empty object here?
$owner = new stdClass();
if (!empty($invoice->uid)) {
$owner = $invoice
->wrapper()->owner
->value();
$to = $owner->mail;
}
// @todo: Add "Order owner" as a fallback.
$owner_language = user_preferred_language($owner);
// Replace tokens
$subject = token_replace($subject, [
'user' => $owner,
'commerce_invoice' => $invoice,
]);
$body = token_replace($body, [
'user' => $owner,
'commerce_invoice' => $invoice,
]);
// Set mail values.
$params = [
'subject' => $subject,
'body' => $body,
'plain' => !empty(variable_get('commerce_invoice_mail_plaintext', FALSE)),
'attachments' => $attachments,
'headers' => [
'cc' => trim(variable_get('commerce_invoice_mail_cc', '')),
'bcc' => trim(variable_get('commerce_invoice_mail_bcc', '')),
],
'context' => [
'subject' => $subject,
'body' => $body,
'invoice' => $invoice,
],
];
// Remove the Bcc and Cc headers if they are empty, else this will crash phpmailer and maybe other modules.
if (empty($params['headers']['cc'])) {
unset($params['headers']['cc']);
}
if (empty($params['headers']['bcc'])) {
unset($params['headers']['bcc']);
}
$from = variable_get('commerce_invoice_mail_from', variable_get('site_mail', ini_get('sendmail_from')));
$mailkey = 'commerce_invoice_send_order_invoice';
$send = TRUE;
// Send mail.
$message = drupal_mail('commerce_invoice_mail', $mailkey, $to, $owner_language, $params, $from, $send);
if (!$message['result']) {
watchdog('commerce_invoice_mail', 'Error sending e-mail (from %from to %to).', array(
'%from' => $message['from'],
'%to' => $message['to'],
), WATCHDOG_ERROR);
drupal_set_message(t('Unable to send e-mail. Contact the site administrator if the problem persists.'));
return FALSE;
}
return TRUE;
}
/**
* Helper function to prepare the invoice mail.
*
* @param Invoice $invoice Invoice object.
* @return array Email attachments.
*/
function commerce_invoice_mail_pdf_prepare(Invoice $invoice) {
if (module_exists('commerce_invoice_pdf')) {
module_load_include('module', 'commerce_invoice_pdf');
}
else {
drupal_set_message(t('PDF email attachment could not be created, because commerce_invoice_pdf is disabled.'), 'error');
return [];
}
$file = commerce_invoice_pdf_create($invoice);
$file_content = file_get_contents(drupal_realpath($file->uri));
$attachments = [];
$attachments[] = array(
'filecontent' => $file_content,
// we use a dynamically created file.
'filename' => $file->filename,
// can use any alias name here
'name' => $file->filename,
// required for mandril and other libraries.
'filemime' => $file->filemime,
// mime file type
'type' => $file->filemime,
// required for mandril and other libraries
'list' => TRUE,
);
return $attachments;
}
/**
* Implements hook_mail().
*/
function commerce_invoice_mail_mail($key, &$message, $params) {
switch ($key) {
default:
if (isset($params['subject'])) {
$message['subject'] = $params['subject'];
}
if (isset($params['body'])) {
$message['body'][] = $params['body'];
}
if (isset($params['headers']) && is_array($params['headers'])) {
$message['headers'] = array_merge($message['headers'], $params['headers']);
}
if (isset($params['plain'])) {
$message['plain'] = $params['plain'];
}
if (isset($params['attachments'])) {
$message['attachments'][] = $params['attachments'];
}
break;
}
}
Functions
Name | Description |
---|---|
commerce_invoice_mail_mail | Implements hook_mail(). |
commerce_invoice_mail_pdf_prepare | Helper function to prepare the invoice mail. |
commerce_invoice_mail_send |