The core of this solution is to use the Postie plugin that is free and customizable. After the plugin setup and configuration I changed its behaviour of creating posts and I create Formidable entries instead.
The sample code below contains some logic that may not apply to somebody else but the flow of processing can be easily modified. I put everything into my functions.php file.
global $ivory_to;
//just after the email is retrieved, check for issues and clean data
add_filter('postie_post_pre', 'IVORY_postie_post_pre');
function IVORY_postie_post_pre($email) {
global $ivory_to;
//preprocess email content to leave only attachemts
$email['html'] = ""; //clean email body, avoid $5 postie add-on
$email['text'] = ""; //clean email body, avoid $5 postie add-on
//extract from email address
$ivory_to = $email['headers']['from']['mailbox']."@".$email['headers']['from']['host'];
if (!filter_var($ivory_to, FILTER_VALIDATE_EMAIL)) {
preg_match("/[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}/i", $ivory_to, $matches);
$ivory_to = $matches[0];
}
//test logic if received email is well configured, send feedback in case of error
if (!is_numeric($email['headers']['subject'])) {
wp_mail( $ivory_to, "Subectul mesajului nu este numeric: " . $email['headers']['subject'], "Retransmiteti mesajul cu subiect corect.", array('Content-Type: text/html; charset=UTF-8','From: Fise IVORY ') );
$ivory_to = null;
return null;
}
else if ( strpos(FrmProDisplaysController::get_shortcode(array('id' => 111, 'filter' => 1, 'fisa' => $email['headers']['subject']) ), 'No Entries Found') !== false ) {
wp_mail( $ivory_to, "Nu exista nici o fisa cu codul: " . $email['headers']['subject'], "Retransmiteti mesajul cu subiect corect.", array('Content-Type: text/html; charset=UTF-8','From: Fise IVORY ') );
$ivory_to = null;
return null;
}
else
return $email;
}
//I only want image attachments as defined into the file upload field
add_action('postie_file_added', 'IVORY_fileadded', 10, 3);
function IVORY_fileadded($postid, $attachmentid, $fileinfo) {
$file = $fileinfo['file'];
$url = $fileinfo['url'];
$mimetype = $fileinfo['type'];
if (strpos($mimetype, 'image', 0) === 0) { //only if attachment is an image
//continue
}
else {
wp_delete_attachment( $attachmentid, true );
}
}
//final processing
add_filter('postie_post_before', 'IVORY_postie_post_function');
function IVORY_postie_post_function($post) {
global $ivory_to;
//if there is valid email message then process it
if ($ivory_to != null) {
//make array of images from attachments ids placed on separate rows in HTML post_content
$imgs = explode("n",$post['post_content']);
array_pop($imgs); //remove last empty item
$ivory_body = "Mesajul a fost procesat cu succes!nhttps://dentserver2/fisa/" . $post['post_title'];
//pop any attachment that was deleted
foreach ($imgs as $v) {
if ( wp_get_attachment_url( $v ) ) {
//sometimes
might be left over, get rid of non numeric characters
$ids .= preg_replace('/[^0-9.]+/', '', $v) . ",";
}
else {
$ivory_body = "Unele atasamente au fost eliminate pentru ca nu erau de tip imagine.nhttps://dentserver2/fisa/" . $post['post_title'];
}
}
//cut final comma from string
$ids = rtrim($ids, ',');
//make it back as an array of permitted media ids to be attached to the entry
$ids = explode(",",$ids);
//create new medical note
FrmEntry::create(array(
'form_id' => 7, //medical note form id to be appended to the patient record
'item_key' => 'entry', //change entry to a dynamic value if you would like
'frm_user_id' => '1', //change $user_ID to the id of the user of your choice (optional)
'item_meta' => array(
96 => 'Imagistica sosita prin email', //medical note text
83 => $ids, //append the valid media attachments
78 => $post['post_title'], //patient record id
77 => '1' //root user id
),
));
wp_mail( $ivory_to, "Nota medicala creata pentru fisa " . $post['post_title'], $ivory_body, array('Content-Type: text/html; charset=UTF-8','From: Fise IVORY ') );
}
//avoid to actually save any post, the default postie plugin action
return null;
}

