Skip to content

Commit

Permalink
Multiple attachment support added for email to fax
Browse files Browse the repository at this point in the history
  • Loading branch information
nasirbest committed Aug 30, 2018
1 parent 5405f1f commit b996677
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 39 deletions.
4 changes: 3 additions & 1 deletion bin/sendmail/gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
} // foreach part
} // if attachment

$aAttachment = array();
$filename = NULL;

/* iterate through each attachment and save it */
Expand All @@ -116,6 +117,7 @@
$fp = fopen($filename, "w");
fwrite($fp, $attachment['attachment']);
fclose($fp);
$aAttachment[] = $filename;
}
}

Expand All @@ -126,7 +128,7 @@
$from = isset($header->from) ? $header->from[0] : $header->sender[0];
$date = $header->date;
$to = isset($header->to) ? $header->to[0] : $header->reply_to[0];
$attachment = $filename;
$attachment = \ICT\Core\path_array_to_string($aAttachment);

$status = imap_setflag_full($conn, $email_number, "\\Seen");
try {
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"twig/twig": "^1.35",
"jacwright/restserver": "dev-master",
"firebase/php-jwt": "^5.0",
"firehed/processmanager": "dev-master"
"firehed/processmanager": "dev-master",
"ncjoes/office-converter": "^0.1.3"
},
"autoload": {
"psr-4": {
Expand Down
47 changes: 45 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/Application/Email_receive.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ public function process()
return Spool::STATUS_COMPLETED;
}

}
}
15 changes: 10 additions & 5 deletions core/Gateway/Sendmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,16 @@ public function send($command, Provider $oProvider = NULL)
if (!empty($data['body_alt'])) {
$mailMsg->addPart($data['body_alt'], 'text/plain');
}
if (!empty($data['attachment']) && is_file($data['attachment'])) {
// Optionally add any attachments
$attachment = Swift_Attachment::fromPath($data['attachment']);
// $attachment->setFilename($data['file_title']);
$mailMsg->attach($attachment);
// Optionally add attachments
if (!empty($data['attachment'])) {
$aAttachment = \ICT\Core\path_string_to_array($data['attachment']);
foreach($aAttachment as $attachment) {
if (is_file($attachment)) {
$oAttachment = Swift_Attachment::fromPath($attachment);
// $oAttachment->setFilename($data['file_title']);
$mailMsg->attach($oAttachment);
}
}
}
} catch (Exception $msg_error) {
throw new CoreException("500", "error while preparing email message", $msg_error);
Expand Down
25 changes: 16 additions & 9 deletions core/Message/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,24 @@ public function delete()
protected function set_file_name($file_path)
{
global $path_data;
if (!empty($this->type)) {
$file_type = $this->type;
} else {
$aType = explode('.', $file_path);
$file_type = end($aType);
$this->type = strtolower($file_type);
}
$oSession = Session::get_instance();
$user_id = empty(User::$user) ? 0 : $oSession->user->user_id;
$file_name = 'document_' . $user_id . '_';
$file_name .= DB::next_record_id($file_name);
$tiff_file = $path_data . DIRECTORY_SEPARATOR . 'document' . DIRECTORY_SEPARATOR . $file_name . '.tif';
$this->create_tiff($file_path, $file_type, $tiff_file);

if (!empty($this->type)) {
$file_type = $this->type;
}

$aFile = \ICT\Core\path_string_to_array($file_path);
foreach($aFile as $file) {
$aType = explode('.', $file);
$file_type = isset($this->type) ? $this->type : end($aType);
$file_type = strtolower($file_type);
$this->create_tiff($file, $file_type, $tiff_file); // it will append new tiff file into $tiff_file
}
$this->type = $file_type;
$this->file_name = $tiff_file;
}

Expand Down Expand Up @@ -284,10 +289,12 @@ private function create_tiff($sourceFile, $type, $targetFile)
$resolution_string = $this->resolution_x . "x" . $this->resolution_y;
//$cmd = "convert -quiet -density 150 $sourceFile -shave 65x65 -colorspace rgb -quality 100 -resample 320 $targetFile";
//$cmd = "cat $sourceFile | gs -q -sDEVICE=tiffg3 -sPAPERSIZE=a4 -r204x196 -dNOPAUSE -sOutputFile=$targetFile"; //
$cmd = \ICT\Core\sys_which('gs', '/usr/bin') . " -dBATCH -dNOPAUSE -sDEVICE=tiffg3 -r$resolution_string -sOutputFile='$targetFile' -dFIXEDMEDIA -dDEVICEWIDTHPOINTS=$this->size_y -dDEVICEHEIGHTPOINTS=$this->size_x -f '$pdfFile.ps'";
$cmd = \ICT\Core\sys_which('gs', '/usr/bin') . " -dBATCH -dNOPAUSE -sDEVICE=tiffg3 -r$resolution_string -sOutputFile='$targetFile.tmp' -dFIXEDMEDIA -dDEVICEWIDTHPOINTS=$this->size_y -dDEVICEHEIGHTPOINTS=$this->size_x -f '$pdfFile.ps'";
Corelog::log("Converting source image into fax support tiff", Corelog::CRUD, $cmd);
exec($cmd);
//exec("rm -rf '$sourceFile'");
$cmd = \ICT\Core\sys_which('tiffcp', '/usr/bin') . " -a '$targetFile.tmp' '$targetFile'"; // -a for append
exec("rm -rf '$targetFile.tmp'");

return $this->pages;
}
Expand Down
52 changes: 33 additions & 19 deletions core/Message/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

class Template extends Message
{
const ATTACHMENT_MAXIMUM = 10;

protected static $table = 'template';
protected static $primary_key = 'template_id';
Expand Down Expand Up @@ -68,6 +69,7 @@ class Template extends Message
* @var string
*/
protected $attachment = NULL;
protected $aAttachment = array();

/**
* @property-read integer $length
Expand Down Expand Up @@ -151,7 +153,7 @@ protected function load()
$this->subject = $data['subject'];
$this->body = $data['body'];
$this->body_alt = $data['body_alt'];
$this->attachment = $data['attachment'];
$this->attachment = json_decode($data['attachment']);
$this->type = $data['type'];
$this->length = $data['length'];
Corelog::log("Template loaded name: $this->name", Corelog::CRUD);
Expand All @@ -166,33 +168,45 @@ public function delete()
return DB::delete(self::$table, 'template_id', $this->template_id, true);
}

protected function get_attachment() {
return \ICT\Core\path_array_to_string($this->aAttachment);
}

protected function set_attachment($file_path)
{
global $path_data;
if (file_exists($file_path)) {
$oSession = Session::get_instance();
$user_id = empty(User::$user) ? 0 : $oSession->user->user_id;
$file_parts = explode('.', $file_path);
$raw_type = strtolower(end($file_parts));
$file_type = empty($raw_type) ? 'html' : $raw_type;
$file_name = 'attachment_' . $user_id . '_';
$file_name .= DB::next_record_id($file_name);
$dst_file = $path_data . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $file_name . '.' . $file_type;
rename($file_path, $dst_file);
$this->attachment = $dst_file;
$oSession = Session::get_instance();
$user_id = empty(User::$user) ? 0 : $oSession->user->user_id;

if ($file_path === NULL) {
$this->aAttachment = array();
} else {
$this->attachment = NULL;
return; // invalid file
$aAttachment = \ICT\Core\path_string_to_array($file_path);
foreach($aAttachment as $attachment) {
if (file_exists($attachment)) {
$file_parts = explode('.', $attachment);
$raw_type = strtolower(end($file_parts));
$file_type = empty($raw_type) ? 'html' : $raw_type;
$file_name = 'attachment_' . $user_id . '_';
$file_name .= DB::next_record_id($file_name);
$dst_file = $path_data . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $file_name . '.' . $file_type;
rename($file_path, $dst_file);
$this->aAttachment[] = $dst_file;
}
}
}
$this->attachment = \ICT\Core\path_array_to_string($this->aAttachment);
}

public function save()
{
$attachment_data = '';
if (file_exists($this->attachment)) {
$attachment_data = file_get_contents($this->attachment);
$this->length = strlen($this->body . $this->body_alt . $this->subject);
foreach($this->aAttachment as $attachment) {
if (file_exists($attachment)) {
$attachment_data = file_get_contents($attachment);
$this->length += strlen($attachment_data);
}
}
$this->length = strlen($this->body . $this->body_alt . $this->subject . $attachment_data);

$data = array(
'template_id' => $this->template_id,
Expand All @@ -202,7 +216,7 @@ public function save()
'subject' => $this->subject,
'body' => $this->body,
'body_alt' => $this->body_alt,
'attachment' => $this->attachment,
'attachment' => json_encode($this->aAttachment),
'length' => $this->length
);

Expand Down
8 changes: 8 additions & 0 deletions core/lib/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ function path_to_namespace($path)
return $namespace;
}

function path_array_to_string($aFile) {
return implode(',', $aFile);
}

function path_string_to_array($path) {
return explode(',', $path);
}

/**
* Return an array from given bitmask
* @param integer $mask Integer of the bit
Expand Down
2 changes: 1 addition & 1 deletion db/email.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ CREATE TABLE template
subject varchar(255) NOT NULL default '',
body text,
body_alt text,
attachment varchar(255) NOT NULL default '',
attachment text, NOT NULL default '',
length int(11) NOT NULL default 0,
date_created int(11) default NULL,
created_by int(11) unsigned default NULL,
Expand Down

0 comments on commit b996677

Please sign in to comment.