Command Line Mail Sender – Drangua

0
105

Introduction

The reason why I made this application is that I needed to send emails to my GMail account which uses SSL. Before I tried the good old Blat, but now most of the SMTP servers require SSL and using the STunnel was a lot of work for a little job.

You can download the executable file called drangua.exe which i converted into exe. Below i explain how it works and also show an example how to create an email sending script yourself, but not fully featured as my program.

Using the Code

First of all, as the application is run with parameters, I show how to retrieve those arguments that were applied.


Set Args = WScript.Arguments

Now we can simply call the Args variable. To get the amount of arguments, we call the Count() method.

Dim amount
amount = Args.Count()

Since the WScript.Arguments is an array, we can call them by indexes.

Dim firstArgument
firstArgument = Args(0)

Example

Create an array of rules.

params = Array("#subject*", "#body*", "#from*", "#to*" , "#smtp*", "#username*", "#password*")

Check for parameters that were applied from outside into the program.

If (Args.Count > 0) Then
 
 
 Dim cSyntax
 cSyntax = Syntax(Args)
 
 Call SEND(cSyntax) 
Else
 
 Call DisposeApp()
End If

This function corrects the syntax of arguments. It splits the values from the rules and also automatically reorders the parameters so that different parameter order is supported. So basically it converts the array of arguments passed in into a string and then clarifies the values.

Function Syntax(ByVal argv)
 Dim result(6) 
 
 
 
 Dim strArgs 
 For Each arg In argv
 strArgs = strArgs & arg & " "
 Next

 
 For Each synt In Split(strArgs, "#")
 
 synt = "#" & synt
 
 For i = 0 To UBound(params)
 Dim arg
 arg = params(i)
 
 If (Contains(synt, arg)) Then 
 
 
 Dim pos
 pos = IndexOf(synt, arg)
 
 Dim value 
 value = SubString(synt, pos + Len(arg), Len(synt) - (pos + Len(arg)))
 Select Case arg
 Case params(0)
 
 Case params(1)
 
 Case Else
 
 value = Replace(value, " ", "")
 End Select
 
 result(i) = value
 End If
 Next
 Next
 
 Syntax = result 
End Function

How are Emails Sent using VBScript?

To create an email sending function, we first have to create an instance of the CDO.Message using the CreateObject structure.

 

Now we can simply refer to the variable myEMail to call or set its underlying properties and methods.

Function SEND(ByVal argv)

On Error Resume Next
 WScript.Echo "Sending ..."
 
 Set MyEmail=CreateObject("CDO.Message")

 
 Dim o_subject, o_body, o_from, o_to, o_smtp, o_username, o_password 

 o_subject=argv(0) 
 o_body=argv(1) 
 o_from=argv(2) 
 o_to=argv(3) 
 Dim o_server, o_port
 o_server=Split(argv(4), ":")(0) 
 o_port=Split(argv(4), ":")(1) 
 o_username=argv(5) 
 o_password=argv(6) 

 MyEmail.Subject=o_subject
 MyEmail.TextBody=o_body
 MyEmail.From = o_from
 
 MyEmail.To = o_to
 
 
 
 

 
 MyEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")="2" 
 
 MyEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")=o_server
 
 MyEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=o_port
 
 MyEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl")="True" 
 
 
 MyEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")="1" 
 
 MyEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername")=o_username
 
 MyEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword")=o_password

 
 

 
 MyEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

 MyEmail.Configuration.Fields.Update()
 MyEmail.Send()

 
 Set MyEmail=Nothing 
If (Err.Number <> 0) Then
 WScript.Echo "Error while sending Email."
Else
 WScript.Echo "Email has been sent."
End If
On Error GoTo 0

 Call DisposeApp()
End Function

Sub DisposeApp()
 Set Args = Nothing
 WScript.Quit()
End Sub






Function IndexOf(ByVal str_buff, ByVal keyWord)
 Dim result, found
 result = -1
 found = False
 
 If (Not keyWord = "") And (Not str_buff = "") Then
 If(Contains(str_buff, keyWord)) Then
 Dim incr : incr = 0
 Do
 If (Len(str_buff) < (incr + Len(keyWord))) Then Exit Do
 If (LCase(keyWord) = LCase(SubString(str_buff, incr, Len(keyWord)))) Then
 found = True
 result = incr
 Exit Do
 End If
 incr = incr + 1
 Loop While (found = False)
 End If
 End If 
 IndexOf = result
End Function
Function SubString(ByVal str_buff, ByVal position, ByVal length)
 If (position = 0) Then
 position = 1
 Else
 position = position + 1
 End If
 SubString = Mid(str_buff, position, length)
End Function

Function Contains(ByVal str_buff, ByVal keyWord)
 Dim result : result = False
 If (InStr(1, str_buff, keyWord, 1) > 0) Then
 result = True
 End If
 Contains = result
End Function

Save the above code into a VBScript file, say MyEmail.vbs

Create a Text Document in the same folder and rename it i.e. sendmail.bat

Open the file and write the addresses and credentials down;

MyEmail.vbs #subject*Hi! #body*What's up? #from*me@something.com #to*someone@something.com #smtp*smtp.server.com:25 #username*me@something.com #password*?????

Save the file and run it. MyEmail.vbs can be accessed from any programing language not just from a script.

Drangua

Drangua program is a full blown email sender. It can either be called directly applying parameters or use the -config parameter to refer to the configuration file. A file that is located in the same directory, it is called drangua.xml. You can write the information there, so the program can retrieve the parameters from it. To create a sample configuration file, call drangua -createConfig.

Note that if some fields are left unspecified, the application will either ignore them or apply default values. SSL is used by default. The default SMTP method is using Port aka 2. The default Authentication method is Basic aka 1.

Drangua accepts parameters of different order. It supports Company Name aka the sender's nickname, multiple recipients separated by comma "," also multiple CC and BCC addresses and multiple attachment files.

Usage

Drangua currently supports these parameters;

  • -help displays instructions
  • -config to use the configuration file
  • -createConfig to create a sample configuration file
  • -loud when this option is used (combined with others) it displays notifications, echos

The -loud option as opposed to the above options can be combined with other options but not with the above ones, since -help, -config and -createConfig can only be called alone. The Mail parameters are a little bit different, they have an extra star sign for prevention.


-subject* -body* -attachment* -company* -from* -to* -cc* -bcc* -smtp* -identify* -authenticate* -ssl* -username* -password*

Multiple values are to be separated by comma (,). The attachment* parameter can either accept full paths or just a file in the directory of the program. Below are examples how we would send an email from a CMD or another application.

drangua -loud -smtp*smtp.server.com:465 -subject*Hi! -from*me@something.com -to*someone@something.com -username*me@something.com -password*12345678


drangua -loud -smtp*mail.server.com:25 -subject*Hi! -body*What's up? -attachment*C:\folder name with spaces\example.txt -company*Me -from*me@something.com -to*someone@something.com -username*me@something.com -password*12345678 -identify*2 -authenticate*1 -ssl*True


drangua -config

Note

  • Drangua is freeshareware.
  • Drangua won't work if the name is changed.

History

  • 14th June, 2017: Published first
  • 14th June, 2017: Published version 1.6.1.4 (contains 299,008 bytes, 292 KB) and Source Code

LEAVE A REPLY