使用带有cakePHP的STARTTLS命令通过SMTP发送电子邮件

时间:2013-06-26 08:53:10

标签: cakephp ssl smtp

我被要求使用外部SMTP服务器,其中包含以下说明:

“我们希望您以明文形式连接到端口25,然后发出STARTTLS以启动TLS / SSL。然后登录”

这可能吗?如果可行,我该怎么做?据我了解,这与在smtp连接数组中将TLS设置为true不同,是吗?

UPDATE 在这里向胜利倾斜。看看SmtpTransport.php中的代码,我可以看到它与评论中链接的规范(显然)匹配,但是块102-107似乎将主机设置为客户端或本地主机 - 我正在设置远程IP主机配置。我做错了吗?

蛋糕代码:

protected function _connect() {
    $this->_generateSocket();
    if (!$this->_socket->connect()) {
        throw new SocketException(__d('cake_dev', 'Unable to connect to SMTP server.'));
    }
    $this->_smtpSend(null, '220');

    if (isset($this->_config['client'])) {
        $host = $this->_config['client'];
    } elseif ($httpHost = env('HTTP_HOST')) {
        list($host) = explode(':', $httpHost);
    } else {
        $host = 'localhost';
    }

    try {
        $this->_smtpSend("EHLO {$host}", '250');
        if ($this->_config['tls']) {
            $this->_smtpSend("STARTTLS", '220');
            $this->_socket->enableCrypto('tls');
            $this->_smtpSend("EHLO {$host}", '250');
        }
    } catch (SocketException $e) {
        if ($this->_config['tls']) {
            throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.'));
        }
        try {
            $this->_smtpSend("HELO {$host}", '250');
        } catch (SocketException $e2) {
            throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection.'));
        }
    }
}

我的电子邮件配置:

    public $smtp = array(
    'transport' => 'Smtp',
    'from' => array('me@example.com' => 'my name'),
    'host' => 'secretipaddress',
    'port' => 25,
    'timeout' => 30,
    'username' => 'me@example.com',
    'password' => 'secretpassword',
    'client' => null,
    'log' => true,
    'tls' => true
);

1 个答案:

答案 0 :(得分:0)

启用&#t;'在您的配置文件app.php中为true。 它适用于我和cakephp 3.1 with gmail smtp