Symfony2使用交换服务器发送电子邮件

时间:2013-10-25 07:36:54

标签: php email symfony exchange-server php-ews

我是Symfony的新手,我设法创建了一个简单的Symfony2.3.​​5应用程序,它应该通过Microsoft Exchange Server 2007发送(仅发送)自动电子邮件,我有一个用户和密码;一个普通的电子邮件用户,我可以使用该用户使用API​​进行交换,在Linux中使用Outlook或Evolution发送电子邮件。但我不知道如何从Symfony应用程序向此服务器发送电子邮件,然后服务器应该像我的用户那样传送任何正常的电子邮件。有没有人在MS交换之前做过这个?我可以阅读的任何文档,以获得它的工作原理IDEA吗?

我一直在阅读关于PhpEws,但我不知道它是否适用于这种情况我不知道如何将它添加到Symfony,我试过,但我没有管理,这就是我决定的原因询问这个问题。 问候并谢谢你!

1 个答案:

答案 0 :(得分:1)

显然我设法使它与Php-Ews一起使用,即使我知道这不是正确的方法,我使用它的方法仍然有效。我已将Php-Ews文件夹包含在项目的Controller文件夹中。我创建了一个响应/电子邮件的新路由,然后我在控制器中添加了这个:

namespace Osd\RetireBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use PhpEws\ExchangeWebServices;

class EmailsController {
public function indexAction()
{

    $this->sendEmail();

    return new Response('<html><body>Hello !</body></html>');
}
protected function sendEmail(){
    $PhpEwsPath = __DIR__."/php-ews/";

    require_once $PhpEwsPath.'ExchangeWebServices.php';
    require_once $PhpEwsPath.'NTLMSoapClient.php';
    require_once $PhpEwsPath.'NTLMSoapClient/Exchange.php';
    require_once $PhpEwsPath.'EWS_Exception.php';
    require_once $PhpEwsPath.'EWSType.php';
    require_once $PhpEwsPath.'EWSType/MessageType.php';
    require_once $PhpEwsPath.'EWSType/EmailAddressType.php';
    require_once $PhpEwsPath.'EWSType/BodyType.php';
    require_once $PhpEwsPath.'EWSType/SingleRecipientType.php';
    require_once $PhpEwsPath.'EWSType/CreateItemType.php';
    require_once $PhpEwsPath.'EWSType/NonEmptyArrayOfAllItemsType.php';
    require_once $PhpEwsPath.'EWSType/ItemType.php';        


    $server = 'server';
    $username = 'username';
    $password = 'password';  
    $ews = new \ExchangeWebServices($server, $username, $password);

    $msg = new \EWSType_MessageType();

    $toAddresses = array();
    $toAddresses[0] = new \EWSType_EmailAddressType();
    $toAddresses[0]->EmailAddress = 'email@example.com';
    $toAddresses[0]->Name = 'John Doe';   

    /*$toAddresses[1] = new \EWSType_EmailAddressType();
    $toAddresses[1]->EmailAddress = 'email2@example.com';
    $toAddresses[1]->Name = 'Richard Roe';  

    $toAddresses[2] = new \EWSType_EmailAddressType();
    $toAddresses[2]->EmailAddress = 'email3@example.com';
    $toAddresses[2]->Name = 'Hustle and Flow';        

    $toAddresses[3] = new \EWSType_EmailAddressType();
    $toAddresses[3]->EmailAddress = 'email4@example.com';
    $toAddresses[3]->Name = 'Crookedeye Moe';*/     

    $msg->ToRecipients = $toAddresses;

    $fromAddress = new \EWSType_EmailAddressType();
    $fromAddress->EmailAddress = 'email@example.com';
    $fromAddress->Name = 'Abel';

    $msg->From = new \EWSType_SingleRecipientType();
    $msg->From->Mailbox = $fromAddress;

    $msg->Subject = 'Test email message from RAS';

    $msg->Body = new \EWSType_BodyType();
    $msg->Body->BodyType = 'HTML';
    $msg->Body->_ = '<p style="font-size: 18px; font-weight: bold;">Test email message from php ews library from RAS.</p>';

    $msgRequest = new \EWSType_CreateItemType();
    $msgRequest->Items = new \EWSType_NonEmptyArrayOfAllItemsType();
    $msgRequest->Items->Message = $msg;
    $msgRequest->MessageDisposition = 'SendAndSaveCopy';
    $msgRequest->MessageDispositionSpecified = true;

    $response = $ews->CreateItem($msgRequest);
    var_dump($response);        
}

}

让我告诉你什么......它的工作原理!现在我需要你的帮助才能正确组织它。在哪里是添加我为此创建的函数或类的正确位置。 先谢谢你。

相关问题