Sign in to follow this  
Followers 0
Sn3akyP3t3

SMTP Email Sending With Blat

1 post in this topic

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.

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Run_Obfuscator=y
#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: 3.3.5.6 (beta)
 Author:         Sn3akyP3t3

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

 Requirements:
  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 = "127.0.0.1"
$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]
EndIf

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

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

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

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)
EndFunc

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
;options = DONT_INSERT_EMPTY_FRAGMENTS

; 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 = 127.0.0.1:1099
connect = smtp.googlemail.com:465

;[pop3s]
;accept  = 995
;connect = 110

;[imaps]
;accept  = 993
;connect = 143

;[ssmtp]
;accept  = 465
;connect = 25

;[https]
;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"')

SendBlatEmail.au3

stunnelConfig.au3

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  
Followers 0