View source
<?php
function commerce_invoice_receipt_mail_form($form_state, $order) {
$form['order_id'] = array(
'#type' => 'hidden',
'#value' => $order['build_info']['args'][0]->order_id,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('Recipient e-mail address'),
'#default_value' => $order['build_info']['args'][0]->mail,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Email invoice'),
);
return $form;
}
function commerce_invoice_receipt_mail_form_validate($form, &$form_state) {
$recipient = check_plain($form_state['values']['email']);
if (empty($recipient) || !valid_email_address($recipient)) {
form_set_error('email', t('Invalid e-mail address.'));
}
}
function commerce_invoice_receipt_mail_form_submit($form, &$form_state) {
$order = commerce_order_load($form_state['values']['order_id']);
if ($order === FALSE) {
drupal_set_message(t('Order @order_id does not exist.', array(
'@order_id' => $form_state['values']['order_id'],
)));
drupal_goto('admin/commerce/orders');
}
$recipient = check_plain($form_state['values']['email']);
$params = array(
'order' => $order,
);
$from_email = variable_get('commerce_invoice_send_from', variable_get('site_mail'));
if ($copy_all = variable_get('commerce_invoice_copy_all_user', '')) {
$method = variable_get('commerce_invoice_copy_all_method', 'Bcc');
$params['headers'] = array(
$method => $copy_all,
);
}
$account = user_load($order->uid);
$language = user_preferred_language($account);
$sent = drupal_mail('commerce_invoice_receipt', 'invoice', $recipient, $language, $params, $from_email);
if (!$sent) {
drupal_set_message(t('Sending email failed. Please check the error log for more details.'), 'error');
}
else {
$message = t('Invoice emailed to @email.', array(
'@email' => $recipient,
));
drupal_set_message($message);
}
}
class InvoiceMailSystem implements MailSystemInterface {
public function format(array $message) {
$message['body'] = implode("\n\n", $message['body']);
return $message;
}
public function mail(array $message) {
$mimeheaders = array();
foreach ($message['headers'] as $name => $value) {
$mimeheaders[] = $name . ': ' . mime_header_encode($value);
}
$line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
return mail($message['to'], mime_header_encode($message['subject']), preg_replace('@\\r?\\n@', $line_endings, $message['body']), join("\n", $mimeheaders));
}
}