Reply To: Insert Message from another application

Home Forums Front End PM PRO Insert Message from another application Reply To: Insert Message from another application

Craig Tucker

I am sorry. I am having a hard time figuring out your functions. fep_send_message() works fine but I need to do the attachments. I am not sure of email piping would be a good work around. I am trying to stay completely on the server for this so no patient data goes out over the internet. I may be misunderstanding what you were suggesting however. So to get the job done I just did this because I can understand the wordpress stuff:

// Logic to process the "putmessage" action.
// Sends a message to the designated user with an optional attachment.
// FrontendPM version
function action_putmessage(&$args) {
  global $wpdb, $out, $admin_user_login;
  $sender = convertToID($admin_user_login);
  if (!$sender) {
    $out['errmsg'] = "No such sender '$admin_user_login'";
  $recipient = convertToID($args['user']);
  if (!$recipient) {
    $out['errmsg'] = "No such recipient '{$args['user']}'";

	$year = date('Y');
	$month = date('m');
	$timestamp = idate("U"); 
	$upload_dir =  ABSPATH . "wp-content/uploads/front-end-pm/" . $year . "/" . $month ;
	if (!file_exists($upload_dir)) {
		mkdir($upload_dir, 0755, true);
	$filename = $args['filename'];
	$newfilename = wp_unique_filename( $upload_dir, $filename );
	$pathtofile = $upload_dir . '/' . $newfilename;
	file_put_contents($pathtofile, base64_decode($args['contents']));
	$postarr = array(
	 'post_status' => 'publish',
	 'post_type' => 'post',
	 'post_title' => $args['title'],
	 'post_content' => $args['message'],
	 'post_author' => $sender,
	 'post_type' => 'fep_message', 
	$parent_post_id = wp_insert_post($postarr);

	add_post_meta( $parent_post_id, '_fep_participants', $sender);	
	add_post_meta( $parent_post_id, '_fep_participants', $recipient);	
	add_post_meta( $parent_post_id, '_fep_parent_read_by_' .$sender , $timestamp);	
	add_post_meta( $parent_post_id, '_fep_last_reply_time', $timestamp);	
	add_post_meta( $parent_post_id, '_fep_last_reply_id', parent_post_id);
	add_post_meta( $parent_post_id, '_fep_last_reply_by', $sender);
	if(!$parent_post_id) {
		$out['errmsg'] = "Message insert failed";

	$attachment = array(
	 'post_author' => $sender,
	 'post_mime_type' => $args['mimetype'],
	 'post_title' => preg_replace('/\.[^.]+$/', '', basename($pathtofile)),
	 'post_content' => '',
	 'post_status' => 'inherit',
	 'guid' => $pathtofile,
	$attach_id = wp_insert_attachment( $attachment, $pathtofile, $parent_post_id );
	require_once( ABSPATH . 'wp-admin/includes/image.php' );
	$attach_data = wp_generate_attachment_metadata( $attach_id, $pathtofile );
	wp_update_attachment_metadata( $attach_id, $attach_data );
	if ($attach_id === false) {
	  $out['errmsg'] = "Attachment insert failed";

This works to input the post and file. I would like to trigger an email. If you have concerns about me submitting messages and attachments this way let me know. If you have a method I may be able to add to trigger your email functions let me know that. Otherwise I will write a phpmailer thing to get the job done. With this I am about 95% done with the OpenEMR interface. I will put it out on Github when it is all done.