'Send Outlook 2010 email using PHP
I'm looking to use the default Windows 7 Outlook 2010 mail account to send an email.
I've tried:
oApp = new COM("Outlook.Application") or die('error');
$oMsg = $oApp ->CreateItem($oApp->OlItemType->olMailItem);
$oMsg ->Recipients->Add("[email protected]");
$oMsg ->Subject="aaaa";
$oMsg ->Body="body";
$oMsg ->Save();
$oMsg ->Send();
But I get the error:
Outlook loaded, version 14.0.0.7109
Fatal error: Uncaught exception 'com_exception' with message 'Unable to lookup
`OlItemType': Unknown name. ' in C:\xampp\htdocs\Intranet_IT_Request_Form
\comunread.php:5 Stack trace: #0 C:\xampp\htdocs\Intranet_IT_Request_Form
\comunread.php(5): unknown() #1 {main} thrown in C:\xampp\htdocs
\Intranet_IT_Request_Form\comunread.php on line 5
My research tells me I need cdo.dll, which contains all the email functions, but I can only install this with Outlook 2007; not practical at all.
Does anyone know how to send an Outlook 2010 email using PHP? (I'm using XAMPP).
Many many thanks
Solution 1:[1]
This works:
if (!defined("olMailItem")) {define("olMailItem",0);}
$oApp = new COM("Outlook.Application") or die('error');
$oMsg = $oApp->CreateItem(olMailItem);
$oMsg->Recipients->Add("[email protected]");
$oMsg->Subject=$subject;
$oMsg->Body=$message;
$oMsg->Save();
$oMsg->Send();
Solution 2:[2]
For me the next code works just out of the box:
<?php
$subject="This is a test message";
$message="This is a Body Section now.....! :)";
$to="[email protected]";
// starting outlook
com_load_typelib("outlook.application");
if (!defined("olMailItem")) {define("olMailItem",0);}
$outlook_Obj = new COM("outlook.application") or die("Unable to start Outlook");
//just to check you are connected.
echo "Loaded MS Outlook, version {$outlook_Obj->Version}\n";
$oMsg = $outlook_Obj->CreateItem(olMailItem);
$oMsg->Recipients->Add($to);
$oMsg->Subject=$subject;
$oMsg->Body=$message;
$oMsg->Save();
$oMsg->Send();
?>
Please, ensure that you have added
[COM_DOT_NET]
extension=php_com_dotnet.dll
at the end of php.ini (In my case I have PHP 5.3)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ian |
Solution 2 | riopiedra |