Ready to get started?Download WordPress


WooCommerce - excelling eCommerce
How can I send an attachment with my email? (10 posts)

  1. TimoVeld
    Posted 10 months ago #

    How would I go about attaching a file to the order complete email?
    I have my file location, but I have no idea how I would attach a file to the email generated in woocommerce.

    How would I go about adding something like this?


  2. saas
    Posted 10 months ago #

  3. TimoVeld
    Posted 10 months ago #

    Yeah I found that one. But I'd rather have a hardcoded sollution.
    Since the file is generated within the code, and the file is generated per order.

    I found the part that send the email, this uses the normal wp_mail class. Which should allow an attachment to be send. But I'm having a hard time decrypting the construction, and how to fill the attachment variable via the normal template.

  4. saas
    Posted 10 months ago #

    I see,

    may be you want to have a look at this filter


    and try to attach your attachment files via this?

  5. TimoVeld
    Posted 10 months ago #

    I found this in class-wc-emails.php

    function send( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments , $content_type = 'text/html' ) {
    		$attachments = array(WP_CONTENT_DIR . '/uploads/invoice.pdf'); //my added code
    		// Set content type
    		$this->_content_type = $content_type;
    		// Filters for the email
    		add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
    		add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
    		add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
    		// Send
    		wp_mail( $to, $subject, $message, $headers, $attachments );
    		// Unhook filters
    		remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
    		remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
    		remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );

    I found this piece of code, where I added in my $attachments variable. But for some reason that doesn not work.
    I'm at a loss here.

  6. saas
    Posted 10 months ago #

    :), first of all don't hardcode anything into the core (in this case woocommerc) this is technically, morally wrong (and may even kill kittens every time this code runs on your website :D).

    So that's wrong, and secondly your code is incorrect.

  7. TimoVeld
    Posted 10 months ago #

    I noticed it is incorrect since it still don't have any delicious attachments in my email inbox, what I can't figure out is why.

  8. saas
    Posted 10 months ago #

    I suggest you explore the filter

    add_filter('woocommerce_email_attachments','handle_my_auto_email_attachments'), 10, 2);

    function handle_my_auto_email_attachments($attachment = "", $emailtype = ''){
    //do your stuff here

    I wish I had a working example, but I don't have it :).

    Note: I suggest you try to buy the "Email Attachemnts" plugin from woothemes and see how they did it and that will surely help you achieve what you want.

  9. TimoVeld
    Posted 10 months ago #

    I'll look in to that, if I can't find my solution there I'll probably do your second suggestion.
    Thanks for the input =)

  10. saas
    Posted 10 months ago #

    No problem welcome!. :)


You must log in to post.

About this Plugin

About this Topic