The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

Create entries with attached media via email

Labels

This Discussion is public

Notifications

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!nhttp://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.nhttp://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;
}

this editor is stripping stuff, does not like empty rows, etc.

For instance this line

$imgs = explode("n",$post['post_content']);

should be

$imgs = explode("\n",$post['post_content']);

Hi Rvencu,

Can you explain what the application was?   This way I can better understand the solution.

I'm assuming that with your solution a user can send an email with information formatted a certain way that will then be added as a new form entry?  is that right?

If so, I wonder what the method for ensuring the right data aligns to the right fields is?  This might work with a small simple form but surely there'd be lots of issues with a larger more complex form.  Humans don't tend to follow rules when entering information in freeform text.  :)

If you had a template like say in a word or excel document this would help lock down some of this perhaps?

I'm interested to learn more.

Thanks,

John.

 

Hi John

Yes, you are right. This is a medical record application and sometimes patients send their images via email. By forwarding the email to a robot mailbox, changing the subject to their patient ID we can easily add these images to the patient record. It was a way for us to minimize time to deal with these incoming images in matter of seconds. Before that saving the images to a proper folder structure was taking minutes of the operator time.

In Postie plugin you can handle various elements of the incoming email. I would not dare to hope that someone can structure the email body in any way so data could be extracted from there and put into various fields.

That sounds a perfect solution to a manual problem.  Well done.

I'll store this for future reference.

Thanks.

Discussion closed.