Email attachments [message #3035] |
Mon, 16 July 2012 10:32 |
gpatti
Messages: 283 Registered: August 2008
|
Senior Member |
|
|
Tony,
Your FAQ 137 looks like you have an application that sends attachments via email. Is this in one of the prototype apps? I'd like to have a look at the code to see how you have done that if it is available.
Thanks,
Graham
|
|
|
|
|
Re: Email attachments [message #3038 is a reply to message #3037] |
Tue, 17 July 2012 08:10 |
AJM
Messages: 2363 Registered: April 2006 Location: Surrey, UK
|
Senior Member |
|
|
The button is made to appear in the screen with the following:
$this->fieldspec['add_attachment'] = array('type' => 'string',
'control' => 'button',
'task_id' => 'py_email_attachment(fileupload)');
The fileupload task has thge following code in the _cm_post_fileUpload() method:
$array['filepath'] = $filename;
$array['filename'] = $this->filename;
$array['filesize'] = $filesize;
This causes those values to be passed back to the calling form where they appear in the _cm_popupReturn() method where I have the following code:
if ($return_from == 'py_email_attachment(fileupload)') {
if (!empty($select_array['add_attachment'])) {
$filename = $select_array['add_attachment']['filename'];
$filepath = $select_array['add_attachment']['filepath'];
$filesize = $select_array['add_attachment']['filesize'];
$fieldarray['add_attachment'] = null;
// save these details for the _cm_postInsertRecord() method
$this->attachments[$filename] = $filepath .' [size='.$filesize.']';
// rebuild the list of attachments
$list = '';
foreach ($this->attachments as $key => $value) {
$list .= $key .', ';
} // foreach
$fieldarray['attachments'] = rtrim($list, ', ');
} // if
} // if
These values are processed in the _cm_postInsertRecord() method with the following code:
if (!empty($this->attachments) AND is_array($this->attachments)) {
$attachments = $this->attachments;
} elseif (!empty($rowdata['attachments']) AND is_array($rowdata['attachments'])) {
$attachments = $rowdata['attachments'];
} // if
if (!empty($attachments)) {
// $attachments has 2 possible formats:
// 1) associative - 'filename = filepath'
// 2) indexed - [idx]['filename'] = '....'
// [idx]['filesize'] = '....'
// [idx]['filebody'] = '....' or [idx]['filepath'] = '....'
$array1 = $attachments;
$attachments = array();
foreach ($array1 as $key => $value) {
if (is_array($value)) {
$attachments[] = $value;
} else {
// convert from associative to indexed
$filename = $key;
$filepath = $value;
if (preg_match('/(?<=\[size=).*(?=\])/', $filepath, $regs)) {
$filesize = $regs[0]; // extract file size
// remove '[size=...]' from the value
$filepath = preg_replace('/\[size=.*\]/', '', $filepath);
} else {
$filesize = null;
} // if
$attachments[] = array('filename' => $filename,
'filepath' => $filepath,
'filesize' => $filesize);
} // if
} // foreach
} // if
if (!empty($attachments)) {
// add each attachment to a separate table
$dbobject =& RDCsingleton::getInstance('email_attachment');
$dbobject->audit_logging = $this->audit_logging;
foreach ($attachments as $idx => $filedata) {
$insert['email_id'] = $rowdata['email_id'];
$insert['filename'] = $filedata['filename'];
$insert['filesize'] = $filedata['filesize'];
if (!empty($filedata['filebody'])) {
$insert['filebody'] = $filedata['filebody'];
} else {
$insert['filepath'] = $filedata['filepath'];
} // if
$insert = $dbobject->insertRecord($insert);
if ($dbobject->errors) {
$this->errors = array_merge($this->errors, $dbobject->errors);
return $rowdata;
} // if
} // foreach
unset($dbobject);
} // if
I hope this helps.
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
|
|
|
|