Hi have requirement to prepare outlook email draft and open it in Outlook.
I have created php form to run supply variables to a powershell wich invokes outlook methods and create the email.
When I execute the powershell as below
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe C:\wamp\www\bizops\prepare_email.ps1 >> C:\wamp\www\bizops\log.log
It works perfectly.
But when I place the above line in a batch file and call it in PHP Script,
$mail_string="c:\WINDOWS\system32\cmd.exe /c START C:\wamp\www\bizops\outlook_mail.bat";
exec($mail_string);
it gives me below error
New-Object : Retrieving the COM class factory for component with CLSID
{0006F03A-0000-0000-C000-000000000046} failed due to the following error:
80080005 Server execution failed (Exception from HRESULT: 0x80080005
(CO_E_SERVER_EXEC_FAILURE)).At C:\wamp\www\bizops\prepare_email.ps1:25 char:7
+ $ol = New-Object -comObject Outlook.Application
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
my powershell code looks like (Section which thorows this error)
$ol = New-Object -comObject Outlook.Application
$ns = $ol.GetNameSpace("MAPI")
$Mail = $ol.CreateItem(0)
$Mail.Recipients.Add($mail_to_list)
$mail.Subject = $mail_subject
$mail.DeferredDeliveryTime = $deliverAt
$Mail.HTMLBody = "SOME HTML TEXT"
Though it took so long to find a resolution as error was not clear. There was confusion if the issue is due to permission miss match or something else.
I am glad I was able to find the solution of this.
We need to run httpd through a valid user(You may want to create a separate user t to run your tasks). enter image description here
Hope screenshot will make it clear
But when I place the above line in a batch file and call it in PHP Script,
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.
As a workaround you may consider using EWS if you deal with Exchange profiles only, see EWS Managed API, EWS, and web services in Exchange. Or Outlook REST API which is available for Office 365 users, see Get Started with Mail, Calendar, and Contacts REST APIs.
Also you may consider using a low-level API on which Outlook is based on - Extended MAPI. Or just any other third-party wrapper around that API such as Redemption.
CO_E_SERVER_EXEC_FAILURE
most likely means the two processes are running in different security contexts - the COM system refuses to marshal the calls in this case.
Is either app running with elevated security privileges (Rus As Administrator)?
User contributions licensed under CC BY-SA 3.0