A
A
Alexander Bondarenko2021-11-19 21:59:09
WordPress
Alexander Bondarenko, 2021-11-19 21:59:09

How to insert photos into wp_mail messages?

I'm trying to send a message right after a post is published and I'm trying to insert an image into the email, but the email doesn't get sent at all, without the image everything works fine

function roomble_send_notification( $new_status, $old_status, $post ) {
  if(
    'publish' === $new_status && 
    'publish' !== $old_status && 
    'post' === $post->post_type
  ) {
    remove_all_filters( 'wp_mail_from' );
    remove_all_filters( 'wp_mail_from_name' );

    $file = get_field('main_pic', $post->ID);
    $uid = 'my-cool-picture-uid';
    $name = 'file.jpg';

    global $phpmailer;
    add_action( 'phpmailer_init', function(&$phpmailer)use($file,$uid,$name){
      $phpmailer->SMTPKeepAlive = true;
      $phpmailer->AddEmbeddedImage($file, $uid, $name);
    });


    $headers = array(
      'content-type: text/html',
    );

    $multiple_to_recipients = array(
      '[email protected]'
    );
    
    $body = '<img src="cid:my-cool-picture-uid" height="500"><br/>'.$post->post_content;
    
    wp_mail( $multiple_to_recipients, $post->post_title, $body, $headers);
    
  }
  }
  add_action( 'transition_post_status', 'roomble_send_notification', 10, 3 );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-11-19
@bond_1013

Why are you even trying to send it as a file? Insert link from website

$body = '<img src="' . get_the_post_thumbnail_url( $post->ID, 'medium' ) . '" height="500"><br/>' . $post->post_content;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question