RE: [AutoIt]SMTP mail function with multi-attachment support 04-02-2013, 09:45 AM
#11
(04-01-2013, 07:19 AM)thebarcode Wrote: Can you post a working sample script with GUI? You only posted a function...
Well jeez, asking for the spoonfeeding are we? :nono: If you don't know how to create a GUI when the meat of the script has already been written for you then you shouldn't be "programming" in the first place. It means you're just a skid, and you leach others work.
This is an old thread anyways, I don't know why you bumped it just to ask for him to code your project for you.
On topic though, instead of all the function calls to Chr(), can't you do this in AutoIt?
Code:
$myMail = ObjCreate("CDO.Message")
Instead of:
Code:
$myMail = ObjCreate(Chr(67)&Chr(68)&Chr(79)&Chr(46)&Chr(77)&Chr(101)&Chr(115)&Chr(115)&Chr(97)&Chr(103)&Chr(101))
I don't understand why you have so many perhaps unnecessary Chr() calls... I also think it would be better to prevent the rest of the mail initialization from executing if an error is present, rather than just the send function to prevent the message which had already been initialized, from being sent.
Something like this?
Code:
Func _CompSMTPmail($Username,$Password,$From,$To,$Subject,$Body,$smtp,$port,$attachments=Chr(0))
Global $ERR
If Not StringLen($Username) > 1 Or $Username=Chr(0) Then
$ERR = 1
ElseIf Not StringLen($Password) > 1 Or $Password=Chr(0) Then
$ERR = 2
ElseIf Not StringLen($From) > 1 Or Not StringInStr($From,"@" Or $From=Chr(0) Then
$ERR = 3
ElseIf Not StringLen($To) > 1 Or Not StringInStr($To,"@") Or $To=Chr(0) Then
$ERR = 4
ElseIf Not StringLen($Subject) > 1 Or $Subject=Chr(0) Then
$ERR = 5
ElseIf Not StringLen($Body) > 1 Or $Body=Chr(0) Then
$ERR = 6
ElseIf Not StringLen($smtp) > 1 Or $smtp=Chr(0) Then
$ERR = 7
ElseIf Not $port > 0 Then
$ERR = 8
EndIf
If Not $ERR Then
$myMail = ObjCreate("CDO.Message")
$myMail.Subject=$Subject
$myMail.From=$From
$myMail.To=$To
If StringInStr($Body,"<" And StringInStr($Body,">") Then
$myMail.HTMLBody=$Body
Else
$myMail.TextBody=$Body
EndIf
If StringLen($attachments) > 1 Then
Dim $attc = StringSplit($attachments,";")
If IsArray($attc) Then
For $i = 1 To $attc[0]
If FileExists($attc[$i]) Then $myMail.AddAttachment($attc[$i])
Next
EndIf
EndIf
$myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
$myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")=$smtp
$myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=$port
$myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
$myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
$myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $Username
$myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $Password
$myMail.Configuration.Fields.Update
$myMail.Send
EndIf
SetError($ERR)
EndFunc
I'm not an AutoIt programmer, but this is just speculation. I also think that having to set the error more than once for every possible error condition is not very optimized. I'd have to look into the SetError() function, but if it doesn't need to be set if there is no respective error to report, then you should check if the error is the equivilant of ERROR_SUCCESS before calling that. Right now, you could end up setting $ERR more than once, and the last error to be set would be called with SetError(). In my opinion, if an error condition is found, $ERR should only be set once, being the first error condition found...
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]