[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4688: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3823)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4690: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3823)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4691: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3823)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4692: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3823)
Poco Forums • View topic - Script to put list of attachments in message body

Script to put list of attachments in message body

Scripting questions and ideas

Moderators: Eric, Tomas, robin, Michael

Script to put list of attachments in message body

Postby Pete » Fri Apr 15, 2005 4:41 am

This script will add a list of attachments to the body of a message or template.

Since the Poco Systems forums have a habit of "disappearing" :) (I posted this script last year in the old forums), I now put my script instructions inside my scripts, which is probably a good idea anyway. You only need to read the instructions at the beginning of the script.

Code: Select all
{###############################################################################
{  Script: Attachment Lister.poc
{  Author: PJR
{###############################################################################
{
{ This script will add a list of attachments to the body of a message or template.
{
{ Note:  This script will not work with Outgoing P & S messages.  I recommend
{        that you consider switching permanently from this format to
{        the Styled format in your PocoMail/Barca settings.
{
{ Setup Instructions:
{
{    1) Copy this script to a new PocoScript file (ends with .poc extension)
{       and put it in PocoMail's "Scripts" folder.
{
{    2) In PocoMail, go to "Tools > Scripts", select the new script in the
{       drop-down box at the top, click on the "Setup Script" tab, and enter
{       the options that you want the script to use.  (described below)
{       Click the Close button at the bottom-right of the window.
{
{    3) If you use templates, put the tag in the desired templates.
{
{    4) In PocoMail, go to "Tools > Filters" and click on the "Add Script"
{       button at the bottom and specify this script.  (Optionally, you can
{       click on "New Filter" if you want to specify conditions for when
{       this script runs.)
{       You can do this either in the Incoming section of the Filters window,
{       the Outgoing section, the Post-Send section, or any combination of these.
{       Position the filter in the list as desired (use drag-and-drop).
{       Close the Filters window and everything should work!
{
{ For step (2) of the instructions, here are more details about the options:
{
{###### Specify the tag to use:  %AttachList%
{
{   * By default, the script will use a tag of %AttachList%
{   * The script will search the body of a message or your template
{     for the first occurrence of this string and, if found, it will replace it
{     with the list of attachments.  If not found, it will put the list
{     at the end of the message (depending on the other options).
{   * If there are no attachments, then the script will simply remove the
{     tag if it finds it.
{   * The tag is case-sensitive (i.e. %ATTACHLIST% is different than %AttachList%)
{   * The percent signs are optional.
{
{###### Always add attachment list?  Yes
{
{   * By default, the script will always add the attachment list to the
{     message/template if attachments exist.
{   * Set this option to No if you only want the script to add the list
{     if it finds the tag that you defined above.
{
{###### Use full file path?  No
{
{   * By default, the script will use just the filename of each attachment.
{   * Set this option to Yes if you also want to see the full path
{     (e.g. C:\MyFolder\MyAttachment.pdf).
{   * This option only applies to outgoing messages.
{
{###############################################################################
{ History:
{
{  02 Sep 2005
{      * Changed again how it determines if a message is Styled (HTML).
{        Now it considers a message to be Styled if any one of these three
{        things are true:  (1) there is a </body> tag, (2) there is a
{        </html> tag, or (3) the content-type header is "text/html".
{  29 Apr 2005
{      * It won't do anything if it thinks that it has already run on the
{        message (i.e. if it finds the phrase "Attached Files:")
{  15 Apr 2005
{      * Added more instructions at the beginning of the script.
{      * Changed how it determines if a message is Styled (HTML).
{        Before, it considered a message to be styled if a </body>
{        tag existed.  That didn't work with messages that didn't
{        use <body> tags. Now it uses the Content-Type header instead.
{  22 Jun 2004
{      * Use "horizontal rule" HTML tag for styled messages
{        instead of -------------------- at beginning of attachment list.
{      * Use italics for the phrase "Attached files:" for styled messages.
{  29 Mar 2004
{      * Search for </body> tag is now case-insensitive.
{      * Attachment filenames that start with an underscore (_)
{        or that are numeric are now excluded from the list.
{        This is so that PocoMail's emoticons are not included
{        in the list of attachments.
{  06 Mar 2004
{      * Added "External" statements for the user's convenience.
{  03 Mar 2004
{      * Initial release.
{###############################################################################

Set $OnErrorMessage ""
AddStrings $OnErrorMessage "Error in " $pocoScriptName
Set $OnErrorGoTo "POCO_ERROR"

External $userdata1 "Specify the tag to use:" "%AttachList%"
Set $tag $userdata1

External $userdata2 "Always add attachment list?%bool%" "Yes"
Set &alwaysAddList $userdata2

External $userdata3 "Use full file path?%bool%" "No"
Set &useFullPath $userdata3

ImportSettings

{ Don't do anything if the phrase "Attached files:" already exists }
ReadRawBody $rawBody %message
StringPos #index "Attached files:" $rawBody
If #index > 0 Then DONE

{ Find and remove tag from message if present }
StringPos #tagIndex $tag $rawBody
If #tagIndex = 0 Then TAG_NOT_FOUND
Set &tagFound TRUE
CharCount #tagSize $tag
ChopString $rawBody #tagIndex #tagSize
Goto DETERMINE_BODY_FORMAT

{ Possibly quit if tag not found }
:TAG_NOT_FOUND
Set &tagFound FALSE
If &alwaysAddList Then DETERMINE_BODY_FORMAT
Exit

{ Determine if this is a Styled message or a Plaintext message. }
:DETERMINE_BODY_FORMAT
Set $isStyled "False"
Set $rawBodyLowercase $rawBody
Lowercase $rawBodyLowercase
StringPos #endOfBodyIndex "</body>" $rawBodyLowercase
If #endOfBodyIndex > 0 Then IS_STYLED
StringPos #endOfBodyIndex "</html>" $rawBodyLowercase
If #endOfBodyIndex > 0 Then IS_STYLED
ReadHeader $contentType "Content-Type:" %message
Lowercase $contentType
If $contentType = "text/html" Then IS_STYLED
Goto SET_END_OF_BODY_INDEX

:IS_STYLED
Set $isStyled "True"

:SET_END_OF_BODY_INDEX
If &tagFound Then COUNT_ATTACHMENTS
If #endOfBodyIndex > 0 Then COUNT_ATTACHMENTS
Call STRIP_TRAILING_LINEFEEDS
Set #endOfBodyIndex #STL_bodySize
Inc #endOfBodyIndex

{ Get list of attachments for message.
{ If none, then save message without tag (if tag was found) and quit.
:COUNT_ATTACHMENTS
ReadAttached $attachmentList %message
LineCount #attachmentListSize $attachmentList
Call ATTACHMENT_FILTER
If #attachmentListSize > 0 Then HANDLE_BODY_FORMAT
If &tagFound Then ASSIGN_BODY
Exit

:HANDLE_BODY_FORMAT
If $isStyled = "True" Then HANDLE_STYLED
Set $lineTerminator "\n"
Set $headerText "\n\n---------------\nAttached files:\n\n"
Goto LOOP_PREPARE

:HANDLE_STYLED
Set $lineTerminator "<br>"
Set $headerText "<br><br><hr align=\"left\" width=\"35%\"><i>Attached files:</i><br><br>"

{ Prepare for loop.  Add header text for attachment list at bottom of
{ message if user didn't put a tag in the message.
:LOOP_PREPARE
Set $newAttachmentList ""
Set #i 0
If &tagFound Then LOOP
Set #tagIndex #endOfBodyIndex
AddStrings $newAttachmentList $headerText

{ For each attachment... }
:LOOP
If #i = #attachmentListSize Then ADD_EXTRA_BLANK_LINE
GetLine $attachmentName #i $attachmentList
If &useFullPath Then LOOP_ADD_LIST
Call BASENAME

:LOOP_ADD_LIST
AddStrings $newAttachmentList $attachmentName $lineTerminator
Inc #i
Goto LOOP

{ Add extra blank line because PocoMail doesn't add one between
{ the body and the text "Attached image has been moved or deleted".
:ADD_EXTRA_BLANK_LINE
If $isStyled = "False" Then INSERT_LIST
If &tagFound Then INSERT_LIST
AddStrings $newAttachmentList $lineTerminator

{ Insert the new list of attachments into the message }
:INSERT_LIST
InsertString $rawBody #tagIndex $newAttachmentList

:ASSIGN_BODY
If $isStyled = "True" Then ASSIGN_STYLED
AssignBody %message $rawBody
Goto SAVE_MESSAGE

:ASSIGN_STYLED
AssignStyledBody %message $rawBody

{ Save the message }
:SAVE_MESSAGE
If #pocoScriptMode ! 5 Then DONE
SaveMessage %message $currentMailbox
DeleteMessage %message

:DONE
Exit

:POCO_ERROR
Exit

{###############################################################################
{# Subroutines
{###############################################################################

{****************************************************************************}
{* ATTACHMENT FILTER
{*   Removes attachment filenames from the list if:
{*
{*        1) the filename starts with an underscore (_), or
{*
{*        2) the filename is numeric (up to but not including the extension)
{*           (current implementation only works for integral numbers
{*            up to about two billion)
{*
{*    Modifies $attachmentList
{*    Modifies #attachmentListSize
{****************************************************************************}
:ATTACHMENT_FILTER
Set #AF_listIndex #attachmentListSize

:AF_MAIN_LOOP
Dec #AF_listIndex
If #AF_listIndex = -1 Then Return
GetLine $attachmentName #AF_listIndex $attachmentList
Call BASENAME

{ Check for leading underscore (_)
Char $AF_char 1 $attachmentName
If $AF_char = "_" Then AF_REMOVE_FROM_LIST

{ Check for numeric filename (up to but not including the extension)
Call REMOVE_EXTENSION
IsNumber &AF_isFilenameNumeric $attachmentName
If &AF_isFilenameNumeric Then AF_REMOVE_FROM_LIST
Goto AF_MAIN_LOOP

:AF_REMOVE_FROM_LIST
DeleteLine $attachmentList #AF_listIndex 1
Dec #attachmentListSize
Goto AF_MAIN_LOOP


{**************************************************************}
{* REMOVE EXTENSION (removes the extension from the filename)
{*
{*    Modifies $attachmentName
{**************************************************************}
:REMOVE_EXTENSION
CharCount #RE_attachmentNameIndex $attachmentName

:RE_LOOP
If #RE_attachmentNameIndex = 0 Then Return
Char $RE_char #RE_attachmentNameIndex $attachmentName
If $RE_char = "." Then RE_FOUND_DOT
Dec #RE_attachmentNameIndex
Goto RE_LOOP

:RE_FOUND_DOT
ChopString $attachmentName #RE_attachmentNameIndex 9999
Return


{*************************************************}
{* BASENAME (removes the path from the filename)
{*
{*    Modifies $attachmentName
{*************************************************}
:BASENAME
StringPos #BN_index \ $attachmentName
If #BN_index = 0 Then Return
ChopString $attachmentName 1 #BN_index
Goto BASENAME


{****************************************************************************}
{* STRIP TRAILING LINEFEEDS (removes linefeeds from the end of the message)
{*
{*    Modifies $rawBody
{*    Returns #STL_bodySize
{****************************************************************************}
:STRIP_TRAILING_LINEFEEDS
CharCount #STL_bodySize $rawBody

:STRIP_LOOP
If #STL_bodySize = 0 Then Return
Char $STL_ch #STL_bodySize $rawBody
CharToInt #STL_ch $STL_ch
If #STL_ch = 10 Then STRIP_IT
If #STL_ch = 13 Then STRIP_IT
Return

:STRIP_IT
ChopString $rawBody #STL_bodySize 1
Dec #STL_bodySize
Goto STRIP_LOOP
Last edited by Pete on Mon Sep 12, 2005 3:21 pm, edited 2 times in total.
Pete
 

Postby Pete » Mon Sep 12, 2005 3:15 pm

I've just posted the most recent version of this script. Here are the comments for the changes:

Code: Select all
{  02 Sep 2005
{      * Changed again how it determines if a message is Styled (HTML).
{        Now it considers a message to be Styled if any one of these three
{        things are true:  (1) there is a </body> tag, (2) there is a
{        </html> tag, or (3) the content-type header is "text/html".

This was necessary because I discovered that some email clients only specify one or two of these things, so now the script should be better at determining if a message is Styled.


Code: Select all
{  29 Apr 2005
{      * It won't do anything if it thinks that it has already run on the
{        message (i.e. if it finds the phrase "Attached Files:")

I added this because I correspond with another PocoMail user who also uses this script. Before this change, the list of attachments was being added to the messages too many times.


Code: Select all
{ Note:  This script will not work with Outgoing P & S messages.  I recommend
{        that you consider switching permanently from this format to
{        the Styled format in your PocoMail/Barca settings.

PocoScript was designed to handle either Plain messages or Styled messages, but (as far as I know) it doesn't have the necessary facilities to handle the P & S format, especially when using commands like AssignBody and AssignStyledBody. If you try to use this script with Outgoing P & S messages, PocoScript will add the list of attached files to the Plain part, but any of your correspondents that use a modern email client will probably only see the Styled part and the list of attachments will not be in that part.

I understand the purpose of P & S messages, but I think that any modern email client that is smart enough to decode a P & S message is probably also smart enough to convert a Styled message to plaintext if the recipient of the message requires that.

I handle this by setting PocoMail to use Styled format all of the time. For the rare exception, if a correspondent tells me that he/she cannot handle Styled format and needs me to send only in plaintext format, then I select the plaintext checkbox in the properties of that contact in PocoMail's address book.
Pete
 


Return to PocoScript Help and How-To

Who is online

Users browsing this forum: No registered users and 1 guest

cron