如何在wp_mail函数中显示消息部分中的图像?

时间:2017-08-29 11:48:05

标签: wordpress email

我正在尝试发布帖子时发送邮件。为此,我在我的function.php文件中编写了代码,邮件正确发送,但未发送特色图像。我想显示附在帖子上的精选图片。现在邮件中没有显示特色图像,但会显示该图像的链接。

如何才能完成在邮件中显示精选图片的任务?我附上了我在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');

3 个答案:

答案 0 :(得分:2)

要通过wp_mail函数附加文件,您需要在其中使用$ attachments参数。您需要在其中提供附件的绝对文件路径。

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 :(得分:1)

您需要将内容类型设置为text/html才能使图像生效。您可以使用wp_mail_content_type filter或在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');

答案 2 :(得分:1)

尝试这个,只是在本地安装上测试,它运行良好:)你需要改变所讨论的邮件的内容类型,也回复图像的实际URL(大尺寸)。

<div class="box">
  <svg width="300" height="200" viewBox="0 0 300 200">
    <defs>
      <linearGradient id="grad">
        <stop offset="0" stop-color="#11a798" />
        <stop offset="1" stop-color="#23645d" />
      </linearGradient>
      <filter id="shadow">
        <feGaussianBlur in="SourceAlpha" stdDeviation="4" />
        <feMerge>
          <feMergeNode />
          <feMergeNode in="SourceGraphic" />
        </feMerge>
      </filter>
    </defs>
    <polygon id="shape" points="10,12 265,10 285,93 265,184 10,184" stroke="#333" stroke-width="2" fill="url(#grad)" filter="url(#shadow)" />
  </svg>
</div>