Skip Navigation

[Resolved] Email attachments

This thread is resolved. Here is a description of the problem and solution.

Problem:

Send repeating file field as email attachments with Toolset Forms.

Solution:

It needs custom codes, for example:

https://toolset.com/forums/topic/email-attachments/#post-1685885

Relevant Documentation:

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 5 replies, has 2 voices.

Last updated by nabils 1 month ago.

Assigned support staff: Luo Yang.

Author
Posts
#1679527

Tell us what you are trying to do?
Hi,
I have a filed called communication_attachment that allows multiple instances. I am trying to send an email with attachments. I am using

 $attachment = get_post_meta($post_id, "wpcf-communication_attachment", true);
   $status= wp_mail($to, $subject, $mailbody, $from, $attachment);

The email is received but without attachment.
Is "get_post_meta" is correct in this case or I have to use other code to get attachment since it is many item (array)?

#1679965

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Hello,

How do you setup the custom field "communication_attachment"? is it a custom file field?
If it is, the custom file field created with Types plugin stores value in URL field, but you can not use file URL as email attachment, see WP document:
https://developer.wordpress.org/reference/functions/wp_mail/#notes

The filenames in the $attachments attribute have to be filesystem paths.

So you will need to turn the each file URL into file path, then put them into $attachments parameter.

See similar thread here:
https://wordpress.stackexchange.com/questions/216913/how-to-convert-the-file-path-to-a-url-of-the-same-file

And as a workaround, you can print the "communication_attachment" URL in email body, then your user can download them by clicking URLs:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-for-each
description:
Iterate through multiple items in a post meta field and output the enclosed text for each item. Including Types repeating fields.

#1684849

Could you please tell me how to turn each file URL into file path, then put them into $attachments parameter?

#1684859

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Please check the thread I mentioned above, there is a solution with custom PHP function "convert_url_to_path":
https://wordpress.stackexchange.com/questions/216913/how-to-convert-the-file-path-to-a-url-of-the-same-file/346036#346036

If you need more assistance for it, please provide a test with the same problem, also point out the problem page URL and form URL, where I can edit your PHP codes. I need a live website to test and debug the codes.

#1685885

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Please try to modify this line from:

$attachment = get_post_meta($post_id, "wpcf-communication_attachment", true);

To:

$urls = get_post_meta($post_id, "wpcf-communication_attachment", false);
$attachment = array();
foreach($urls as $url){
	$attachment[] = convert_url_to_path( $url );
}

and add the PHP function into functions.php:

function convert_url_to_path( $url ) {
  return str_replace( 
      wp_get_upload_dir()['baseurl'], 
      wp_get_upload_dir()['basedir'], 
      $url
  );
}

Then test again

#1686295

My issue is resolved now. Thank you!