Jump to content
Sign in to follow this  

SMTP Email Sending With Blat

Recommended Posts


There a few options to send email to SSL enabled accounts such as Gmail this topic simply provides a quick how to with Blat and Stunnel.

First, the script to execute blat. Note that it can take two command line parameters so that the subject and body of the email are not hard coded, thus making it a bit more portable and reusable.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#Obfuscator_Parameters=/sf /sv /Convert_Vars=1 /Convert_Funcs=1 /Convert_Strings=1 /Convert_Numerics=1 /SCI=1
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: (beta)
 Author:         Sn3akyP3t3

 Script Function:
    Send text only email from Gmail SMTP with SSL authentication.

  Blat and Stunnel executables.  Stunnel configuration file must be configured properly.

#ce ----------------------------------------------------------------------------
; Set these values
; Note that I don't recommend hardcoding username and password into the script.  If saved as clear text in the script then it is up to you to safely guard them.
; If anyone has suggestions on how to properly do this securely then please contact me via the Autoit Forums.
$sPassword = "" ; smtp authentication pwd
$sUsername = "@gmail.com" ; full gmail email address required
;smtp server and port given to stunnel config
$sSmptServerAddress = ""
$sSmptPort = "1099"
$sBlatDirectory = "Q:\My Dropbox\Utilities\blat262\full"
$sStunnelDirectory = "Q:\My Dropbox\Utilities\blat262\stunnel-4.36"
$sSubject = "Subject is missing from command line"
$sBody = "Body is missing from the command line"

If $CmdLine[0] == 2 Then
    $sSubject = $CmdLine[1]
    $sBody = $CmdLine[2]

EmailUsingBlat($sBlatDirectory, $sUsername, $sPassword, $sSmptServerAddress, $sSmptPort, "toEmailAddress12345678@reallyDoesNotExist.com", "fromEmailAddress12345678@reallyDoesNotExist.com", $sSubject, $sBody)

Func startStunnel($sStunnelDir)
    ; Usage: stunnel [ [-install | -uninstall | -start | -stop] [-quiet] [<filename>] ] | -help | -version | -sockets
    Run($sStunnelDir & "\stunnel.exe")

Func killStunnel()
    If ProcessExists("stunnel.exe") Then ProcessClose("stunnel.exe")

Func EmailUsingBlat($sBlatDir, $sUn, $sPw, $sSmtpSvr, $sSmtpPt, $sTo, $sFrm, $sSubj, $sBdy)
    ; If you are trying to debug then I suggest temporary enabling of logging add the next line to the last of the parameters
    ;  & '" -log "' & @ScriptDir & '\blat.log"'
    ShellExecuteWait($sBlatDir & "\blat.exe", "-u " & $sUn & " -pw " & $sPw & " -serverSMTP " & $sSmtpSvr & " -portSMTP " & $sSmtpPt    & " -to " & $sTo & " -f " & $sFrm & ' -subject "' & $sSubj & '" -body "' & $sBdy, $sBlatDirectory, "", @SW_HIDE)

Second, Be sure to modify the Stunnel config file, stunnel.conf, to look something like this. Enable logging if you are having trouble.

; Sample stunnel configuration file by Michal Trojnara 2002-2006
; Some options used here may not be adequate for your particular configuration

; Certificate/key is needed in server mode and optional in client mode
; The default certificate is provided only for testing and should not
; be used in a production environment
cert = stunnel.pem
;key = stunnel.pem

; Some performance tunings
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1

; Workaround for Eudora bug

; Authentication stuff
;verify = 2
; Don't forget to c_rehash CApath
;CApath = certs
; It's often easier to use CAfile
;CAfile = certs.pem
; Don't forget to c_rehash CRLpath
;CRLpath = crls
; Alternatively you can use CRLfile
;CRLfile = crls.pem

; Some debugging stuff useful for troubleshooting
;debug = 7
;output = stunnel.log

; Use it for client mode
client = yes

; Service-level configuration

[SMTP Gmail]
accept =
connect = smtp.googlemail.com:465

;accept  = 995
;connect = 110

;accept  = 993
;connect = 143

;accept  = 465
;connect = 25

;accept  = 443
;connect = 80
;TIMEOUTclose = 0

; vim:ft=dosini

Third, Make a new certificate for Stunnel since the one provided is useless. If you have questions about how to create a certificate then see the old deprecated documentation. I recommend openssh on linux or windows to generate a new key. Use this command to do so:

sudo openssl req -new -x509 -days 365 -nodes -out stunnel.pem -keyout stunnel.pem

Last, call the SendBlatEmail script above from other scripts like so.

ShellExecute(@DesktopDir & '\SendBlatEmail.exe', '"This would be the subject" "This then is the body"')



Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this