Jump to content

My first script


Recommended Posts

Hello, I'm new to the forum and to the language itself but It has proven quite usefull for a mail script I had to code yesterday. I have everything done and when you see or compile the source code you'll see what's it supposed to do. The tool sends a single mail to a previously made group of mail adresses. The tool also enables the user to create groups easily. But my problem is in the string splitting. I don't really understand how to split a string from a edit box in GUI into "strings per line". I easily bypassed this by splitting them with a semicolon ";". But now the tool is finished or nearly to it's finish state, i'd like this detail to be fixed. Thanks in advance.

NOTE: this is my first autoIT project so some amateur errors are probably included.

Opt("TrayIconHide",1)
Opt("OnExitFunc","OnAutoItExit")

global $send_mail
global $i
global $sender_server
global $sender_name
global $sender_mail
global $sender_subject
global $sender_msg
global $send_mail_number
global $group_max
global $port
global $nMsg
global $SmtpServer
global $FromName 
global $FromAddress
global $ToAddress
global $Subject
global $Body 
global $AttachFiles
global $CcAddress
global $BccAddress
global $Importance
global $Username
global $Password
global $IPPort
global $ssl
global $list
global $name

$group_max = iniread("config\\groups\\index.ini","INDEX","NUMBER", 0)
global $group_array[$group_max+1]
global $sender_attachments = ""

;
;##################################
; Include
;##################################
#Include<file.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

;

;### First we read the information of the sender from the sender.ini file ###
_ReadSenderInfo()
_ReadMailGroups()
;###


#Region ### START Koda GUI section ### Form=
local $form1
local $label1
local $button1
local $button2
local $label3
local $label4
local $label2
local $label5
local $label6
local $button3
local $label6
local $groups

$Form1 = GUICreate("Mail sender ver. 1.25", 371, 308, 192, 124)
$Label1 = GUICtrlCreateLabel("Script written by Adnan Elezovic, adnan_elezovic@hotmail.com", 0, 288, 304, 17)
$Button1 = GUICtrlCreateButton("Send mails", 32, 192, 241, 25)
$Groups = GUICtrlCreateCombo("Groups", 136, 120, 121, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
$Label6 = GUICtrlCreateLabel("Sending to group:", 32, 120, 88, 17)
$Button3 = GUICtrlCreateButton("New Group...", 264, 120, 97, 25)
local $Button4 = GUICtrlCreateButton( "Change mail text", 32, 152, 241, 25)
$Label3 = GUICtrlCreateLabel("Sender:  " & $sender_mail, 32, 24)
$Label2 = GUICtrlCreateLabel("Sender name:  " & $sender_name, 32, 48)
$Label5 = GUICtrlCreateLabel("Message subject:  " & $sender_subject, 32, 72)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
for $i = 1 to $group_max step 1
    GUICtrlSetData( $groups, $group_array[$i])
Next
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Button1
            ExitLoop
            
        Case $Button3
            _New_mail_group()
        case $button4
            _change_msg()
    EndSwitch
WEnd


#Region ### START Koda GUI section ### Form=
local $Form2
local $ButtonOk
local $ButtonCancel
local $input_password
global $sender_password

$Form2 = GUICreate("Password Dialog", 260, 115, -1, -1)
$input_password = GUICtrlCreateInput("", 8, 32, 233, 21,BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
$ButtonOk = GUICtrlCreateButton("&OK", 86, 64, 75, 25, $BS_NOTIFY)
$ButtonCancel = GUICtrlCreateButton("&Cancel", 167, 64, 75, 25, $BS_NOTIFY)
GUICtrlCreateLabel("Please enter your mail password", 8, 12, 155, 17, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ButtonCancel
            Exit
        Case $ButtonOk
            ExitLoop

    EndSwitch
WEnd


$sender_password = GUICtrlRead($input_password,1)
global $group_selected = GUICtrlRead($groups)
GUIDelete($form1)
GUIDelete($form2)

$i = 1
$send_mail_number = iniread( "config\\groups\\" & $group_selected & ".ini", "MAILS", "NUMBER", "1")
msgbox(0,"dbg","Number of mails : " & $send_mail_number)

while $i <= $send_mail_number
;### Now we read the mail list to wich the mail is going to be sent
_ReadMails()
;###
;##################################
; Variables
;##################################
$SmtpServer = $sender_server ; address for the smtp-server to use - REQUIRED
$FromName = $sender_name ; name from who the email was sent
$FromAddress = $sender_mail  ; address from where the mail should come
$ToAddress = $send_mail     ; destination address of the email - REQUIRED
$Subject = $sender_subject  ; subject from the email - can be anything you want it to be
$Body = $sender_msg ; the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = ""   ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
$CcAddress = ""     ; address for cc - leave blank if not needed
$BccAddress = ""    ; address for bcc - leave blank if not needed
$Importance = "Normal" ; Send message priority: "High", "Normal", "Low"
$Username = $sender_mail   ; username for the account used from where the mail gets sent - REQUIRED
$Password = $sender_password                ; password for the account used from where the mail gets sent - REQUIRED
$IPPort = $port                                 ; port used for sending the mail
$ssl = 1                                    ; enables/disables secure socket layer sending - put to 1 if using httpS

;##################################
; Script
;##################################
Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
If @error Then
    MsgBox(0, "Error sending message", "Error code:" & @error & "  Description:" & $rc)
EndIf

$i = $i + 1
WEnd
MsgBox(0,"Script finished","Messages sent")


;
; The UDF
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance="Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
    Local $objEmail = ObjCreate("CDO.Message")
    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
    $objEmail.To = $s_ToAddress
    Local $i_Error = 0
    Local $i_Error_desciption = ""
    If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
    If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
    $objEmail.Subject = $s_Subject
    If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then
        $objEmail.HTMLBody = $as_Body
    Else
        $objEmail.Textbody = $as_Body & @CRLF
    EndIf
    If $s_AttachFiles <> "" Then
        Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
        For $x = 1 To $S_Files2Attach[0]
            $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
;~          ConsoleWrite('@@ Debug : $S_Files2Attach[$x] = ' & $S_Files2Attach[$x] & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
            If FileExists($S_Files2Attach[$x]) Then
                ConsoleWrite('&> File attachment added: ' & $S_Files2Attach[$x] & @LF)
                $objEmail.AddAttachment($S_Files2Attach[$x])
            Else
                ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
                SetError(1)
                Return 0
            EndIf
        Next
    EndIf
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
    If Number($IPPort) = 0 then $IPPort = 25
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    ;Authenticated SMTP
    If $s_Username <> "" Then
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
    EndIf
    If $ssl Then
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    EndIf
    ;Update settings
    $objEmail.Configuration.Fields.Update
    ; Set email Importance
    Switch $s_Importance
        Case "High"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "High"
        Case "Normal"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Normal"
        Case "Low"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Low"
    EndSwitch
    $objEmail.Fields.Update
    ; Sent the Message
    $objEmail.Send
    If @error Then
        SetError(2)
        Return $oMyRet[1]
    EndIf
    $objEmail=""
EndFunc   ;==>_INetSmtpMailCom


; Com Error Handler
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    $oMyRet[0] = $HexNumber
    $oMyRet[1] = StringStripWS($oMyError.description, 3)
    ConsoleWrite("### COM Error !  Number: " & $HexNumber & "   ScriptLine: " & $oMyError.scriptline & "   Description:" & $oMyRet[1] & @LF)
    SetError(1); something to check for when this function returns
    Return
EndFunc   ;==>MyErrFunc


Func _ReadSenderInfo()
    
    $sender_subject = iniread("config\\sender.ini","SENDER","SUBJECT","no subject")
    $sender_mail = iniread("config\\sender.ini","SENDER","MAIL","mail@host.com")
    $sender_name = iniread("config\\sender.ini","SENDER","NAME","no name")
    $sender_server = iniread("config\\sender.ini","SENDER","SERVER","SERVER")
    $sender_msg = iniread("config\\sender.ini","SENDER","MESSAGE","MESSAGE BODY")
    $port = iniread("config\\groups\\mails.ini","MAILS","PORT","465")
EndFunc

Func _ReadMails()
    $send_mail = iniread( "config\\groups\\" & $group_selected & ".ini", "MAILS", "MAIL" & $i, "mail@host.com" )
EndFunc


func _new_mail_group()
    local $new_mail_list_window
    local $mail_list
    local $label1
    local $button1
    local $button2
    local $input1
    local $label2
    
    #Region ### START Koda GUI section ### Form=
    $new_mail_list_window = GUICreate("New Mail List", 405, 294, 302, 218)
    $mail_list = GUICtrlCreateEdit("", 40, 86, 321, 163)
    GUICtrlSetData(-1, "example@hotmail.com;example2@yahoo.com")
    $Label1 = GUICtrlCreateLabel("Type the mails here. Split mails with a semicolon ';' NOT WITH ENTER ", 48, 24)
    $Button1 = GUICtrlCreateButton("OK", 80, 264, 75, 25)
    $Button2 = GUICtrlCreateButton("Cancel", 192, 264, 73, 25)
    $Input1 = GUICtrlCreateInput("", 120, 56, 241, 21)
    $Label2 = GUICtrlCreateLabel("Group name:", 40, 56, 65, 17)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###
    
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $Button2
                ExitLoop
            Case $Button1
                _make_new_list( GUICtrlRead( $mail_list ), GUICtrlRead( $input1 ) )
                MsgBox( 0, "Note", "Newly created groups can't be used before program re-launch" )
                ExitLoop
    
        EndSwitch
    WEnd
    GUIDelete($new_mail_list_window)
EndFunc

func _make_new_list( $list, $name )
        local $array
        $array = stringsplit( $list, ",", 1 )
        local $k
        for $k = 1 to $array[0] step 1
            Iniwrite( "config\\groups\\" & $name & ".ini", "MAILS","MAIL" & $k, $array[$k] )
        Next
        iniwrite("config\\groups\\" & $name & ".ini", "MAILS", "NUMBER", $k-1 )
        local $tmp = iniread("config\\groups\\index.ini","INDEX","NUMBER",0)
        $tmp = $tmp + 1
        iniwrite("config\\groups\\index.ini","INDEX","NUMBER", $tmp )
        iniwrite("config\\groups\\index.ini","INDEX","GROUP" & $tmp, $name )
        
    EndFunc
    
    func _ReadMailGroups()
        
        local $k
        if $group_max > 0 Then
            for $k = 1 to $group_max step 1
                $group_array[$k] = iniread( "config\\groups\\index.ini", "INDEX", "GROUP" & $k, "" )
            Next
        EndIf
        
        
        
    EndFunc
    

func _change_msg()
    #Region ### START Koda GUI section ### Form=
    $msg_window = GUICreate("Edit Mail", 615, 438, 192, 124)
    local $labels = GUICtrlCreateLabel("Mail Body", 16, 80, 50, 17)
    local $Labeld = GUICtrlCreateLabel("Mail Subject", 16, 40, 62, 17)
    local $mail_subject_input = GUICtrlCreateInput("", 88, 40, 169, 21)
    local $msg_body_input = GUICtrlCreateEdit("", 16, 96, 577, 297)
    local $Button_ok_body = GUICtrlCreateButton("OK", 48, 400, 129, 33)
    local $Button_cancel_body = GUICtrlCreateButton("Cancel", 344, 400, 137, 33)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###
    GUICtrlSetData( $msg_body_input, $sender_msg )
    GUICtrlSetData( $mail_subject_input, $sender_subject )
    
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
        Case $GUI_EVENT_CLOSE
                GUIDelete($msg_window)
                ExitLoop
    
            Case $Button_ok_body
                $sender_subject = GUICTrlread( $mail_subject_input)
                $sender_msg = GuiCtrlread( $msg_body_input )
                GUIDelete( $msg_window)
                ExitLoop
    
            Case $Button_cancel_body
                GUIDelete($msg_window)
                ExitLoop
            EndSwitch
    WEnd

EndFunc
Link to comment
Share on other sites

really your first script? you've out done yourself. Maybe i'm just not all that used to koda. That is too much for me to figure out. stringsplit returns an array with indexs to each of the strings separated by the delimiter string.

; parameter " " here is the delimiter string
$stringarray= stringsplit("I am a string to be cut into parts at each of my spaces", " ")
;now,
;$stringarray[0]= 14;total string segments
for $i=0 to $stringarray[0]
    MsgBox(0, "stringarray["&$i&"]= ", $stringarray[$i])
next

;$stringarray[0]= total string segments found in scanned string in this case 14

$stringarray[1] would be the first string segment

it would equal= I

$stringarray[2] would be the first string segment

it would equal= am

and so on down the list

That help?

Edited by songersoft
Link to comment
Share on other sites

really your first script? you've out done yourself. Maybe i'm just not all that used to koda. That is too much for me to figure out. stringsplit returns an array with indexs to each of the strings separated by the delimiter string.

$stringarray= stringsplit("I am a string to be cut into parts at each of my spaces", " ")
;now,
;$stringarray[0]= 14;total string segments
for $i=0 to $stringarray[0]
    MsgBox(0, "stringarray["&$i&"]= ", $stringarray[$i])
next

;and so on down the list

That help?

my problem isn't with the syntax or anything. My problem is when i type " $array = stringsplit( $list, "\n", 1 ) ", it's not sepperating it from the end of line. I.E. i write:

adnan_elezovic@hotmail.com

some_other_mail@gmail.com

and_another@yahoo.com

i need a array where:

$array[0]=3 -already done

$array[1]=adnan_elezovic@hotmail.com

$array[2]=some_other_mail@gmail.com

$array[3]=and_another@yahoo.com

but when i type $array = stringsplit( $list, "\n", 1 ) i get $array[0]=1 and the whole string in $array[1]

Link to comment
Share on other sites

Hi and welcome if you want to split the newlines maybe you can try this.

Edit line 309 to this

$array = stringsplit( $list, @CRLF, 1 )

@CR Carriage return, Chr(13); sometimes used for line breaks.

@LF Line feed, Chr(10); typically used for line breaks.

@CRLF = @CR & @LF ;occasionally used for line breaks.

:) come back whan you have any doubt.
Link to comment
Share on other sites

your running Windows OS? try searching for @CRLF as the delimiter for next line.

Hi and welcome if you want to split the newlines maybe you can try this.

Edit line 309 to this

$array = stringsplit( $list, @CRLF, 1 )

:) come back whan you have any doubt.

Thank you, just what i needed. I tried with CHR(13) but had some difficulties. I attached a zip with the complete project. If someone want's to see the end result be my guest. Though it's having some difficulties sending a mail from hotmail smtp, reason not yet found. But smtp.gmail.com works flawless...

full.zip

Edited by Raziel
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...