Using External Scripting Languages

G-Lock EasyMail supports very powerful scripting languages: Microsoft Visual Basic, JavaScript, Perl, and PHP, all driven by components within the operating system (for VBasic and Javascript) or in a separate installation (for Perl and PHP).

JavaScript and Visual Basic are included as components with Internet Explorer, which should be installed already on your computer.

PerlScript requires download and installation of ActivePerl

To use the PHPScript, you must download and install PHP 5.2.0 installer 

Installing PHPScript 

After having downloaded the PHP installer, double click the mouse on the php-5.2.0-win32-installer.msi file to begin the installation.

During the installation process select the items to install: Activescript (required).

You can also select other features if you want. Click Next.

After having all the files installed, you must register the DLL on your system. To achieve this:

  • Click on the Start menu and open a Command Prompt window.
  • Type regsvr32 C:\PHP\ext\php5activescript.dll.
  • Click OK.

Now you can use the PHPScript in G-Lock EasyMail.

Writing Script in G-Lock EasyMail 

To write a script in G-Lock EasyMail:

  • Click on the Tools menu and select Script Repository.

 

  • Click Add button in the Script Repository.
  • On the Microsoft Windows Script screen enter a script name. It can be any name describing what the script is doing.
  • Select the script language: VBScript, JScript, PerlScript, or PHPScript.
  • Select the mode to execute the script:

Before the start of email campaign – the script will be executed before the email campaign starts

Before sending email to every recipient – the script will be executed before every email is sent

After sending email to every recipient – the script will be executed after every email is sent

After the email campaign is complete – the script will be executed after the email campaign is complete

  • Write the script and click on the Test button to test the script.

Script Samples

Sample 1

If the First_Name field for the recipient has no value in the Address Book, the script
inserts “Friend” instead of the first name.

Script Name: replace empty first name
Language: VBScript
Execute script: Before sending email to every recipient
 

function main()
   if Recipient.FieldByName(“First_Name”) = “” then
Recipient.FieldByName(“First_Name”) = “Friend”
   end if

‘msgbox Recipient.FieldByName – remove the comment to test the script

end function

_____________________________________________________________________

Sample 2

If the recipient’s first name begins from the lower case letter in the Address
Book, the script changes it to the upper case letter.

Script Name: write first name from the upper case letter
Language: VBScript
Execute script: Before sending email to every recipient

function main

‘ Recipient.AddField “First_Name”, “alex” – remove the comment to test the script

Temp = Recipient.FieldByName(“First_Name”)
Temp = UCase(Mid(Temp,1,1))+ Mid(temp,2, Len(Temp))
Recipient.FieldByName(“First_Name”) = Temp

‘ msgbox Recipient.FieldByName(“First_Name”) – remove the comment to test the script

end function

_____________________________________________________________________

Sample 3

The script stops the sending campaign after 100 emails are processed.

Script Name: stop sending after 100 emails
Language: VBScript
Execute script: Before sending email to every recipient

function main
   if CInt(Message.ProcessedInSession) >= 100 then
Message.StopSending = 1

   end if

end function

____________________________________________________________

Sample 4

The script writes sent and not delivered emails to different files. The email addresses, to which the message was successfully sent are saved to this file c:\Sent Items.txt. The email addresses, to which the delivery failed are saved to this file c:\Not Delivered.txt. 

Script Name: save sent and not delivered emails to the files
Language: VBScript
Execute script: After sending email to every recipient

function main
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f

    if Recipient.Status = 0 then
FileName = “c:\Sent Items.txt”
   else
FileName = “c:\Not Delivered.txt”

    end if

Set fso = CreateObject(“Scripting.FileSystemObject”)
Set f = fso.OpenTextFile(FileName, ForAppending, True)
f.WriteLine Recipient.FieldByName(“Email”) & “, ” & Recipient.ServerResponse

‘ or f.WriteLine Recipient.FieldByName(“Email”) if you don’t want to save the mail server response

f.Close

end function

____________________________________________________________

Sample 5

The script saves sent and not delivered email addresses to separate files: Sent
Items.txt and Not Delivered.txt. Plus, not delivered emails are separated into different files based on reply codes: 452 4.2.1, 451, 550 5.7.1, and 550 5.1.1.

Script Name: separate not delivered emails based on reply codes
Language: VBScript
Execute script: After sending email to every recipient

function main
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f

if Recipient.Status = 0 then
FileName = “c:\Sent Items.txt”
else
FileName = “c:\Not Delivered.txt”
end if

if InStr(Recipient.ServerResponse, “452”) and InStr(Recipient.ServerResponse,
“4.2.1”) <> 0 then
FileName = “c:\452 4.2.1.txt”
end if
if InStr(Recipient.ServerResponse, “451”) <> 0 then
FileName = “c:\451.txt”
end if
if InStr(Recipient.ServerResponse, “550”) <> 0 and InStr(Recipient.ServerResponse, “5.7.1”) <> 0 then
FileName = “c:\550 5.7.1.txt”
end if
if InStr(Recipient.ServerResponse, “550”) <>0 and InStr(Recipient.ServerResponse, “5.1.1”) <> 0 then
FileName = “c:\550 5.1.1.txt”
end if

Set fso = CreateObject(“Scripting.FileSystemObject”)
Set f = fso.OpenTextFile(FileName, ForAppending, True)
f.WriteLine Recipient.FieldByName(“Email”) & “, ” & Recipient.ServerResponse
f.Close

end function

_____________________________________________________________________

Sample 6

The script inserts the current date into the email message sent to each recipient.

Script Name: insert current date
Language: VBScript
Execute script: Before sending email to every recipient

function main

Recipient.FieldByName(“TDate”) = date()

end function

Note: to get the script working, you need to add a new TDate field to the group in G-Lock EasyMail address book to which you will send the email. Set the type for the TDate field as Date/Time. Then use the Merge menu in the Edit Message window to merge the TDate field into the email. During the sending process the %%TDate%% variable will be replaced by the current date.

_____________________________________________________________________

Sample 7

The script updates the SQL database, i.e. flags the records as sent or not sent after the email is sent out. The script writes the result: 0, 1, or 2 into the database.  0 – Message accepted for delivery, 1 – message not sent, 2 – unknown user.

Script Name: update database
Language: VBScript
Execute script: After sending email to every recipient

function main
Dim oConnection, oCommand
ConnectionString = Message.ConnectionString
TableName = Message.TableName
UpdateSQL = “UPDATE ” & TableName & ” SET EmailStatus = ” & Recipient.Status &_
” WHERE Email ='” & Recipient.FieldByName(“Email”) & “‘”
Set oConnection = CreateObject(“ADODB.Connection”)
oConnection.Open ConnectionString
Set oCommand = CreateObject(“ADODB.Command”)
oCommand.ActiveConnection = oConnection
oCommand.CommandText = UpdateSQL
oCommand.execute
Set oConnection = Nothing
Set oCommand = Nothing
end function

Notes:

EmailStatus is the name of the field in your database to which you will save the result (0, 1, 2 ). So, write the correct field name instead of “EmailStatus” in the script. This field must be numeric.

Email is the name of the field in your database that stores the email addresses. If this field is called differently in your database, for example, Email_address, write the correct field name instead of “Email” in the script.

_____________________________________________________________________

Sample 8

The script pulls the email content from a database – Test.mdb file which is in the root directory on the D drive. There are 2 fields in the database: HtmlPart (type – “memo”) and PlainPart (type – “memo”). And there is only one record with the HTML and plain text parts in the database.

Script Name: take email content from database
Language: VBScript
Execute script: Before the start of email campaign

function main
Dim oConnection, oRecordSet
ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Test.mdb”

Set oConnection = CreateObject(“ADODB.Connection”)
set oRecordSet = CreateObject(“ADODB.Recordset”)

oConnection.open ConnectionString
oRecordSet.ActiveConnection = oConnection
oRecordSet.open “SELECT * FROM Content”, oConnection

Message.HTMLPART = oRecordSet.Fields(“HtmlPart”).Value
Message.PlainPART = oRecordSet.Fields(“PlainPart”).Value

Set oConnection = Nothing
Set oRecordSet = Nothing
end function