Additional Blog Posts by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
3,523 Views
19 Comments
h3. Purpose  Recently a discussion  (calling php functions from sap) came up in the PHP Scripting forum on how to access an arbitrary POP3 mail server from within ABAP using any scripting language to build an RFC enabled POP3 client proxy but no webserver. I'll present a prototype using PHP.   h3. Prerequisites   The examples shown here have been tested on ABAP WAS 6.40 and PHP 5.1 but they are very likely to run on much lower releases both on the ABAP and PHP side. The PHP side requires the SAPRFC libraries (SAPRFC  (http://saprfc.sourceforge.net/)) and it has to be compiled with imap support(in Windows enable php_imap.dll in php.ini).   h3. General   POP3 stands for Post Office Protocol version 3 and allows the retrieval of email from a server by a client application. The protocol is defined in RFC1939  (http://tools.ietf.org/html/1939). The second wide spread standard mail protocol is IMAP, the Internet Message Access Protocol, defined in RFC3501  (http://tools.ietf.org/html/3501).  PHP offers numerous functions to support this protocols. Those imap_* functions can be found here  (http://www.php.net/manual/en/ref.imap.php).    Although it's quite simple to read a mailbox in principle it quickly gets difficult in a practical implementation. The protocols are very mature and stable but they support very complex email objects. An emai can consist of several parts, for instance a text body and several attachments. Each part can have a different mime type and subtype (e.g. TEXT/PLAIN, TEXT/HTML, IMAGE/GIF) and it can have it's own encoding or character set(you probably know that unreadable mails due to 'mixed' character sets).   There is no defined order of this parts, so an attachment can come in front of or past the message body. This may lead to a situation, where a virus scanning program adds another TEXT/PLAIN part to the email, containing a note that the email has been scanned. Now, which one of two TEXT/PLAIN parts is the main message body? This is not defined.   Another problem arises from a fundamental conceptual difference between IMAP and POP3. IMAP is designed to support an online mail reading habit whereas POP3 is thought to be the offline mail management protocol. What does that that mean? With IMAP you download a temporary copy of an email to the client but the management of your account happens on the server. For instance, after downloading a mail you tell the server that you have  SEEN this mail. With POP3 on the other hand you download a mail to your client and 'add it' to your local permanent 'mail store'. If you synchronize the next time you fetch an overview from the server, compare it with your local store and download only those you haven't seen yet. That means, you need a local store and cannot flag the mail on the server. If you don't have a local store ('logbook') you always have to download your whole mailbox.    In this blog I'll show how to access an arbitrary mailbox by means of POP3 and PHP. The PHP programm will be designed to be a RFC server program which can be called from ABAP. So we finally have an ABAP POP3 mail client. Furthermore I'll sketch out how to identify mime types (kinds of attachments) and how to store certain kinds of attachments(images of certain formats) to the filesystem.  h3. The PHP CLIENT PROXY  There are a lot of comments in the code so here I am going to explain the script in general only. We have to read it from bottom to top. A table type interface of data to be transferred is defined. We do not hand the complete unstructured mails to ABAP but selected structured data. This is +ORIGUID+,  an UID of the email, +MDATE+, the date the mail has been sent, +MTEXT+, the message body of the email, and finally +FINAME+, a unique filname for image type attachments. All fields are set to be abap type C.  A function +FETCH_EMAIL+ is registered and the server started.   The main function is +FETCH_EMAIL+. First some parameters have to be set, most notably an image directory for the attached images to be stored to, and the connection parameters to the mailbox. If we want to show attached images in an HTML control in our ABAP mail client we should set the image directory to a location on a webserver.$msgid));                     }                   }                 }               }             }           }           // uncommented the following to delete retrieved messages from the mail server           // $status = imap_setflag_full ($mbox, imap_uid($mbox,$msgno), " DELETED", ST_UID);         }       }     }     imap_close($mbox);   }   return (true); }  // the RFC interface, a table containing selected mail data $interface = array(                array(                  "name"=>"MAILTAB",                  "type"=>"TABLE",                  "optional"=>"0",                  "def"=> array(                               array("name"=>"MTEXT", "abap"=>"C", "len"=>"255", "dec"=>0),                               array("name"=>"FINAME", "abap"=>"C", "len"=>"32", "dec"=>0),                               array("name"=>"MDATE", "abap"=>"C", "len"=>"32", "dec"=>0),                               array("name"=>"ORIGUID", "abap"=>"C", "len"=>"32", "dec"=>0)                             )                )              );  $rfc = saprfc_server_accept ($argv); saprfc_set_trace(0, true); $fcl["FETCH_EMAILS"] = saprfc_function_define(0,"FETCH_EMAILS",$interface);  while (true)   $rc = saprfc_server_dispatch ($rfc, $fcl);  ?>     h3. The ABAP CLIENT      REPORT ZTW_GET_EMAILS.  TYPES: begin of lst_mailtab,  mtext(255) type C,  finame(32) type C,  mdate(32) type C,  origuid(32) type C,  end of lst_mailtab. DATA: lt_mailtab type table of lst_mailtab,  ll_mailtab like line of lt_mailtab.   CALL FUNCTION 'fetch_emails' destination 'PHPCONNECTOR'  TABLES MAILTAB = lt_mailtab.  loop at lt_mailtab into ll_mailtab.  write: / ll_mailtab-origuid, ll_mailtab-mdate, ll_mailtab-mtext,  ll_mailtab-finame.  endloop.     h3. Putting things together   First we define a RFC connection called PHPCONNECTOR in transaction SM59 as shown in the following image:      Next we change to the directory where we put our php script (e.g. C:usrsapNSPSYSexe
unphpstuff) and create our image directory in there. Now we are ready to run our PHP RFC mail client proxy with the following command     full/path/to/php-cgi.exe -dmax_execution_time=0 mailserver.php -a PHPCONNECTOR -g the_server -x sapgw00     where +-dmax_execution_time=0+ means 'run forever', +mailserver.php+ is the name of our script, +PHPCONNECTOR+ is our RFC destination defined earlier, +the_server+ is the name of the server, and +sapgw00+ the name of the SAP gateway.  The RFC connection can now be tested in SM59. If this test is okay we are ready to run our ABAP report. 
19 Comments