【问题标题】:Send email using php from outlook (com component) ,using particular account使用特定帐户从 Outlook(com 组件)使用 php 发送电子邮件
【发布时间】:2015-03-27 16:56:05
【问题描述】:

我正在使用 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 设置发件人帐户。

【问题讨论】:

    标签: php outlook


    【解决方案1】:

    您似乎对 MailItem 类的 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 服务)的 Microsoft Office 应用程序自动化,因为 Office在此环境中运行 Office 时可能会出现不稳定的行为和/或死锁。

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

    【讨论】:

    • 感谢您的回复。你能解释一下oMail.SendUsingAccount = oAccount的用法吗,特别是oAccount在这里是什么意思
    • 是Account类的一个实例。查看命名空间类的 Accounts 属性,了解有关 Outlook 配置文件中所有已配置帐户的详细信息。
    • 再次感谢。我遇到的问题如下:当我使用oMail.SendUsingAccount = "abc@xyz.com"时,它不起作用。有没有办法使用帐户电子邮件 ID 获取 oAccount 对象,例如:oAccount = new oAccount("abc@xyz.com")
    • 不行,需要在Accounts集合中找到对应的Account对象(见Namespace类的Accounts属性)。
    • 我是否需要每次循环所有帐户以找到匹配的帐户对象以发送电子邮件?
    猜你喜欢
    • 1970-01-01
    • 2015-07-26
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 2016-04-20
    • 2014-03-03
    相关资源
    最近更新 更多