如何将特色图片嵌入到电子邮件中?
How to embed featured image to an email?
在产品页面上,我添加了 Contact Form 7 表单,让访问者可以通过电子邮件请求有关产品的更多信息。
现在我想在电子邮件中嵌入特色图片(来自发送邮件的产品页面)。
我正在使用插件 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 );
}
这段代码有效,它给出了 URL,但是在电子邮件中,当我将它放在标签之间时它不显示图像。
我也试过:
$product_img = get_the_post_thumbnail( $product_id, 'thumbnail' );
这只显示原始 <img />
html 代码。
Contact Form 7 电子邮件设置中有一个复选标记:“使用 HTML 内容类型”,这没有区别。
找到答案here,我必须将图像转换为 base64。
这是最终代码:
// 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 );
}
在产品页面上,我添加了 Contact Form 7 表单,让访问者可以通过电子邮件请求有关产品的更多信息。
现在我想在电子邮件中嵌入特色图片(来自发送邮件的产品页面)。
我正在使用插件 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 );
}
这段代码有效,它给出了 URL,但是在电子邮件中,当我将它放在标签之间时它不显示图像。
我也试过:
$product_img = get_the_post_thumbnail( $product_id, 'thumbnail' );
这只显示原始 <img />
html 代码。
Contact Form 7 电子邮件设置中有一个复选标记:“使用 HTML 内容类型”,这没有区别。
找到答案here,我必须将图像转换为 base64。
这是最终代码:
// 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 );
}