segunda-feira, 9 de julho de 2012

How to send HTML formatted mail messages using a LotusScript agent

REF: http://www-01.ibm.com/support/docview.wss?uid=swg21098323

Question

How can you send HTML formatted mail messages using LotusScript?
Prior to the expansion of the NotesMIMEEntity class in Lotus Notes and Lotus Domino Designer 6.x, it was not possible to use a scheduled agent to send mail messages with HTML formatting. When you added HTML code to the body of a mail message, even setting the PassThruHTML NotesRichTextStyle property, Notes sent only the raw HTML code to the recipient without rendering it into HTML.

Answer

New methods and properties in the NotesMIMEEntity class, as well as the addition of the NotesStream class, make it possible to send HTML formatted mail directly from a scheduled agent in Notes/Domino 6.x and later. This functionality is useful for sending HTML newsletters or for responding to users who submit information into a mail-in database. You can create an agent that sends an HTML file stored on the local file system or that creates the HTML dynamically. The agents below demonstrate this functionality.
IMPORTANT NOTE: The following is a sample script, provided only to illustrate one way to approach this issue. In order for this example to perform as intended, the script must be laid out exactly as indicated below. IBM Support will not be able to customize this script for a customer's own configuration.


Using a local HTML file

The following agent sends an HTML file from the local file system (server's hard drive):

Dim s As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim body As NotesMIMEEntity
Dim header As NotesMIMEHeader
Dim stream As NotesStream
Set db = s.CurrentDatabase
Set stream = s.CreateStream
s.ConvertMIME = False ' Do not convert MIME to rich text
Set doc = db.CreateDocument
doc.Form = "Memo"
Set body = doc.CreateMIMEEntity
Set header = body.CreateHeader("Subject")
Call header.SetHeaderVal("HTML message") ' this is the subject
Set header = body.CreateHeader("SendTo")
Call header.SetHeaderVal("user@us.ibm.com")
'The file named below is located on the Domino server because the scheduled agent runs on the Domino server
Call stream.Open("c:\newsletter.html")
Call body.SetContentFromText(stream, "text/HTML;charset=UTF-8", ENC_IDENTITY_7BIT)
Call doc.Send(False)
s.ConvertMIME = True ' Restore conversion



Using dynamic HTML content
    The following agent builds an HTML file dynamically. It is also possible to add dynamic content into the HTML as demonstrated.

    Notice that the stream.WriteText method uses the pipe ( | ) string delimiter rather than quotation marks. This usage makes it easier to place quotation marks directly into the string. Also, the WriteText lines are separated for easier reading, but they could be concatenated together.

    The example code contains EOL_CRLF characters to make the message source properly formatted. This is important because many spam filters block improperly formatted MIME messages.

    Dim s As New NotesSession
    Dim db As NotesDatabase
    Dim doc As NotesDocument
    Dim body As NotesMIMEEntity
    Dim header As NotesMIMEHeader
    Dim stream As NotesStream
    Set db = s.CurrentDatabase
    Set stream = s.CreateStream
    s.ConvertMIME = False ' Do not convert MIME to rich text
    Set doc = db.CreateDocument
    doc.Form = "Memo"
    Set body = doc.CreateMIMEEntity
    Set header = body.CreateHeader("Subject")
    Call header.SetHeaderVal("HTML message")
    Set header = body.CreateHeader("To")
    Call header.SetHeaderVal("user@us.ibm.com")
    Call stream.writetext(||)
    Call stream.writetext(||)
    Call stream.writetext(||)
    Call stream.writetext(||)
    Call stream.writetext(||)
    Call stream.writetext(||)
    Call stream.writetext(|
    Hello World!
    |)
    user$ = s.CommonUserName 'if scheduled agent this returns the name of the server
    'Below uses the ampersand (&) to concatenate user$
    Call stream.writetext(|
    | & user$ &||)
    Call stream.writetext(||)
    Call stream.writetext(||)
    Call body.SetContentFromText(stream, "text/HTML;charset=UTF-8", ENC_IDENTITY_7BIT)
    Call doc.Send(False)
    s.ConvertMIME = True ' Restore conversion - very important



    Using a prompt for subject, name(s), and HTML file

    Sub Initialize
    Dim uiwk As New NotesUIWorkspace
    Dim s As New NotesSession
    Dim db As NotesDatabase
    Dim doc As NotesDocument
    Dim body As NotesMIMEEntity
    Dim header As NotesMIMEHeader
    Dim stream As NotesStream
    Dim vname As Variant
    Dim vfile As Variant
    Dim ssubject As String
    Dim snames As String
    Dim sfile As String

    ssubject = CStr(InputBox("Please enter a subject","Subject input dialog"))
    vname = uiwk.PickListStrings(PICKLIST_NAMES,True)
    vfile = uiwk.OpenFileDialog(False,"Please select the HTML file to import")
    ForAll names In vname
    Set db = s.CurrentDatabase
    Set stream = s.CreateStream
    s.ConvertMIME = False ' Do not convert MIME to rich text
    Set doc = db.CreateDocument
    doc.Form = "Memo"
    Set body = doc.CreateMIMEEntity
    Set header = body.CreateHeader("Subject")
    Call stream.Open(vfile(0))
    Call body.SetContentFromText(stream, "text/HTML;charset=UTF-8", ENC_IDENTITY_7BIT)
    Call header.SetHeaderVal(ssubject) ' this is the subject
    Set header = body.CreateHeader("SendTo") 'alternatively you can use "BlindCopyTo" instead of "SendTo"
    Call header.SetHeaderVal(names)
    Call doc.Send(False)
    s.ConvertMIME = True ' Restore conversion
    End ForAll
    End Sub

    sexta-feira, 6 de julho de 2012

    Botões / ações ( actions) para ajudar no desenvolvimento




    Caminho
    Base:= @Subset(@DbName;-1);
    Server:= @Name([CN];@Subset(@DbName;1));
    @Prompt([OkCancelEdit];"Caminho";"Caminho";Server + "\\" + Base)

    AtualizaDocSelecionado
    @Command([ToolsRefreshSelectedDocs])

    Bt Ninja
    $campo:=@Prompt([OkCancelList];"Super Mega Button";"Selecione o campo que será alterado:";"";@DocFields);
    $perg01:=@Text(@Prompt([YesNo];"Super Mega Button";"O valor do campo " + $campo + " é numérico?"));

    @If(
    $perg01="1";
    @Do(
    $valor_num:=@Prompt([OkCancelEdit];"Super Mega Button";"Informe o valor numérico do campo " + $campo +":";"");
    @SetField($campo;@TextToNumber($valor_num))
    );
    @Do(
    $perg02:=@Text(@Prompt([YesNo];"Super Mega Button";"O valor do campo " + $campo + " é data (formato: dd/mm/aaaa)?"));
    @If(
    $perg02="1";
    @Do(
    $valor_dia:=@Prompt([OkCancelEdit];"Super Mega Button";"Informe o valor do dia (2 dígitos) para o campo " + $campo +":";"");
    $valor_mes:=@Prompt([OkCancelEdit];"Super Mega Button";"Informe o valor do mês (2 dígitos) para o campo " + $campo +":";"");
    $valor_ano:= @Prompt([OkCancelEdit];"Super Mega Button";"Informe o valor do ano (4 dígitos) para o campo " + $campo +":";"");
    $valor_data:=@Date(@TextToNumber($valor_ano);@TextToNumber($valor_mes);@TextToNumber($valor_dia));
    @SetField($campo;$valor_data)
    );
    @Do(
    $perg03:=@Text(@Prompt([YesNo];"Super Mega Button";"O campo " + $campo + " é multi-valorado?"));
    @If(
    $perg03="1";
    @Do(
    $valor_multi:=@Prompt([OkCancelEdit];"Super Mega Button";"Informe o valor texto para incluir na lista do campo " + $campo +":";"");

    $lista:=@Implode(@GetField($campo);",");
    @SetField($campo;@Explode($lista+","+@Text($valor_multi);","))
    );
    @Do(
    $valor_texto:=@Prompt([OkCancelEdit];"Super Mega Button";"Informe o valor texto do campo " + $campo +":";"");
    @SetField($campo;@Text($valor_texto))
    ))))))


    Bt Ninja 2
    REM { v1.2 - if field exists, uses default value and default field type in @Prompt dialogs [Raphael Rodrigues, 2007-08-08]};

    unid:= @Text(@DocumentUniqueID);

    theField := @Prompt([OkCancelList]; "Change Field"; "Select Field"; ""; "":@DocFields);

    currValueTemp:= @If( @IsAvailable(theField); @GetDocField(unid; theField); "errorTemp" );
    currValue:= @If( @IsError(currValueTemp); "errorValue"; @If(@Elements(currValueTemp)>1;@Implode(@Text(currValueTemp);";");@Text(currValueTemp)) );
    theValue := @Prompt([OkCancelEdit]; "Change Field"; "New Value: use semicolon separator for lists."; currValue);

    currType:= @If( @IsNumber(currValueTemp); "Number"; @IsTime(currValueTemp); "Time"; "Text" );
    theType := @Prompt([OkCancelList]; "Change Field"; "Data Type"; currType; "Text" : "Time" : "Number" : "Text List" : "Number List" : "Time List");

    @If(

    theType = "Time";
    @SetField(theField; @TextToTime(theValue));

    theType = "Number";
    @SetField(thefield; @TextToNumber(theValue));

    theType = "Text List";
    @SetField(theField; @Trim(@Explode(theValue;";")));

    theType = "Number List";
    @SetField(theField; @TextToNumber(@Explode(@Trim(@ReplaceSubstring(theValue;" ";""));";")));

    theType = "Time List";
    @SetField(theField; @TextToTime(@Explode(theValue;";")));

    @SetField(theField; @Text(theValue))
    )