Attach PDF file to Drupal Commerce Order Notification 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(&amp;$message) {
  global $user;

  if (!empty($message['id']) &amp;&amp; ('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' =&gt; $pdf_coupon,
    'filemime' =&gt; $pdf_coupon-&gt;filemime,
    'filename' =&gt; $pdf_coupon-&gt;filename,
    'filepath' =&gt; 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-&gt;handler-&gt;execute('', 'F') 


to 

$display-&gt;handler-&gt;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' =&gt; $clean_email_nobase64,
    'filemime' =&gt; 'application/pdf',
    'filename' =&gt; "tester.pdf",
    'filepath' =&gt; 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-&gt;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-&gt;set_arguments(preg_split("/\r\n|\n|\r/", $arguments));

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

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