将php代码从使用cdo迁移到替代版本

时间:2019-06-25 14:04:29

标签: php exchange-server exchangewebservices cdo.message exchange-server-2016

我正在为一个客户进行项目。项目中的许多工具都通过交换服务器发送电子邮件。交换服务器更改为exchange2016。原始代码可用于cdo模式,但是exchange2016不再支持此功能,因此我需要找到另一种发送邮件的方式。

使用phpmailer尝试过,但不起作用

public function mailCOM($toAddress, $fromAddress = '', $subject = '<div>No subject</div>', $htmlBody = '', $textBody = '', $attachments = array()) {
    $this->toAddress = (array) $toAddress;
    $this->fromAddress = $fromAddress;

    if (empty($this->toAddress))
        echo "Need a to field to send the mail\n";
    else {
        $this->subject = $subject;
        $this->attachments = (array) $attachments;
        $this->htmlBody = $htmlBody;

        if ($textBody == '' && $htmlBody != '') {
            $htt = new html2text($htmlBody);
            $this->textBody = $htt->get_text();
        } else
            $this->textBody = $textBody;

            $this->message = new COM('CDO.Message');
            $this->message->Configuration = $this->configure();
            $this->message->From = $this->fromAddress;
            $this->message->Subject = $this->subject;
            if ($this->htmlBody != '')
                $this->message->HTMLBody = $this->htmlBody;
            $this->message->TextBody = $this->textBody;
            foreach ($this->attachments as $attachment){
                if(!empty($attachment) && $attachment!="")
                    $this->message->AddAttachment($attachment);
            }
            foreach ($this->toAddress as $address) {
                $this->message->To = $address;
                echo("\n - Sending mail to :" . $address . "\n");
                try {
                    $this->message->Send();
                } catch (com_exception $e) {
                    echo "\n catch: $e \n";
                    $this->error = $e;
                    $this->failed = true;
                }
            }
    }
}

protected function configure() {
    $messageCon = new COM('CDO.Configuration');
    //$messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/smtpserver'] = "s526smtp.p526.be";
    $messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/smtpserver'] = "10.198.127.10";
    $messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/smtpserverport'] = 25;
    $messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/sendusing'] = 2;
    $messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout'] = 60;
    $messageCon->Fields->Update();
    return $messageCon;
}

一种不同的工作方式,也许通过phpmailer?或EWS

0 个答案:

没有答案