Working flawlessly, but I'm struggling with the sender-part. I'm running this script as a scheduled task on my comp, but want another e-mail-address as the reply-address.
Obviously outlok uses the users configured account as default sender, and I do not accomplish to change this in the script. I have even tried using sender accounts which I have full access to... but still no luck.
Any suggestions?
Edited by Curious (2016-02-1611:08 AM)
_________________________
I was placed on earth to do a job. Right now I have so much to do, I will never die..
My personal experience scripting Outlook to email has been filled with cursing and inconsistent results. I ended up using the CDO object instead. All you need is a SMTP server, which could be local to the PC, installed on a server on your LAN, or you can even use something like Outlook.com's or Gmail's SMTP server. For more details see:
Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
The cursing and inconsistent results sound familiar Allen. Been working on this too and it is so f****ng frustrating that it works some times and does not work the next time with absolutely no changes to the script and the system running it.
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
I've avoided this and standardized on BLAT. You can specify all of the parameters at run-time and accomplish what you need pretty easily. See the script vault for an example of using Blat.
One EXE file, no installation - just copy the blat.exe and call it from Kix with the args you need.
Glenn
_________________________ Actually I am a Rocket Scientist!
Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
Originally Posted By: Glenn Barnas
I've avoided this and standardized on BLAT. ....
Certainly an option. We used to use BLAT for all of processes that send their results by e-mail. When we had our own Exchange server all was fine but when we moved to Google apps mail and continued to use BLAT we got blacklisted within one hour after the first process sent just one e-mail. By using Outlook and GASMO we avoid all blacklist issues. This might be different in another setup but I’m just saying that you need to be careful. BLAT might not be appreciated that much by your mail provider.
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
I'm not sure how you'd be using Blat, but we always have an internal mail relay server that is either properly configured to authoritatively send email for our domain(s) or use a smart-host that is. Blat forwards email to that internal relay host. We'd never use Blat (or Outlook or any other mail tool for that matter) to try and send email directly.
Glenn
_________________________ Actually I am a Rocket Scientist!
Have been thinking of the "SendOnBehalf" earlier, but rejected this as a good option. Tried it quickly though, and to my surprise the reply-address became the originating sender, not the send-on-behalf-of.. Strange. But not putting any further effort looking into this.
So gotta look at other options I guess, cause as you guys underline, the way I thought I would handle this is not the way to go.
We have our own mail-server, so problems like using an external isn't a problem regarding the use of for example Blat.
//Regards JE
_________________________
I was placed on earth to do a job. Right now I have so much to do, I will never die..
you say you can specify all parameters at runtime?
I have quite a number of arrays I need to be handled, like this (sorry for my probabbly bad quick-translation into english)
Code:
$MsgSubject = "New user " + $aOut3[2] + ", " + $aOut3[1] + " has to bee created in our XXX-system"
$MailRcpt = "someaddress@mail.no"
$MsgTekst1 = "This is an automated mail ." + @crlf + @crlf + "User " + $aOut3[2] + ", " + $aOut3[1] + " has been created in our system in department " + $aOut3[6] + @crlf + @crlf + "The user har been created with this information: " + @crlf + @crlf
$MsgTekst2 = left("Sirname ",12) + $aOut3[2] + @crlf + left("Givenname: ",12) + $aOut3[1] + @crlf + left("Department ",12) + $aOut3[6] + ", " + $aOut3[5] + @crlf
...before calling the sendmail-function.
I haven't still explored blat, but can this be handled?
//Regards JE
_________________________
I was placed on earth to do a job. Right now I have so much to do, I will never die..
Allen: I'm looking into your sendmail also, looking a bit overwhelming, but if it gets the job done...
You write that you need to install the smtp-service or exchange. So.. we have our own exchange-server, does this mean that there should not be necessary to install anything to get this to work?
Maybe I need to set up relaying on our exchange-server?
Anyway.. I already have installed Remote Server Admin Tools, isn't this where the smtp-service is? How do I activate this if this is needed?
//Regards JE
_________________________
I was placed on earth to do a job. Right now I have so much to do, I will never die..
Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
A short example using BLAT. I used your text for the body of the message to show that what you want/need is possible.
The BlatMailerNoAttachment function needs to be updated with the correct path to the BLAT executable. I used BLAT in a function because it was used in several locations in the script.
Please be aware that I did not test the code below.
Code:
;Set sender address
$Sender = "email@@yourdomain.com"
;Set a recipient address.
$Recipient = "someaddress@mail.no"
;Set the subject.
$Subject = "E-mail subject goes here"
;SMTP server to be used for sending the message.
$smtpserver = "New user " + $aOut3[2] + ", " + $aOut3[1] + " has to bee created in our XXX-system"
;Create the body text.
$Body = "This is an automated mail ." + @crlf + @crlf + "User " + $aOut3[2] + ", " + $aOut3[1] + " has been created in our system in department " + $aOut3[6] + @crlf + @crlf
$Body = $Body + "The user har been created with this information: " + @crlf + @crlf
$Body = $Body + Left("Sirname ", 12) + $aOut3[2] + @crlf + Left("Givenname: ", 12) + $aOut3[1] + @crlf + Left("Department ", 12) + $aOut3[6] + ", " + $aOut3[5] + @crlf
;Send the message.
$Send = BlatMailerNoAttachment($Recipients, $Sender, $Subject, $Body, $smtpserver)
Function BlatMailerNoAttachment($Recipient, $Sender, $Subject, $Body, $smtpserver)
Dim $MailerLine
$MailerLine = 'ENTERPATHTOBLATHERE\blat.exe -' + ' -to ' + $recipient + ' -f ' + $sender + ' -subject ' + $subject + ' -body ' + $body + ' -server ' + $smtpserver + ' -q '
Shell $MailerLine
EndFunction
The thing about UDFs (User Defined Functions) is, you really don't have to let the code be so overwhelming, but instead just focus on what you need to provide it to work. In this case, the information you need to provide it is... the EXACT same information you provide blat. (In fact, it crossed my mind yesterday to update the function to assist those that still want to use blat and put all the variable in the right place). Look at the examples in the header. The advantage of the UDF is the CDO object it is based on, is baked right into every Win OS since XP, nothing to install, nothing to copy.
Most Exchange Servers do not allow relay so you would likely need to look into that before you could email from blat or sendmail. However, I have found that to be overly complicated. What I have done is something similar to what Glenn was talking about, I set up a hmailserver that is specifically designed to relay email for internal things like this. Hmailserver is one of the best couple of hours I've spent lately learning how to use it. It has IMAP and POP to, but you don't have to use them. Hmail has great documentation and the forums are active as well.
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
There's a SendMailB() UDF on my web site that I recently extracted from the example here in the Script Vault forum. You can download that and follow the instructions and have a mail delivery system running in a few minutes.
Basically, you take your data and write it to a file, like this:
Code:
$ = RedirectOutput('.\messageid.eml')
'This is the message body to send' @CRLF
'[SENDMAIL]' @CRLF
'RecipientAddr=' $SendTo @CRLF
'RecipientName=' $SendName @CRLF
'RecipientCC=' $CCList @CRLF
'RecipientBCC=' $BCCList @CRLF
'FromAddr=' $FromEmail @CRLF
'FromName=' $FromName @CRLF
'Subject=' $Subject @CRLF
'HtmlFormat=' 1 @CRLF
$ = RedirectOutput('') ; close the message file
$aResult = SendMailB('.\messageid.eml')
This will send the message using Blat with the parameters in the MailSend.ini file, which is located in the script dir with Blat.exe.
The service-based version of this is actually quite handy, as it allows many systems to send an email without any special software or multiple authorizations defined. By having a single file server with the Blat executable and the mail sending service, all a script needs to do is create a file (as shown above) and then move it to the shared folder. Within 30 seconds, the file is found, processed, mail sent, and the file removed. This also improves (and simplifies) security by having just one system authorized for mail relay, yet any machine in the environment running with credentials authorized to the mail drop folder can send mail.
I use the code from the script vault almost as-is, except that I added a simple logging process that reports each message processed and the status from Blat. It's been so reliable that I never implemented the "retry" logic mentioned in the article, although that would not be difficult to do.
In my DMZ, the web content management server runs this script-service, and all of the web delivery servers use this to drop messages. The server sends the message to the hMail server (also in the DMZ), which is authorized for direct external delivery via smart-host, and allowed through the firewall to the Exchange server. The various maintenance and monitoring scripts that I use employ the same method. I defined a SRV record in DNS to identify the hosts with the mail drop folder, and the script gets the address/name of the server in their network using the GetResourceBySRV() UDF. (I have 2 mail-drop servers, one in the DMZ and one in the protected network. I create a SRV record for both and use the GetResourceBySRV() function to return the "closest" resource, based on network subnet range.)
I can convert the SendMailB() function to accept all parameters via an array, something that I had been planning to do. Give me an hour and I should have it ready and will post it here.
Glenn
_________________________ Actually I am a Rocket Scientist!
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Here's the Blat based send-mail UDF that accepts all values via an array. See the example in the header for how-to-use. I've done limited testing in my environment based on where I can send SMTP messages from/to.
This version automatically applies arguments like "-ss" to suppress the subject when the arg is blank, or use "-ur" when the To is blank and BCC is defined (send to Undisclosed Recipients). Also, when a multi-line message is supplied, it is written to a temporary file, and the file is removed after calling Blat. The result is an array with Success/Fail, the error code, error string, and the STDOUT and STDERR messages from Blat.
Glenn
Code:
;;
;;=====================================================================================-----
;;
;;FUNCTION SendMailBA()
;;
;;ACTION Sends mail from an array using BLAT.EXE
;;
;;AUTHOR Glenn Barnas
;;
;;VERSION 1.0 - 2016/02/16
;;
;;SYNTAX SendMailBA(MessageData)
;;
;;PARAMETERS MessageData - REQUIRED, Array - The array of data representing the.
;; message. It must use the following format:
;;
;; 0 SMTP_Server = FQDN or IP of the SMTP Server authorized to send
;; 1 User = The username, if required for authorization
;; 2 Password = The password, if required for authorization
;; 3 HtmlFormat = <Boolean - set to 1 to use HTML Formatting>
;; 4 Reserved for Future Use
;; 5 RecipientAddr = <The "To" email address - name@domain.tld>
;; 6 RecipientCC = <email addresses added to the "CC" list>
;; 7 RecipientBCC = <email addresses added to the "BCC" list>
;; 8 FromAddr = <The "From" email address - myname@domain.tld>
;; 9 FromName = <The "From" email user name - Jane Sender>
;; 10 Subject = <The "Subject" line content>
;; 11 Message body - text or html, can span multiple lines, including blank lines.
;;
;;
;; The username and password are only required if the server requires
;; authentication. If it does, the file should be restricted to
;; administrators, SYSTEM, (Modify) and the user running the script (Read).
;;
;;REMARKS This function REQUIRES that NoMacrosInStrings be used or "@" signs be escaped!
;; If the message contains multiple lines, it will be written to a temp file in
;; the user's TEMP folder. The file will be removed upon exiting the function.
;;
;;RETURNS Array of 3 elements
;; 0 = Result (1=success, 0=fail)
;; 1 = Error Code
;; 2 = Error Message
;; 3 = STDOUT result from BLAT command
;; 4 = STDERR result from BLAT command
;;
;;DEPENDENCIES BLAT.exe - must be in same directory as the calling script or in a folder
;; specified by the S_BIN environment variable.
;;
;;TESTED WITH Servers: W2K-W2K12r2 Workstations: W2K, XP, Vista, Windows 7, 8, 10
;;
;;EXAMPLES $ = SetOption('NoMacrosInStrings', 'On') ; REQUIRED to avoid issues with "@" signs
;;
;; Dim $aMessageData[11]
;; $aMessageData[0] = 'SMTPserver.domain.tld' ; SMTP Server IP or FQDN
;; $aMessageData[1] = 'AuthUser@domain.tld' ; SMTP Auth User
;; $aMessageData[2] = 'P@sswurd' ; SMTP Auth Password
;; $aMessageData[3] = 'N' ; Boolean - Use HTML message format
;; $aMessageData[4] = '' ; Reserved for Future Use
;; $aMessageData[5] = 'JohnDoe@somewhere.tld' ; To (comma-delimited list)
;; $aMessageData[6] = '' ; CC (comma-delimited list, can be blank)
;; $aMessageData[7] = '' ; BCC (comma-delimited list, can be blank)
;; $aMessageData[8] = 'sender@senddom.tld' ; From (address)
;; $aMessageData[9] = 'Mary Q. Sender' ; From Name (full name)
;; $aMessageData[10] = 'Test Message' ; Subject (can be blank)
;; $aMessageData[11] = 'This is Line 1' + @CRLF ; Message Body (can be multi-line)
;; $aMessageData[11] = $aMessageData[11] + 'This is Line 2' + @CRLF + @CRLF
;; $aMessageData[11] = $aMessageData[11] + 'This is Line 4'
;;
;; $aResult = SendMailBA($aMessageData) ; call the function
;; If $aResult[0]
;; 'Success!' ?
;; Else
;; ' Result: ' $aResult[0] ?
;; 'Error Code: ' $aResult[1] ?
;; 'Error Text: ' $aResult[2] ?
;; ' STDOUT: ' $aResult[3] ?
;; ' STDERR: ' $aResult[4] ?
;; EndIf
;
Function SendMailBA($_aMsgData)
Dim $_ ; general purpose var
Dim $_Cmd ; command string
Dim $_To ; Recipient List
Dim $_Subj ; Subject line
Dim $_Fmt ; body format flag
Dim $_Msg ; log message
Dim $_BlatCmd ; path to Blat.exe
Dim $_oExec ; Wsh Exec Object
Dim $_NMISState ; NoMacrosInStrings state
Dim $_TFile ; temp file for multi-line messages
; fail if BLAT.EXE is not present in Script dir or System BIN dir
If Not Exist(@SCRIPTDIR + '\blat.exe') And Not Exist('%S_BIN%\blat.exe')
$SendMailBA = 0,87,'Invalid Configuration - no BLAT.EXE file','',''
Exit 87
Else
If Exist(@SCRIPTDIR + '\blat.exe')
$_BlatCmd = @SCRIPTDIR + '\blat.exe'
Else
$_BlatCmd ='%S_BIN%\blat.exe'
EndIf
EndIf
; Determine if HTML or TEXT format - HTML overrides TEXT
$_Fmt = IIf(InStr(' 1 T TRUE Y YES O ON ', ' ' + $_aMsgData[3] + ' ') > 0, ' -html', '')
; Define the recipient (To) list, use "-ur" if blank and BCC is defined, error if blank and no BCC
If Not $_aMsgData[5] ; if To is empty
If $_aMsgData[7] ; and BCC is defined
$_To = ' -ur' ; set "Undisclosed Recipients"
Else
$SendMailBA = 0,87,'Invalid Configuration - invalid recipients','',''
Exit 87
EndIf
Else
$_To = ' -to ' + $_aMsgData[5]
EndIf
; Set the Subject line to "-ss" if blank
$_Subj = IIf(Len($_aMsgData[10]) = 0, ' -ss', ' -subject "' + $_aMsgData[10] + '"')
; Check the Message body for multiple lines - use a temp file if so
$_TFile = ''
If InStr($_aMsgData[11], @CRLF)
$_TFile = '%TEMP%\BEMMsg_' + Right('00000' + @TICKS, 5) + '.msg'
$_ = RedirectOutput($_TFile, 1)
$_aMsgData[11]
$_ = RedirectOutput('')
EndIf
; build the BLAT command to execute
$_Cmd = '%COMSPEC% /c ' + $_BlatCmd ; base command
If $_TFile
$_Cmd = $_Cmd + ' ' + $_TFile
Else
$_Cmd = $_Cmd + ' - -body "' + $_aMsgData[11] + '"' ; add Body text
EndIf
$_Cmd = $_Cmd + $_Fmt ; add html format arg if defined
$_Cmd = $_Cmd + $_To ; Recipient list
If $_aMsgData[6]
$_Cmd = $_Cmd + ' -cc ' + $_aMsgData[6] ; CC Recipient list if defined
EndIf
If $_aMsgData[7]
$_Cmd = $_Cmd + ' -bcc ' + $_aMsgData[7] ; BCC Recipient list if defined
EndIf
$_Cmd = $_Cmd + ' -f "' + $_aMsgData[8] + '"' ; From Address
$_Cmd = $_Cmd + ' -i "' + $_aMsgData[9] + '"' ; From Name
$_Cmd = $_Cmd + $_Subj ; subject
$_Cmd = $_Cmd + ' -server ' + $_aMsgData[0] ; SMTP server
If $_aMsgData[1]
$_Cmd = $_Cmd + ' -u ' + $_aMsgData[1] ; SMTP Auth ID (if specified)
If $_aMsgData[2]
$_Cmd = $_Cmd + ' -pw ' + $_aMsgData[2] ; SMTP Auth PW (if specified)
EndIf
EndIf
$_oExec = CreateObject("WScript.Shell").Exec($_Cmd) ; instantiate WSH and run the command
If Not VarType($_oExec) = 9 ; Check for correct vartype returned
$SendMailBA = 0,10,'WScript.Shell Exec Unsupported','','' ; set the result
If $_TFile Del $_TFile EndIf ; delete temp file, if used
Exit 10 ; exit with error
Else
; Wait for completion and gather the STDOUT and STDERR text into arrays of arrays
While Not $_oExec.Status
Sleep 0.01
Loop
$SendMailBA = Not($_oExec.ExitCode),$_oExec.ExitCode,@SERROR,$_oExec.StdOut.ReadAll,$_oExec.StdErr.ReadAll
If $_TFile Del $_TFile EndIf ; delete temp file, if used
Exit = $_oExec.ExitCode
EndIf
EndFunction
_________________________ Actually I am a Rocket Scientist!
I looked into blat the other day, and it didn't seem all that easy to handle everything I want, cause the mail I send does need lots of linefeeds and a bunch of different arrays to provide essential information to different people. When I tried adding f.ex. @crlf, blat didn't handle it. So didn't test more since it failed already "in the early stages".
I hear what you say about functions Allen, but I really need a lot of information passed on via mail to selected users. If the function is called and written the right way, what should be the problem even if you have lots of arrays etc?
So seems like - as you're suggesting - my best option is to use your approach to get blat working the way I want, if this will be the solution I choose to use.
Thanks for the effort Glenn, I will definately look into what you're suggesting.
//Regards JE
Edited by Curious (2016-02-1903:01 PM)
_________________________
I was placed on earth to do a job. Right now I have so much to do, I will never die..
Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
Originally Posted By: Curious
I looked into blat the other day, and it didn't seem all that easy to handle everything I want, cause the mail I send does need lots of linefeeds and a bunch of different arrays to provide essential information to different people. When I tried adding f.ex. @crlf, blat didn't handle it. So didn't test more since it failed already "in the early stages". ....
It is defiantly possible to add a large amount of data from arrays, variables, external files, etc.... to the body text of an e-mail using BLAT. It is just a matter or formatting the data as needed, put it in a variable and use that variable as the body text. I did this for many years until we moved away from Exchange. Above is already an example of how to do that with the data you provided earlier. If you need some help formatting all the data so BLAT accepts it as the body text just post it here and we can assist where needed.
Really not sure I am following, but I can tell you that ShaneEP and myself tested that function a lot before posting it. There have been followup suggestions and those have been incorporated as well. You can provide the body as a String or as a file.
All you have to do is copy the function, as is, no changes, to your script. Follow the examples in the header, or try the example below.
Even if your data is in array, all you have to do is a $body=join($data,@CRLF). So something like this...
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
If you look at the function, you will see that the array contains an element for the message. The message can be multi-line. Since Blat doesn't like multi-line messages on the command line, the function is smart enough to see a multi-line message and write it to a temporary file. This file becomes the argument to Blat, which it happily digests. After Blat completes, the function removes the temp file.
There are many more Blat functions available, such as attachments, that my function doesn't support. These would be easy to implement by expanding the input array and adding the logic to the function. I'd be willing to review and update these if they are needed, as it would make the function more globally useful.
Glenn
_________________________ Actually I am a Rocket Scientist!