使用特定帐户从 outlook(com 组件)使用 php 发送电子邮件

Send email using php from outlook (com component) ,using particular account

我正在使用 PHP 和 outlook 发送电子邮件。 我在 outlook 中设置了多个帐户,我想每次都从一个特定的帐户发送电子邮件。 我当前的代码如下:

if (!defined('olMailItem')) define("olMailItem",0);
$objApp = new COM("Outlook.Application");
$myItem = $objApp->CreateItem(olMailItem);
$myItem->To= 'to@abc.com';
$myItem->SentOnBehalfOfName = 'from@xyz.com';
$myItem->Subject='my subject';

$myItem->HTMLBody='email content';
$myItem->Display();
$myItem->Send()

使用 $myItem->SentOnBehalfOfName 不起作用,它总是使用默认帐户发送电子邮件,但我想使用 PHP.

设置发件人帐户

您似乎对 MailItem class 的 SendUsingAccount 属性 感兴趣,它允许设置一个 Account 对象,表示发送 MailItem 所使用的帐户. SendUsingAccount 属性 可用于指定调用 Send 方法时应用于发送 MailItem 的帐户。例如:

Sub SendUsingAccount() 
  Dim oAccount As Outlook.account 
  For Each oAccount In Application.Session.Accounts 
    If oAccount.AccountType = olPop3 Then 
      Dim oMail As Outlook.MailItem 
      Set oMail = Application.CreateItem(olMailItem) 
      oMail.Subject = "Sent using POP3 Account" 
      oMail.Recipients.Add ("someone@example.com") 
      oMail.Recipients.ResolveAll 
      oMail.SendUsingAccount = oAccount 
      oMail.Send 
    End If 
  Next 
End Sub 

请注意,Microsoft 目前不建议也不支持来自任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM和 NT 服务),因为当 Office 在此环境中 运行 时,Office 可能表现出不稳定的行为 and/or 死锁。

如果您要在服务器端上下文中构建 运行 的解决方案,您应该尝试使用已针对无人值守执行安全处理的组件。或者,您应该尝试找到至少允许 运行 客户端部分代码的替代方案。如果您从服务器端解决方案使用 Office 应用程序,该应用程序将缺少许多 运行 成功所必需的功能。此外,您将承担整体解决方案稳定性的风险。有关详细信息,请参阅 Considerations for server-side Automation of Office