I am trying to send mail when a post is published. For that I wrote code in my function.php file, the mail is correctly sending, but the featured image is not sent. I want to display the featured image attached to the post. Right now in mail the featured image is not displayed, but the link of fthe eatured image is displayed.
What can I do to achieve my task to display the featured image in the mail? I am attaching the code I have written in function.php file:
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');
To attach file through wp_mail function, you need to use $attachments parameter in it. In which you need to give absolute file path of attachment.
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');
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments