最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

attachments - Attach pdf from dynamic url

programmeradmin0浏览0评论

When I save a custom post, I would like to send a PDF by email as attachment.

I tried that :

function my_custom_save_post( $post_id, $post, $update )  {

    if( ! $update ) { return; }
    if( wp_is_post_revision( $post_id ) ) { return; }
    if( defined( 'DOING_AUTOSAVE' ) and DOING_AUTOSAVE ) { return; }
    if( $post->post_type != 'MYCUSTOMPOST' ) { return; }
$url = wp_nonce_url( admin_url( "myCustomUrl" ), 'my_nounce_name' );
    $url = file_get_contents($url);
    $attachments = array( $url );
    $headers = 'From: My Name <[email protected]>' . "\r\n";
    wp_mail( '[email protected]', 'subject', $url, $headers, $attachments );
}
add_action( 'save_post', 'my_custom_save_post', 10, 3 );

"myCustomUrl" not ending by .pdf

At first I started not to put $url = file_get_contents($url); and I received an email with the written URl (thanks to $url in wp_mail()) but without the file. URL works, if I open the link, I can see my file. With $url = file_get_contents($url); I don't get an email

How to do this please ?

When I save a custom post, I would like to send a PDF by email as attachment.

I tried that :

function my_custom_save_post( $post_id, $post, $update )  {

    if( ! $update ) { return; }
    if( wp_is_post_revision( $post_id ) ) { return; }
    if( defined( 'DOING_AUTOSAVE' ) and DOING_AUTOSAVE ) { return; }
    if( $post->post_type != 'MYCUSTOMPOST' ) { return; }
$url = wp_nonce_url( admin_url( "myCustomUrl" ), 'my_nounce_name' );
    $url = file_get_contents($url);
    $attachments = array( $url );
    $headers = 'From: My Name <[email protected]>' . "\r\n";
    wp_mail( '[email protected]', 'subject', $url, $headers, $attachments );
}
add_action( 'save_post', 'my_custom_save_post', 10, 3 );

"myCustomUrl" not ending by .pdf

At first I started not to put $url = file_get_contents($url); and I received an email with the written URl (thanks to $url in wp_mail()) but without the file. URL works, if I open the link, I can see my file. With $url = file_get_contents($url); I don't get an email

How to do this please ?

Share Improve this question edited Oct 10, 2020 at 20:27 Ivan Shatsky 8901 gold badge7 silver badges12 bronze badges asked Oct 10, 2020 at 12:25 user7734861user7734861 533 bronze badges 6
  • You need to use a file path within the server filesystem as an $attachments array item value, not the URL. – Ivan Shatsky Commented Oct 10, 2020 at 12:43
  • Okay thanks. But do you know how to save my file on the server filesystem thanks to URL – user7734861 Commented Oct 10, 2020 at 12:49
  • You don't know how to upload some file to the server? It can be uploaded like any other theme/plugin/etc file via FTP or you can use a media library to upload it. – Ivan Shatsky Commented Oct 10, 2020 at 13:19
  • I don't know how to do this with PHP, without user's action – user7734861 Commented Oct 10, 2020 at 13:20
  • I don't understand what $url = wp_nonce_url( admin_url( "myCustomUrl" ), 'my_nounce_name' ); line should do and what is your "myCustomUrl" does mean. Where are your PDF file located? – Ivan Shatsky Commented Oct 10, 2020 at 13:29
 |  Show 1 more comment

1 Answer 1

Reset to default 3

You can try to do it in two ways.

The first one, much more simple, is to save a temporary PDF file somewhere, for example in a uploads directory, use it as attachment and delete if after the call to wp_mail() function is made:

function my_custom_save_post( $post_id, $post, $update )  {
    if( ! $update ) { return; }
    if( wp_is_post_revision( $post_id ) ) { return; }
    if( defined( 'DOING_AUTOSAVE' ) and DOING_AUTOSAVE ) { return; }
    if( $post->post_type != 'MYCUSTOMPOST' ) { return; }
    $url = wp_nonce_url( admin_url( "myCustomUrl" ), 'my_nounce_name' );
    $contents = file_get_contents( $url );
    # here temporary file name is a fixed string
    # but it better to be some unique string
    # (use current timestamp, post ID, etc)
    $tempfile = ABSPATH . 'uploads/filename.pdf';
    file_put_contents( $tempfile, $contents );
    $attachments = array( $tempfile );
    $headers = 'From: My Name <[email protected]>' . "\r\n";
    wp_mail( '[email protected]', 'subject', $url, $headers, $attachments );
    unlink( $tempfile );
}
add_action( 'save_post', 'my_custom_save_post', 10, 3 );

The second is just a guess and needs to be tested. WordPress rely on PHPMailer component for sending emails. That component has a addStringAttachment method which allows to attach a binary object assigned to a PHP variable as a email file attachment. Here is the question about this topic. I don't see any way this could be used via wp_mail() function but in theory you can try to do it via phpmailer_init hook:

function my_attach_pdf( $mailer_instance ) {
    $url = wp_nonce_url( admin_url( "myCustomUrl" ), 'my_nounce_name' );
    $contents = file_get_contents( $url );
    $mailer_instance->addStringAttachment( $contents, 'your_file_name.pdf' );
}
function my_custom_save_post( $post_id, $post, $update )  {
    if( ! $update ) { return; }
    if( wp_is_post_revision( $post_id ) ) { return; }
    if( defined( 'DOING_AUTOSAVE' ) and DOING_AUTOSAVE ) { return; }
    if( $post->post_type != 'MYCUSTOMPOST' ) { return; }
    $headers = 'From: My Name <[email protected]>' . "\r\n";
    add_action( 'phpmailer_init', 'my_attach_pdf' );
    wp_mail( '[email protected]', 'subject', $url, $headers, $attachments );
    remove_action( 'phpmailer_init', 'my_attach_pdf' );
}
add_action( 'save_post', 'my_custom_save_post', 10, 3 );

I'm really curious if the second approach would work. Maybe give it a test one day. If you test it, please give some feedback.

发布评论

评论列表(0)

  1. 暂无评论