• Resolved petebm

    (@petebm)


    Hi
    I’m aware of possible problems with email attachments caused by using Sendcloud (https://www.ads-software.com/support/topic/wrong-invoice-attached-to-mails/), but I’d like to ask if you’re aware of any possible link between the issue and WP Mail SMTP. Last week we had one isolated incidence (first in 2 years) of a customer getting the attachment which had already been correctly sent to the previous one, and all I can think is some conflict WP Mail SMTP. tbh, I can’t remember why that plugin was installed; I’ve now taken it off (its settings were defaulting to PHP mailer anyway) and everything still works fine.

    I’m assuming/hoping that this was a one-off, never to be repeated, but thought I’d ask if you were aeware of any issues.

    All the best
    Pete

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Are your filenames unique, i.e. including the order or invoice number? If you have overriden the default filename to something generic like invoice.php and you are using a plugin that sends email asynchronously, the files can get mixed up. We therefor strongly recommend using either the default filenames (which include that unique identifier) or adding your own unique identifier (like customer name etc.).

    Thread Starter petebm

    (@petebm)

    Thanks very much for your reply. Yes, I’m afraid our filenames are all the same; I removed the order number from them as it was causing confusion for our customers, who are given a completely different invoice number by our stock control / accounting system; the document they get at this point is purely an order acknowldgement (https://www.ads-software.com/support/topic/change-name-of-document/).

    Presumably the safest course of action (even if I’m not using a mail plugin any longer and this has only happened once in 2 years) is to follow your suggestion and add the customer name, which I can do by editing the snippet I’m using currently in some way (apologies for not knowing how to get it to come up in a nice box):

    add_filter( ‘wpo_wcpdf_filename’, ‘wpo_wcpdf_custom_filename’, 10, 4 );
    function wpo_wcpdf_custom_filename( $filename, $template_type, $order_ids, $context ) {
    $count = count($order_ids);

    switch ($template_type) {
    case ‘invoice’:
    $name = _n( ‘invoice’, ‘invoices’, $count, ‘woocommerce-pdf-invoices-packing-slips’ );
    break;
    case ‘packing-slip’:
    $name = _n( ‘packing-slip’, ‘packing-slips’, $count, ‘woocommerce-pdf-invoices-packing-slips’ );
    break;
    case ‘proforma’:
    $name = _n( ‘proforma-invoice’, ‘proforma-invoices’, $count, ‘wpo_wcpdf_pro’ );
    break;
    case ‘credit-note’:
    $name = _n( ‘credit-note’, ‘credit-notes’, $count, ‘wpo_wcpdf_pro’ );
    break;
    default:
    $name = $template_type;
    break;
    /* }

    if ( $count == 1 ) {
    $document = wcpdf_get_document( $template_type, $order_ids );
    if ( $number = $document->get_number() ) {
    $suffix = $number;
    } elseif (isset($document->order) && method_exists($document->order, ‘get_order_number’)) {
    $number = $document->order->get_order_number();
    } else {
    $number = $order_ids[0];
    }
    $suffix = $number;
    } else {
    */ $suffix = date(‘Y-m-d’); // 2020-11-11
    }

    $filename = $name . ‘-‘ . $suffix . ‘.pdf’;

    return $filename;
    }

    The customer name for us is user_login, which you’ve already explained how to get on invoices (https://www.ads-software.com/support/topic/showing-user_id-on-invoice-packing-list/).

    All the best
    Pete

    Plugin Contributor Ewout

    (@pomegranate)

    Yes, you can indeed include something like this:

    
    if ( $count == 1 ) {
    	$order = wc_get_order($order_ids[0]);
    	if ( $user = $order->get_user() ) {
    		$name = $user->user_login;
    	} else {
    		$name = $order->get_formatted_billing_full_name();
    	}
    	$suffix = "{$suffix}-{$name}";
    }
    

    just above

    
    $filename = $name . '-' . $suffix . '.pdf';
    

    in your code.

    Posting code here on the forums can be done by using backticks on the lines before and after your code like so:

    (you can also use the code button for this)

    Thread Starter petebm

    (@petebm)

    Thank you as always. I did wonder if the code button did that, but not being able to preview the post before submitting, I didn’t want to risk it!

    I added the code you kindly suggested, and it worked in that the filename then had the user_login appended . . . but the filename now consisted of user_loginuser_login – ie the name of the attachment (which was ‘Order acknowledgement’, translated from ‘Invoice’) has been swapped for user_login.

    So, in a spirit of adventure, I’ve now taken the liberty of editing your code at various points:

    
    add_filter( 'wpo_wcpdf_filename', 'wpo_wcpdf_custom_filename', 10, 4 );
    function wpo_wcpdf_custom_filename( $filename, $template_type, $order_ids, $context ) {
        $count = count($order_ids);
     
        switch ($template_type) {
            case 'invoice':
                $name = _n( 'invoice', 'invoices', $count, 'woocommerce-pdf-invoices-packing-slips' );
                break;
            case 'packing-slip':
                $name = _n( 'packing-slip', 'packing-slips', $count, 'woocommerce-pdf-invoices-packing-slips' );
                break;
            case 'proforma':
                $name = _n( 'proforma-invoice', 'proforma-invoices', $count, 'wpo_wcpdf_pro' );
                break;    
            case 'credit-note':
                $name = _n( 'credit-note', 'credit-notes', $count, 'wpo_wcpdf_pro' );
                break;
            default:
                $name = $template_type;
                break;
        }
     
     /*   if ( $count == 1 ) {
            $document = wcpdf_get_document( $template_type, $order_ids );
            if ( $number = $document->get_number() ) {
                $suffix = $number;
            } elseif (isset($document->order) && method_exists($document->order, 'get_order_number')) {
                $number = $document->order->get_order_number();
            } else {
                $number = $order_ids[0];
            }
            $suffix = $number;
        } else 
    	*/
    	{
            $suffix = date('d-m-Y'); // 25-07-2019
        }
    
      if ( $count == 1 ) {
    	$order = wc_get_order($order_ids[0]);
    	if ( $user = $order->get_user() ) {
    		$name = $user->user_login;
    	} else {
    		$name = $order->get_formatted_billing_full_name();
    	}
    /*	$suffix = "{$suffix}-{$name}";
    */
    }
      
        $filename = 'Order acknowledegment for ' . $name . '-' . $suffix .'.pdf';
     
        return $filename;
    }
    

    The suffix is now the date (formatted for the UK), and I’ve put in ‘Order acknowledgement for’ as plain text.

    I apologise if I’ve committed any crimes against coding, but it does seem to work, and I thought I ought to have a go at doing it myself rather than always relying on you!

    Now to look into removing those annoying dashes in the text . . .

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Email attachment error’ is closed to new replies.