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)?
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.
Could you please tell me how to turn each file URL into file path, then put them into $attachments parameter?
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.
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
My issue is resolved now. Thank you!