How do I attach a views_pdf file to the commerce email?

I have views_pdf, commerce_coupon.

As Sally I would like to buy a coupon and have a copy emailed to me.

I am stumped on how to get the pdf view into a File object.

function yhivecoupons_mail_alter(&$message) {
  global $user;

  if (!empty($message['id']) && ('message_notify_commerce_order_order_confirmation' ==  $message['id'])) {
$pdf_coupon = views_embed_view('commerce_gc_user_giftcards', $display_id = 'pdf_1');

  $my_attachment = array(
    'filecontent' => $pdf_coupon,
    'filemime' => $pdf_coupon->filemime,
    'filename' => $pdf_coupon->filename,
    'filepath' => NULL,
  $message['params']['attachments'][] = $my_attachment;

Problem #1. Getting a copy of the PDF.

The function views_pdf_rules_action_save() almost did it. I changed

$display->handler->execute('', 'F') 


$display->handler->execute('', 'E')

As TCPDF has an option to generate the PDF as an email format. I don't need to save the file with the resulting security and overwriting challenges.

Problem #2. Stripping the email and base64decode

The email format added the HTML already has the headers. I didn't dig enough to bypass the Drupal mail array processing. Instead the heading was stripped. You could use more of the provided mail headers.

$email_attachment = _yhivecoupons_views_pdf_rules_action_save('commerce_gc_user_giftcards:pdf_1', null, '');
$email_attachment_parts = explode("\n", $email_attachment);
$content_type = array_shift($email_attachment_parts);
$name = array_shift($email_attachment_parts);
$encoding = array_shift($email_attachment_parts);
$disposition = array_shift($email_attachment_parts);
$filename = array_shift($email_attachment_parts);

$clean_email_attachment = implode("\n", $email_attachment_parts);
$clean_email_nobase64 = base64_decode($clean_email_attachment);

      $my_attachment = array(
    'filecontent' => $clean_email_nobase64,
    'filemime' => 'application/pdf',
    'filename' => "tester.pdf",
    'filepath' => NULL,
  $message['params']['attachments'][] = $my_attachment;

Problem #3 Permissions

This process is still executed in a context where the user was not logged in. I am printing out a person's coupons so need their login details.

I don't think this is the best way to do this, as the process is not checking unique email addresses yet.

global $user;  
$user_obj = user_load_by_mail($message['to']);
$user = user_load($user_obj->uid);

Log out user at end.

Instead I am using commerce_immediate_login which does the same thing but in a rule using the ui generated by 'create login for anonymous users'. The order of the rules is specific.

This is my views PDF call function with the 1 character change from views_pdf_rules_action_save(). A tested way of getting PDF content from views_pdf.

function _yhivecoupons_views_pdf_rules_action_save($views_pdf, $arguments, $path) {
  $splits = explode(':', $views_pdf);
  $view_id = $splits[0];
  $display_id = $splits[1];

  if (!empty($view_id)) {
    $view = views_get_view($view_id);

    $view->set_arguments(preg_split("/\r\n|\n|\r/", $arguments));

    // Try to get pdf display
    if (!$view->set_display($display_id)) {
       // Try the display type
       if (!$view->set_display('pdf_1')) {
        // There is definitly no pdf display

    foreach ($view->display as $id => $display) {
      if ($display->display_plugin == 'pdf' && isset($display->handler)) {
        // arguments '' and I
        return $display->handler->execute('', 'E');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.