0

On a product page I have added a Contact Form 7 form that lets a visitor request more information about the product via email.

Now I want to embed the featured image (of the productpage the mail was sent from), in the email.

I'm using the plugin Contact Form 7 - Dynamic Text Extension to use hidden fields in the contact form and some custom code that I found that retrieves the image. (Original source)

// Retrieve product featured imgae.
add_shortcode( 'product_img', 'dcwd_product_img_from_product_id' );
function dcwd_product_img_from_product_id() {
    $product_id = get_the_ID();
    $product_img = 'unknown';

    if ( is_int( $product_id ) ) {
        $product_img = get_the_post_thumbnail_url( $product_id, 'thumbnail' );
    }
    return html_entity_decode( $product_img );
}

This code works, it gives the URL, but in the email it doesn't show the image when I put it in between tags.

I also tried:

$product_img = get_the_post_thumbnail( $product_id, 'thumbnail' );

This just displays the raw <img /> html code.

There is a checkmark in the Contact Form 7 email settings that says: "Use HTML content type", this makes no difference.

MaikThoma
  • 41
  • 1
  • 6

1 Answers1

0

Found the answer here, I had to convert the image to base64.

This is the final code:

// Retrieve product featured imgae.
add_shortcode( 'product_img', 'dcwd_product_img_from_produt_id' );
function dcwd_product_img_from_produt_id() {
    $product_id = get_the_ID();
    $product_img = 'unknown';

    if ( is_int( $product_id ) ) {
        $product_img = get_the_post_thumbnail_url( $product_id, 'large' );
    }

    $path = $product_img;
    $type = pathinfo($path, PATHINFO_EXTENSION);
    $data = file_get_contents($path);
    $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

    return html_entity_decode( $base64 );
}
MaikThoma
  • 41
  • 1
  • 6