Jump to content

_EmailSend() Func. Ready to use.


ezzetabi
 Share

Recommended Posts

Untested with SMTP servers that need Authlogin. If someone can test I'd like to know if it is working. TESTED! YEAH!

;Better code below

Blat can be downloaded here:

http://prdownloads.sourceforge.net/blat/bl...te.zip?download

Main site:

http://www.blat.net

You need only the blat.exe file, copy it in the scrip folder. You need it if you use the uncompiled version. If you compile the script it will be FileInstall()ed so it is unneeded to keep it.

Edited by ezzetabi
Link to comment
Share on other sites

  • Replies 83
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

This script is great if you want to send few emails. If you need to send many it is maybe better Fileinstall() blat.exe in the begin of the script and delete it at the end.

So it is not unpacked and deleted everytime but only once.

If you want to do that remove, or comment, the lines 74 and 64 (considering the Func line as the first one)

and put them in the AutoItStart/Exit() sections.

Enjoy.

Edited by ezzetabi
Link to comment
Share on other sites

Because Blat is GNU, Bmail is not.

And, overall, AFAIK Bmail does not allow auth login.

Anyway, they are both command line mail sender.

Why care about one or the other?

Do you think I should improve this UDF in order to see Blat errors? :ph34r:

Edited by ezzetabi
Link to comment
Share on other sites

Because Blat is GNU, Bmail is not.

And, overall, AFAIK Bmail does not allow auth login.

Anyway, they are both command line mail sender.

Why care about one or the other?

Do you think I should improve this UDF in order to see Blat errors? :ph34r:

<{POST_SNAPBACK}>

don't :cry: ezzetabi, yes, please do improve it to show errors, I knew this would be useful someday, just didn't know how... now that the idea of sms'ing with it comes up... I came searching for your UDF... :(

"I'm not even supposed to be here today!" -Dante (Hicks)

Link to comment
Share on other sites

So, it's returning a 1 and @error's 0.

Why? :ph34r: I don't see in the script where it'd return a 1... The log isn't generating anything either.

I don't know weather the smtp requires auth or not...

so, yeah, please, if you're willing, include parsing for blat errors?

$r = _emailsend("server:25","email to","email from ","test","test","test.txt")
$error = @error
MsgBox(4096,'debug:' , '@error:' & $error);### Debug MSGBOX 
MsgBox(4096,'debug:' , '$r:' & $r);### Debug MSGBOX

PS, I tried this from inside the firewall with corporate server as smtp and from outside with my server as smtp, mine at least, shouldn't require auth...

"I'm not even supposed to be here today!" -Dante (Hicks)

Link to comment
Share on other sites

Finally I started working on this.... And I discovered why you had this problem emmanuel.

I tested this script in a Win98 machine and I forgot about the different way that Win98 and Win2000 manages " in @comspec! The stupiest error possible! :)

Sorry! Sorry! Sorry! Sorry! Sorry!

I'll correct and repost.

Link to comment
Share on other sites

Still it does not out put the errors, but IT WORKS in WinXP. This time I did not tested in W98... The pain of people that travel a lot and uses different computers... :)

;Better code below
Edited by ezzetabi
Link to comment
Share on other sites

Finally, finally! :);)

This time if Blat have an error a windows appear showing the log. (Even if the user didn't ask for one.)

This MsgBox have a 10 second timeout, so this Func won't stop automated scripts.

If you ask for a logfile the existing log (if there) will be appended, not erased. So it is good also using this Func inside loops.

Still, as I said before, there is no need to FileInstall() blat and delete it everytime if you need to use this Func many times, just move line 36 to AutoItStart section (remove the If/Then, of course) and line 95 to AutoItExit section.

Enjoy.

Func _EmailSend($SMTP, $SENDER, $DEST, $SUBJECT, $TEXT, $LOGFILE)
  ; This func is a email sender. You just need to keep a copy of blat.exe
  ; in the script folder that will be FileInstall()ed and used.
  ; This script has been tested with the lite version, but it should works with other too.
  ;
  ; $SMTP is smtp address, use a : to divide the port and @lf to divide Auth login and pass.
  ;   e.g. Server   : mail.alpha.org:1845
  ;        AuthLogin: I am me
  ;        AuthPass : Secret
  ;   will become
  ;   _EmailSend('mail.alpha.org:1845' & @lf & 'I am me' & @lf & 'Secret',...ect...)
  ;   Of course Auth parameters must be used togheter and they are not mandatory.
  ; $SENDER is the sender address.
  ; $DEST is the destination addresses list. (comma separated)
  ; $SUBJECT is the Subject line.
  ; $TEXT is the email main text. It may be a filename or the message itself.
  ;   - if you put directly the text use the @crlf as new line char. Be careful 
  ;     about the 4096 char limit of AutoIt lines.
  ;   -  if you use a file its content will become exatly the mail text, as binary.
  ; $LOGFILE is a filename where append the logfile. If omitted (left '') the log
  ;   won't be created.
   
  ; Returns
  ;  1 All should be OK.
  ; -1 A blat error. The log is shown in a MsgBox with a 10 second timeout.
  ; -2 Badly formatted $SMTP
  ; -3 No @ in the sender address
  ; -4 No @ in the destination address
  ; -5 No text
  ; -6 No blat.exe in the script dir.
   
   Local $SMTP, $SENDER, $DEST, $SUBJECT, $TEXT, $LOGFILE, $LOG, $H = -1, $C, $E
   Local $SMTPSERVER, $AUTHLOGIN = '', $AUTHPASS = ''
   
  ;Install blat.exe (lite version)
   If Not FileInstall('blat.exe', @TempDir & '\~blattmp.exe', 1) Then Return -6
   
  ;Check Smtp server
   If StringInStr($SMTP, @LF) Then
      $SMTP = StringSplit($SMTP, @LF)
      If Not $SMTP[0] = 3 Then Return -2
      $SMTPSERVER = $SMTP[1]
      $AUTHLOGIN = $SMTP[2]
      $AUTHPASS = $SMTP[3]
   Else
      $SMTPSERVER = $SMTP
   EndIf
   
  ;Check addresses
   If Not StringInStr($SENDER, '@') Then Return -3
   If Not StringInStr($DEST, '@') Then Return -4
   
  ;Check subject
   If $SUBJECT = '' Then
      $SUBJECT = ' -ss '
   Else
      $SUBJECT = ' -s "' & $SUBJECT & '" '
   EndIf
   
  ;Check Text
   Select
      Case $TEXT = ''
         Return -5
      Case Not FileExists($TEXT)
         FileDelete(@TempDir & '\~mailtext.tmp')
         FileWrite(@TempDir & '\~mailtext.tmp', $TEXT)
         $TEXT = (@TempDir & '\~mailtext.tmp')
      Case Else
        ; $Text file exists and will be used as mail text.
   EndSelect
   
   FileDelete(@TempDir & '\~maillog.txt')
  ;Sending!
   If IsArray($SMTP) Then;There is Authlogin
      $E = RunWait(@ComSpec & ' /c ""' & @TempDir & '\~blattmp.exe" "' & $TEXT & _
      '" -to ' & $DEST & ' -binary -server ' & $SMTPSERVER & ' -log ' & @TempDir & _
      '\~maillog.txt -timestamp -f ' & $SENDER & $SUBJECT & '-u ' & $AUTHLOGIN & _
      ' -pw ' & $AUTHPASS & '"', @TempDir, @SW_HIDE)
   Else                  ;There is NOT Authlogin
      $E = RunWait(@ComSpec & ' /c ""' & @TempDir & '\~blattmp.exe" "' & $TEXT & _
      '" -to ' & $DEST & ' -binary -server ' & $SMTPSERVER & ' -log ' & @TempDir & _
      '\~maillog.txt -timestamp -f ' & $SENDER & $SUBJECT & '"', @TempDir, @SW_HIDE)
   EndIf
   $LOG = FileRead(@TempDir & '\~maillog.txt', FileGetSize(@TempDir & '\~maillog.txt'))
   
   If $LOGFILE <> '' Then
      $H = FileOpen($LOGFILE, 1)
   EndIf
   If $H <> - 1 Then;$H is -1 both if the user didn't ask for a logfile or
  ;                                if it is impossible to write in it.
      FileWrite($H, $LOG)
      FileClose($H)
   EndIf
   
   FileDelete(@TempDir & '\~maillog.txt')
   FileDelete(@TempDir & '\~mailtext.tmp')
   FileDelete(@TempDir & '\~blattmp.exe') 
   
   If $E <> 0 Then
      MsgBox(16, 'Blat log. Errors.', $LOG, 10)
      Return -1
   EndIf
   
   Return 1
EndFunc  ;==>_EmailSend

:)

Edited by ezzetabi
Link to comment
Share on other sites

Here the modded version that install blat.exe and delete it only once:

Func _EmailSend($SMTP, $SENDER, $DEST, $SUBJECT, $TEXT, $LOGFILE)
  ; This func is a email sender. You just need to keep a copy of blat.exe
  ; in the script folder that will be FileInstall()ed and used.
  ; This script has been tested with the lite version, but it should works with other too.
  ;
  ; $SMTP is smtp address, use a : to divide the port and @lf to divide Auth login and pass.
  ;   e.g. Server   : mail.alpha.org:1845
  ;        AuthLogin: I am me
  ;        AuthPass : Secret
  ;   will become
  ;   _EmailSend('mail.alpha.org:1845' & @lf & 'I am me' & @lf & 'Secret',...ect...)
  ;   Of course Auth parameters must be used togheter and they are not mandatory.
  ; $SENDER is the sender address.
  ; $DEST is the destination addresses list. (comma separated)
  ; $SUBJECT is the Subject line.
  ; $TEXT is the email main text. It may be a filename or the message itself.
  ;   - if you put directly the text use the @crlf as new line char. Be careful 
  ;     about the 4096 char limit of AutoIt lines.
  ;   -  if you use a file its content will become exatly the mail text, as binary.
  ; $LOGFILE is a filename where append the logfile. If omitted (left '') the log
  ;   won't be created.
   
  ; Returns
  ;  1 All should be OK.
  ; -1 A blat error. The log is shown in a MsgBox with a 10 second timeout.
  ; -2 Badly formatted $SMTP
  ; -3 No @ in the sender address
  ; -4 No @ in the destination address
  ; -5 No text
  ; -6 No installed blat.exe
   
   Local $SMTP, $SENDER, $DEST, $SUBJECT, $TEXT, $LOGFILE, $LOG, $H = -1, $C, $E
   Local $SMTPSERVER, $AUTHLOGIN = '', $AUTHPASS = ''
   
   If Not FileExists(@TempDir & '\~blattmp.exe') Then Return -6
   
  ;Check Smtp server
   If StringInStr($SMTP, @LF) Then
      $SMTP = StringSplit($SMTP, @LF)
      If Not $SMTP[0] = 3 Then Return -2
      $SMTPSERVER = $SMTP[1]
      $AUTHLOGIN = $SMTP[2]
      $AUTHPASS = $SMTP[3]
   Else
      $SMTPSERVER = $SMTP
   EndIf
   
  ;Check addresses
   If Not StringInStr($SENDER, '@') Then Return -3
   If Not StringInStr($DEST, '@') Then Return -4
   
  ;Check subject
   If $SUBJECT = '' Then
      $SUBJECT = ' -ss '
   Else
      $SUBJECT = ' -s "' & $SUBJECT & '" '
   EndIf
   
  ;Check Text
   Select
      Case $TEXT = ''
         Return -5
      Case Not FileExists($TEXT)
         FileDelete(@TempDir & '\~mailtext.tmp')
         FileWrite(@TempDir & '\~mailtext.tmp', $TEXT)
         $TEXT = (@TempDir & '\~mailtext.tmp')
      Case Else
        ; $Text file exists and will be used as mail text.
   EndSelect
   
   FileDelete(@TempDir & '\~maillog.txt')
  ;Sending!
   If IsArray($SMTP) Then;There is Authlogin
      $E = RunWait(@ComSpec & ' /c ""' & @TempDir & '\~blattmp.exe" "' & $TEXT & _
            '" -to ' & $DEST & ' -binary -server ' & $SMTPSERVER & ' -log ' & @TempDir & _
            '\~maillog.txt -timestamp -f ' & $SENDER & $SUBJECT & '-u ' & $AUTHLOGIN & _
            ' -pw ' & $AUTHPASS & '"', @TempDir, @SW_HIDE)
   Else                  ;There is NOT Authlogin
      $E = RunWait(@ComSpec & ' /c ""' & @TempDir & '\~blattmp.exe" "' & $TEXT & _
            '" -to ' & $DEST & ' -binary -server ' & $SMTPSERVER & ' -log ' & @TempDir & _
            '\~maillog.txt -timestamp -f ' & $SENDER & $SUBJECT & '"', @TempDir, @SW_HIDE)
   EndIf
   $LOG = FileRead(@TempDir & '\~maillog.txt', FileGetSize(@TempDir & '\~maillog.txt'))
   
   If $LOGFILE <> '' Then
      $H = FileOpen($LOGFILE, 1)
   EndIf
   If $H <> - 1 Then;$H is -1 both if the user didn't ask for a logfile or
    ;                                if it is impossible to write in it.
      FileWrite($H, $LOG)
      FileClose($H)
   EndIf
   
   FileDelete(@TempDir & '\~maillog.txt')
   FileDelete(@TempDir & '\~mailtext.tmp')
   
   If $E <> 0 Then
      MsgBox(16, 'Blat log. Errors.', $LOG, 10)
      Return -1
   EndIf
   
   Return 1
EndFunc  ;==>_EmailSend

Func OnAutoItStart()
  ;Install blat.exe (lite version)
   FileInstall('blat.exe', @TempDir & '\~blattmp.exe', 1)
EndFunc  ;==>OnAutoItStart

Func OnAutoItExit()
  ;Delete blat.exe
   FileDelete(@TempDir & '\~blattmp.exe')
EndFunc  ;==>OnAutoItExit

:):)

Edited by ezzetabi
Link to comment
Share on other sites

  • 2 weeks later...

windows 98 story:

I got BLAT full version created a C:\BLAT222 folder

copied your script to this directory and just added a call to the function at

the beginning of the script

$x=_EmailSend('misite.com' & @lf & 'normeus' & @lf & 'UhPassWord' ,'normeus@misite.com','youyu@yoursite.com',"how ","how you do",'c:/log.txt')

;and like every new user should do. I added a MsgBox for debugging :)

MsgBox(4096,'debug:' , '$x:' & $x) ;### Debug MSGBOX

@ComSpec didnt work; I guess windows 98 quotes differently!!!

this line will work on 98:

$E = RunWait(@ComSpec & ' /c "' & @TempDir & ....... <--- notice single double quotes after /c -------------------^

thanks for this nice Function :)

also my smpt server sends my pc name and ip address so there will be no spam sending from this program kids

Link to comment
Share on other sites

Corrected edition!

Func _EmailSend($SMTP, $SENDER, $DEST, $SUBJECT, $TEXT, $LOGFILE)
; This func is a email sender. You just need to keep a copy of blat.exe
; in the script folder that will be FileInstall()ed and used.
; This script has been tested with the lite version, but it should works with other too.
;
; $SMTP is smtp address, use a : to divide the port and @lf to divide Auth login and pass.
;   e.g. Server   : mail.alpha.org:1845
;        AuthLogin: I am me
;        AuthPass : Secret
;   will become
;   _EmailSend('mail.alpha.org:1845' & @lf & 'I am me' & @lf & 'Secret',...ect...)
;   Of course Auth parameters must be used togheter and they are not mandatory.
; $SENDER is the sender address.
; $DEST is the destination addresses list. (comma separated)
; $SUBJECT is the Subject line.
; $TEXT is the email main text. It may be a filename or the message itself.
;   - if you put directly the text use the @crlf as new line char. Be careful 
;     about the 4096 char limit of AutoIt lines.
;   -  if you use a file its content will become exatly the mail text, as binary.
; $LOGFILE is a filename where append the logfile. If omitted (left '') the log
;   won't be created.
  
; Returns
;  1 All should be OK.
; -1 A blat error. The log is shown in a MsgBox with a 10 second timeout.
; -2 Badly formatted $SMTP
; -3 No @ in the sender address
; -4 No @ in the destination address
; -5 No text
; -6 No installed blat.exe
  
  Local $SMTP, $SENDER, $DEST, $SUBJECT, $TEXT, $LOGFILE, $LOG, $H = -1, $C, $E
  Local $SMTPSERVER, $AUTHLOGIN = '', $AUTHPASS = '', $QUOTE = ''
  
  If Not FileExists(@TempDir & '\~blattmp.exe') Then Return -6
  
  If @OSTYPE = 'WIN32_NT' Then $QUOTE = '"'
  
;Check Smtp server
  If StringInStr($SMTP, @LF) Then
     $SMTP = StringSplit($SMTP, @LF)
     If Not $SMTP[0] = 3 Then Return -2
     $SMTPSERVER = $SMTP[1]
     $AUTHLOGIN = $SMTP[2]
     $AUTHPASS = $SMTP[3]
  Else
     $SMTPSERVER = $SMTP
  EndIf
  
;Check addresses
  If Not StringInStr($SENDER, '@') Then Return -3
  If Not StringInStr($DEST, '@') Then Return -4
  
;Check subject
  If $SUBJECT = '' Then
     $SUBJECT = ' -ss '
  Else
     $SUBJECT = ' -s "' & $SUBJECT & '" '
  EndIf
  
;Check Text
  Select
     Case $TEXT = ''
        Return -5
     Case Not FileExists($TEXT)
        FileDelete(@TempDir & '\~mailtext.tmp')
        FileWrite(@TempDir & '\~mailtext.tmp', $TEXT)
        $TEXT = (@TempDir & '\~mailtext.tmp')
     Case Else
      ; $Text file exists and will be used as mail text.
  EndSelect
  
  FileDelete(@TempDir & '\~maillog.txt')
;Sending!
  If IsArray($SMTP) Then;There is Authlogin
     $E = RunWait(@ComSpec & ' /c ' & $quote & '"' & @TempDir & '\~blattmp.exe" "' & $TEXT & _
           '" -to ' & $DEST & ' -binary -server ' & $SMTPSERVER & ' -log ' & @TempDir & _
           '\~maillog.txt -timestamp -f ' & $SENDER & $SUBJECT & '-u ' & $AUTHLOGIN & _
           ' -pw ' & $AUTHPASS & '"', @TempDir, @SW_HIDE)
  Else                  ;There is NOT Authlogin
     $E = RunWait(@ComSpec & ' /c ' & $quote & '"' & @TempDir & '\~blattmp.exe" "' & $TEXT & _
           '" -to ' & $DEST & ' -binary -server ' & $SMTPSERVER & ' -log ' & @TempDir & _
           '\~maillog.txt -timestamp -f ' & $SENDER & $SUBJECT & '"', @TempDir, @SW_HIDE)
  EndIf
  $LOG = FileRead(@TempDir & '\~maillog.txt', FileGetSize(@TempDir & '\~maillog.txt'))
  
  If $LOGFILE <> '' Then
     $H = FileOpen($LOGFILE, 1)
  EndIf
  If $H <> - 1 Then;$H is -1 both if the user didn't ask for a logfile or
  ;                                if it is impossible to write in it.
     FileWrite($H, $LOG)
     FileClose($H)
  EndIf
  
  FileDelete(@TempDir & '\~maillog.txt')
  FileDelete(@TempDir & '\~mailtext.tmp')
  
  If $E <> 0 Then
     MsgBox(16, 'Blat log. Errors.', $LOG, 10)
     Return -1
  EndIf
  
  Return 1
EndFunc ;==>_EmailSend

Func OnAutoItStart()
;Install blat.exe (lite version)
  FileInstall('blat.exe', @TempDir & '\~blattmp.exe', 1)
EndFunc ;==>OnAutoItStart

Func OnAutoItExit()
;Delete blat.exe
  FileDelete(@TempDir & '\~blattmp.exe')
EndFunc ;==>OnAutoItExit

:)

Link to comment
Share on other sites

  • 2 weeks later...

In the following code:

If $LOGFILE <> '' Then
    $H = FileOpen($LOGFILE, 1)
 EndIf
 If $H <> - 1 Then;$H is -1 both if the user didn't ask for a logfile or
;                                if it is impossible to write in it.
    FileWrite($H, $LOG)
    FileClose($H)
 EndIf

Will - 1 (<dash><space><one>) be interpreted as -1 (<dash><one> or <negative one>)?

*** Matt @ MPCS

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...