Radicore Forum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » RADICORE » How To » Email attachments
Email attachments [message #3035] Mon, 16 July 2012 10:32 Go to next message
gpatti is currently offline  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 #3036 is a reply to message #3035] Tue, 17 July 2012 01:58 Go to previous messageGo to next message
AJM is currently offline  AJM
Messages: 2363
Registered: April 2006
Location: Surrey, UK
Senior Member
I'm afraid that particular email transaction is not available in any of the prototype apps. It is part of a larger commercial package and is therefore not available as open source.

Re: Email attachments [message #3037 is a reply to message #3036] Tue, 17 July 2012 04:51 Go to previous messageGo to next message
gpatti is currently offline  gpatti
Messages: 283
Registered: August 2008
Senior Member
Thanks Tony, I appreciate that.

I don't suppose you could outline the approach you took so that I could code in a similar way?
Re: Email attachments [message #3038 is a reply to message #3037] Tue, 17 July 2012 08:10 Go to previous messageGo to next message
AJM is currently offline  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.


Re: Email attachments [message #3039 is a reply to message #3038] Tue, 17 July 2012 08:16 Go to previous message
gpatti is currently offline  gpatti
Messages: 283
Registered: August 2008
Senior Member
Many thanks. I will play around with this.
Previous Topic: Use SQL REPLACE function in UPDATE SET
Next Topic: Error reporting on UPD3 screen
Goto Forum:
  


Current Time: Thu Oct 31 20:38:41 EDT 2024

Total time taken to generate the page: 0.01166 seconds