2017-08-29 71 views
2

我正在嘗試發佈帖子時發送郵件。爲此,我在我的function.php文件中編寫了代碼,郵件正確發送,但不會發送特色圖像。我想顯示附加到該帖子的精選圖片。現在在郵件中不顯示特色圖像,但會顯示特殊圖像的鏈接。如何在wp_mail函數的消息部分顯示圖像?

如何才能完成我的任務,以在郵件中顯示精選圖像?我附上我已經寫在function.php文件中的代碼:

function mysendmail($post_id) { 

$post = get_post($post_id); 
$author = get_userdata($post->post_author); 
$subject = "Post Published: ".$post->post_title.""; 

$message = " 
     Hi ".$author->display_name.", 

     Your post, \"".$post->post_title."\" has just been published. 

     View post: ".get_permalink($post_id)." 

     Your Image: ".get_the_post_thumbnail($post->ID)." 

     Thanks" 
     ; 

    wp_mail($author->user_email, $subject, $message); 
} 
add_action('publish_post', 'mysendmail'); 

回答

2

要通過wp_mail功能附加文件,你需要在它使用$附件參數。其中您需要提供絕對的附件文件路徑。

function mysendmail($post_id) { 

$post = get_post($post_id); 
$author = get_userdata($post->post_author); 
$subject = "Post Published: ".$post->post_title.""; 


$attachments = get_attached_file(get_post_thumbnail_id($post_id)); 
$headers[] = ''; 


$message = " 
     Hi ".$author->display_name.", 

     Your post, \"".$post->post_title."\" has just been published. 

     View post: ".get_permalink($post_id)." 

     Thanks" 
     ; 

    wp_mail($author->user_email, $subject, $message, $headers, $attachments); 
} 
add_action('publish_post', 'mysendmail'); 
1

您需要設置內容類型爲text/html才能使圖像生效。您可以使用wp_mail_content_typefilter或通過在您的wp_mail()函數中添加標題來完成此操作。這是後者的一個例子。

function mysendmail($post_id) { 

    $post = get_post($post_id); 
    $author = get_userdata($post->post_author); 
    $subject = "Post Published: ".$post->post_title.""; 
    $headers = array('Content-Type: text/html; charset=UTF-8'); 

    $message = " 
      Hi ".$author->display_name.", 

      Your post, \"".$post->post_title."\" has just been published. 

      View post: ".get_permalink($post_id)." 

      Your Image: ".get_the_post_thumbnail($post->ID)." 

      Thanks" 
      ; 

    wp_mail($author->user_email, $subject, $message, $headers); 
} 
add_action('publish_post', 'mysendmail'); 
+0

剛剛嘗試過將頭添加到wp_mail()函數的代碼,但在郵件輸出中,「your image:」字段顯示空白。 – Abhee

1

試試這一個,只是測試在本地安裝和它工作得很好:)你需要更改內容類型爲討論的郵件,即時通訊也恢復圖像(大尺寸)的實際URL。

function set_html_content_type() { 
    return 'text/html'; 
} 
function mysendmail($post_id) { 
add_filter('wp_mail_content_type', 'set_html_content_type'); 

$post = get_post($post_id); 
$author = get_userdata($post->post_author); 
$subject = "Post Published: ".$post->post_title.""; 

$image = get_the_post_thumbnail_url($post_id, 'large'); 

$message = ' 
     Hi '.$author->display_name.',<br/><br/> 

     Your post, '.$post->post_title.' has just been published.<br/> 

     View post: <a href="'.get_permalink($post_id).'">'.get_permalink($post_id).'</a><br/> 

     Your Image: <img src="'.$image.'" /><br/><br/> 

     Thanks'; 

    wp_mail($author->user_email, $subject, $message); 
    remove_filter('wp_mail_content_type', 'set_html_content_type'); 
} 
add_action('publish_post', 'mysendmail'); 
+0

爲您添加了一些格式並鏈接了帖子標題 – Rich

相關問題