Using a FORM BUILDER Plugin that provides the ability to upload files which in turn are emailed as attachments … as the uploaded filenames are random, wanting to rename them automatically to simplify downstream email processing.
The Form Builder provides a before email hook filter which I am attempting to use to rename the uploaded files before they are emailed as attachments.
Added a FORMID-POSTID Prefix to all attachments PLUS each attachment has a unique Fixed name defined by the form’s field details … all attachment files simply get renamed to:
SomeRandomFilename.SomeAcceptablePrefix -> xFORMID-POSTID-FIELDNAME.SomeAcceptablePrefix
The hook logic written in PHP queries all relevant records in posts & postmeta tables followed by successfully renaming all appropriate files then finally updates the relevant records.
Files are renamed using builtin PHP functions: glob() and rename() … files all appear to be renamed properly
posts and postmeta Tables are updated using builtin PHP functions: str_replace() with:
$wpdb->update(table,array(‘data_field’ => $data),array(‘ref_field’ => $ref)
while the filename data appears to be updated properly in both tables, for some reason the email processor is randomly ignoring some of the files. With renaming turned off, testing with 5 uploaded files, all 5 files are emailed. Same test with renaming turned on, files are randomly missing … review of folders, log files, and data shows that all files and records were updated correctly … plus … there are no error_log entries.
I went looking for wp functions to doing meta records updates in case there may be some inherent checksums that I am missing but maybe not using the right questions … regardless … if this was a checksup issue I wouldn’t expect ANY emailed files … if this is a race condition with the wpdb->update() function then perhaps I have to add a wait loop of some sort … if this is a CACHE issue where the Plugin is using previous data then I may have to build my own MAILER routine.
Thanks in advance for ideas
]]>If wp_mail() is used to send emails, the ‘phpmailer_init’ action fires just before PHPMailer::send() is called. This would be the place to check if the renamed file exists yet, and to keep checking (within reason) until it does. For servers still using spinning disks, we can possibly reach this point in code before the renaming is actually recorded on disk, e.g. a race condition.
]]>