Search code examples
phplotus-noteslotus-dominolotusscript

Lotusscript: Issue sending a file from an email to a website in PHP


I would like to send the files from my current email to my website via a lotusscript agent. I would like to do something like that: (I know this code can't work but it's to show the idea)

Sub Initialize
    On Error GoTo ErrorHandler
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim doc As NotesDocument
    Dim item As Variant
    Dim uidoc As NotesUIDocument
    Dim CurrentDocColl As NotesDocumentCollection
    Dim url As String
    Dim jsonBody As String
    Dim http As NotesHTTPRequest
    Dim headers As Variant
    Dim ret As Variant

    Set Session = New NotesSession
    Set db = Session.Currentdatabase
    Set CurrentDocColl = db.Unprocesseddocuments 
    Set doc = CurrentDocColl.Getfirstdocument
    While Not doc Is Nothing
        Set item = doc.GETFIRSTITEM("Body")
        If doc.HasEmbedded Then
            url="http://myurl.com/addFiles.php"
            jsonBody="value={""id"":""7777"",""file"":"""+item.EmbeddedObjects+"""}"

            Set http=session.CreateHTTPRequest()
            http.preferstrings = True

            Call http.SetHeaderField("ContentType","application/json")

            ret = http.Post(url, jsonBody)
            MessageBox ret
        End If
        Set doc=CurrentDocColl.Getnextdocument(doc)
    Wend
    
    Exit Sub
ErrorHandler:  
    MessageBox "Erreur N° : " +Cstr(Err)_    ' code numérique de l'erreur  
    +" Description : " + Error(Err)_      ' La description de l'erreur 
    + " Ligne N° : "+ CStr(Erl)_   ' La ligne où se trouve l'erreur 
    +"",16, " ERREUR !" 
    Exit Sub
End Sub

Here's my php code that would retrieve the files:

header("Vary: Origin");
header("Content-type: application/json");

if (isset($_FILES['file'])) {
    $total = count($_FILES['file']['name']);
    for ($i = 0; $i < $total; $i++) {
        $tmpFileName = $_FILES['file']['tmp_name'][$i];
        echo $tmpFileName;
    }
}

How can I send his files to my website? Because normally in Javascript when we want to send files to PHP code, we send this in a FormData that contains a "file" type field. But I don't know an equivalent in lotusscript. I'm a little confused about how to do this. Thank you for your help!


Solution

  • You're going to need to use the ExtractFile method of the NotesEmbeddedObject class to save the attachment data to a file, and then you're going to need to read the contents of the file and put it into the variable that you send for the POST data. As far as I know, there is no shortcut for doing this in a single step. It's been years since I used this technique, and it was in Java code rather than LotusScript. Note that your code will have to have permissions to create files on the server or client where the code is running.