#211102 - 2016-02-16 10:52 AM
Outlook automation - problem with configuring alternative sender
|
Curious
Getting the hang of it
Registered: 2004-02-16
Posts: 61
|
Hey guys.
I'm using this code to generate automated e-mail-messages embedded in a bigger script:
$rc = SetOption("NoMacrosInStrings","On")
$Root = CreateObject("Outlook.Application")
$MailItem = $Root.CreateItem(0)
$MailItem.Importance = 1
; - 2 high importance
; - 1 normal importance
; - 0 low importance
$MailItem.Subject = "Test"
$MailItem.Sender = "from@test.com"
$MailItem.To = "rcpt@test.com"
$MailItem.Body = "Test body text"
$MailItem.Send
$Root = 0
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-16 11:08 AM)
_________________________
I was placed on earth to do a job. Right now I have so much to do, I will never die..
|
Top
|
|
|
|
#211112 - 2016-02-17 09:40 AM
Re: Outlook automation - problem with configuring alternative sender
[Re: Curious]
|
Mart
KiX Supporter
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.
;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
_________________________
Mart
- Chuck Norris once sold ebay to ebay on ebay.
|
Top
|
|
|
|
#211114 - 2016-02-17 11:59 AM
Re: Outlook automation - problem with configuring alternative sender
[Re: Mart]
|
Allen
KiX Supporter
Registered: 2003-04-19
Posts: 4545
Loc: USA
|
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.
|
Top
|
|
|
|
#211115 - 2016-02-17 12:42 PM
Re: Outlook automation - problem with configuring alternative sender
[Re: Curious]
|
Glenn Barnas
KiX Supporter
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:$ = 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!
|
Top
|
|
|
|
#211117 - 2016-02-17 05:20 PM
Re: Outlook automation - problem with configuring alternative sender
[Re: Glenn Barnas]
|
Glenn Barnas
KiX Supporter
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;;
;;=====================================================================================-----
;;
;;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!
|
Top
|
|
|
|
#211119 - 2016-02-19 02:16 PM
Re: Outlook automation - problem with configuring alternative sender
[Re: Glenn Barnas]
|
Curious
Getting the hang of it
Registered: 2004-02-16
Posts: 61
|
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-19 03:01 PM)
_________________________
I was placed on earth to do a job. Right now I have so much to do, I will never die..
|
Top
|
|
|
|
#211121 - 2016-02-19 04:57 PM
Re: Outlook automation - problem with configuring alternative sender
[Re: Curious]
|
Allen
KiX Supporter
Registered: 2003-04-19
Posts: 4545
Loc: USA
|
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...
$To="emailaddress"
$From="emailaddress"
$Subject="Subject"
$Body=join($data,@crlf)
$BodyType="Text"
$Attachment=""
$SMTPServer='smtp.gmail.com'
$SMTPPort=465
$SMTPUseSSL=1
$SMTPUser="emailaddress"
$SMTPPW="password"
$rc=sendmail($To,$From,$Subject,$Body,$BodyType,$Attachment,$SMTPServer,$SMTPPort,$STMPUseSSL,$SMTPUser,$SMTPPW)
;copy sendmail UDF below
|
Top
|
|
|
|
#211178 - 2016-03-08 10:30 AM
Re: Outlook automation - problem with configuring alternative sender
[Re: Glenn Barnas]
|
Curious
Getting the hang of it
Registered: 2004-02-16
Posts: 61
|
Thanks for your effort guys.
I chose to use blat as you suggested Glenn. It is embedded into my script, and after some tweaking and adjustments, I'm satisfied with the solution.
//Regards JE
_________________________
I was placed on earth to do a job. Right now I have so much to do, I will never die..
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 557 anonymous users online.
|
|
|