In some installations of Storm, the function "Send PDF via e-mail" does not attach the PDF invoice to the email that is sent.

Sample email:

Return-path: <apache@org.net>
Envelope-to: postmaster@org.net
Delivery-date: Sat, 08 Aug 2009 21:11:45 -0400
Received: from mail by org.net with spam-scanned(Exim 4.69 #1 (chianna Linux 0.6))id 1MZwwr-0000bt-FQ; Sat, 08 Aug 2009 21:11:45 -0400
X-Spam-Checker-Version: SpamAssassin 3.2.1-gr1 (2007-05-02) on chianna
X-Spam-Level: 
X-Spam-Status: No, score=0.0 required=5.0 tests=UNPARSEABLE_RELAY
	autolearn=ham version=3.2.1-gr1
Received: from apache by org.net with local(Exim 4.69 #1 (chianna Linux 0.6))id 1MZwwr-0000bq-Ct; Sat, 08 Aug 2009 21:11:45 -0400
To: customer@business.com
Subject: Invoice from Lizard's Lounge
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=d3dee2b6be9273153307cdb44f6d561d
Content-Transfer-Encoding: 8Bit
X-Mailer: Drupal
Errors-To: info@org.net
From: admin<webmaster@org.net>
Message-Id: <E1MZwwr-0000bq-Ct@org.net>
Sender: apache <apache@org.net>
Date: Sat, 08 Aug 2009 21:11:45 -0400


--d3dee2b6be9273153307cdb44f6d561d
Content-Type: text/plain; charset=UTF-8; format=flowed;

Thank you!




--d3dee2b6be9273153307cdb44f6d561d--

Comments

Magnity’s picture

Just a few queries:
- From the problematic installation, does it work if you send to a different email address?
- Does it work if you send a different invoice?
- Are there any log entries, error messages, etc etc etc?

Magnity’s picture

Status: Active » Postponed (maintainer needs more info)

Changing status.

squ1rr3l’s picture

The email always goes out, and yes, I've tried several email addresses. Both the destination and the "send me a copy" emails are sent and received.

I have several invoices, and just tried an invoice with just 1 item, same result.

It appears to just silently fail - I can't find any indication in apache logs or anywhere else of an error.

Magnity’s picture

OK, a few more questions...
- Have you had this working on your installation before?
- Which other modules do you have enabled?
- Are you using the default drupal mail handlers, i.e. are you using SMTP module, or anything like that?
- If you use the demo site (which works for me), at the same email address - does that work?
- The headers above indicate a multipart message - of which the first is the plaintext covernote - is there anything in the second part?

Also - has anyone else had this problem?

squ1rr3l’s picture

Answers:

- Have you had this working on your installation before?
No, it's never worked, AFAICT

- Which other modules do you have enabled?
All of the Storm modules are enabled, although I don't use them all. Not sure if you're looking for other modules, there are not many extra ones installed, just CCK and VersionControl.

- Are you using the default drupal mail handlers, i.e. are you using SMTP module, or anything like that?
Yes, it's just basic Drupal 6.13 install. No mail modules at all. It's sitting on a Gentoo Linux box with Exim.

- If you use the demo site (which works for me), at the same email address - does that work?
There's a demo site? Okay - I've never used it before. Yes, it works fine, as expected. The only difference I can see looking at the email source is that the working one actually has the 2nd section with the uuencoded PDF.

- The headers above indicate a multipart message - of which the first is the plaintext covernote - is there anything in the second part?
No, it's exactly as shown above, ending after the first part. Note the part separator at the end has the "--" at the back, indicating there are no more parts. In the file from the demo site it looks like this:

Content-Type: multipart/mixed; boundary=d1552406b08294444faeb2cf96b00e94
Content-Transfer-Encoding: 8Bit
X-Mailer: Drupal
Errors-To: noreply@beta-web.co.uk
Sender: noreply@beta-web.co.uk
From: mrdemo1<hholt@lizardslounge.org>
X-Provags-ID: V01U2FsdGVkX18/zPyrc7oM02AuFT55Mpr89hP8MWjeN7SFgW2
 9Eq0JizTumeB0djdurClZi5XMfx40UVRVBBrMDI2BwXPvEjFS6
 ftDLFNLNbI=

--d1552406b08294444faeb2cf96b00e94
Content-Type: text/plain; charset=UTF-8; format=flowed;

Thank you!





--d1552406b08294444faeb2cf96b00e94
Content-Type:application/pdf; name='invoice_acme__14-2009.pdf'
Content-Disposition: attachment; filename='invoice_acme__14-2009.pdf'
Content-Transfer-Encoding: base64

JVBERi0xLjcKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL01lZGlhQm94IFsw
[SNIP]
NjEgMDAwMDAgbiAKdHJhaWxlcgo8PAovU2l6ZSAxMgovUm9vdCAxMSAwIFIKL0luZm8gMTAgMCBS
Cj4+CnN0YXJ0eHJlZgoyNzI2CiUlRU9GCg==
--d1552406b08294444faeb2cf96b00e94--

Also - has anyone else had this problem?
I'd like to know that too! This may be one of those "it's just me" issues, and it make little sense to hold things up if that's the case.

Where is the code that performs that function? I've dug around a bit, but I haven't really done any Drupal development (and only a little bit of PHP), so I don't really know where to look and couldn't find it.

... Squirrel.

Magnity’s picture

Lets wait to see if anyone else is having this issue too then - I can't see from your post any reason why it should go wrong in this way.

The code is mainly in storminvoice.admin.inc (from memory - hope that's correct).

Mark_Watson27’s picture

I've tested this on latest dev using Drupal 6.13 and get the same issue.

1) I've never used the feature before so cannot say if it's ever worked before
2) Administration Menu, Advanced Help, CCK, Date, Devel, Node Import, Popups API, Storm, Storm QT, Views
3) Default mail handlers (it doesn't seem to like SMTP Auth module)
4) Not tired demo site
5) Same as above no second part
6) Yes me :)

I'll have a play at some point and see if I can narrow it down.

Cheers
Mark

Mark_Watson27’s picture

Status: Postponed (maintainer needs more info) » Needs review

Okay, I think I see where this is stumbling up....

In storminvoice.admin.inc the function that is called when submitting the send is storminvoice_send_form_submit.

This function gets a variable for the PDF File Name $pdffile = variable_get('storminvoice_email_pdf_file_path', ''); and checks it before attaching the file if($pdffile){. However this never gets set until the function _storminvoice_report_file_pdf is called!

Quick work-around rem out the check and send two test PDFs the second should come through okay

  //if($pdffile){
    $params['body']  .= "--$trenner"."\n";
    $params['body']  .= "Content-Type:$pdffile_mime; name='$pdffile_name'\n";
    $params['body']  .= "Content-Disposition: attachment; filename='$pdffile_name'\n";
    $params['body']  .= "Content-Transfer-Encoding: base64\n\n";

    $node =  $form['#parameters'][3];
    $language = $form['#parameters'][4];
    $filedata = _storminvoice_report_file_pdf($node, $language);

    $params['body']  .= chunk_split(base64_encode($filedata));
  //}

After removing the check, the first time you send the PDF the variable will get set. The second time you send it it's correctly there to attach!

I haven't got time to write the patch at the mo and have no idea how this is working on a fresh system as I can't spot where those variables are being set without calling that function.

Thanks
Mark

homoludens’s picture

those variables are set in function _storminvoice_report_file_pdf at the end of storminvoice.admin.inc file.
i will, also, search for solution. i will, probable, need some time to catch up with other things, but this is high on mine todo list.

Magnity’s picture

Status: Needs review » Active

OK - it looks like I had the variables left in the system. On manually removing one of the variables, I get back to the situation that everyone else is having.

I wonder if this is due to the changes we made to the function prior to putting it in regarding not saving the invoice in the filesystem prior to emailing?

Mark_Watson27’s picture

@homoludens, yeah kinda figured that with my posting in #8 :)

Seriously though I don't think this will take much getting around, it's just wrong logic at the moment.

However it did point out to me that we're not checking the filename that's constructed. It should really be checked for invalid characters etc as per transliteration (http://drupal.org/project/transliteration)

@magnity: what were the changes, at first glance I'd say that we'd just need to check that we can PDF, but would need to take more time with it before confirming.

Cheers
Mark

Magnity’s picture

See #450360: Automatic E-Mail Invoicing comments #19 - #21.

Mark_Watson27’s picture

Changing the function to:

function storminvoice_send_form_submit($form, &$form_state) {
  $form_filled = $form_state['clicked_button']['#post'];

  $to = $form_filled['recipients'];
  $language = null;
  $from = $form_state['values']['yname'].'<'.$form_state['values']['yemail'].'>';

  $params = array();

  $params['subject'] = $form_filled['subject'];
  $params['bcc'] = $form_state['values']['bcc'];

  $trenner  = md5(uniqid(time()));
  variable_set('storminvoice_email_pdf_trenner', $trenner);

  $headers['Content-Type'] = "multipart/mixed; boundary=$trenner";
  $params['body']  = "\n--$trenner\n";
  $params['body'] .= "Content-Type: text/plain; charset=UTF-8; format=flowed;"."\n\n"; // sets the mime type
  $params['body'] .= $form_filled['body']."\n";
  $params['body'] .= "\n\n";
  $params['body'] .= "\n\n";

  $node =  $form['#parameters'][3];
  $language = $form['#parameters'][4];
  $filedata = _storminvoice_report_file_pdf($node, $language);

  $pdffile = variable_get('storminvoice_email_pdf_file_path', '');
  $pdffile_name = variable_get('storminvoice_email_pdf_file_name', '');
  $pdffile_mime = file_get_mimetype($pdffile);
  $params['body']  .= "--$trenner"."\n";
  $params['body']  .= "Content-Type:$pdffile_mime; name='$pdffile_name'\n";
  $params['body']  .= "Content-Disposition: attachment; filename='$pdffile_name'\n";
  $params['body']  .= "Content-Transfer-Encoding: base64\n\n";
  $params['body']  .= chunk_split(base64_encode($filedata));
  $params['body'] .= "--$trenner--";

  drupal_mail('storminvoice', 'invoice', $to, $language, $params, $from , True);

  drupal_set_message(t(variable_get('storminvoice_sent', 'You have sent invoice to !email.'),array('!email' => $to )));
}

Seems to have done it for me, i.e. getting rid of the if and moving the PDF generation to the top.

I'm going to work on abstracting the PDF transformation so we're not duplicating code.

Magnity’s picture

Status: Active » Needs review

I think @homoludens knows this bit of the code best - so i'd like to get a review from him. I'll be testing the code changes myself later on today or tomorrow.

Magnity’s picture

Status: Needs review » Fixed

Committed that fix.

A couple of issue sprouting from here though:
- #553556: Combine PDF invoice generation functions
- #553560: Tax fields blank in PDF invoice sent by email

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.