Prestashop 1.6和SwiftMailer(PHP版本5.6)-fsockopen错误

时间:2019-05-31 10:00:14

标签: php smtp ssl-certificate prestashop-1.6 swiftmailer

我在使用PS1.6随附的Swiftmailer版本发送邮件时遇到问题

快速错误:SMTP连接无法启动[ssl://mail.wineofthemonth.co.za:465]:fsockopen返回错误号0和错误字符串\'\'

这是由于以下事实:自PHP 5.6起,verify_peer选项默认设置为true。

现在,我发现了各种有关如何将验证对等体设置为false的说明:

$https['ssl']['verify_peer'] = FALSE;
$https['ssl']['verify_peer_name'] = FALSE;

问题在于,在Prestashop中使用的Swiftmailer版本中没有位置进行设置。

所以我的问题是,在class / mail.php或其他任何地方,我会将verify_peer设置为false?

这是我认为与此相关的mail.php中的行:

$connection = new Swift_Connection_SMTP(
                    $configuration['PS_MAIL_SERVER'],
                    $configuration['PS_MAIL_SMTP_PORT'],
                    $configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'ssl' ? Swift_Connection_SMTP::ENC_SSL : (($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'tls' ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_OFF))
                );

还有一个sendMailtest方法,可以使它更加清晰:

public static function sendMailTest($smtpChecked, $smtpServer, $content, $subject, $type, $to, $from, $smtpLogin, $smtpPassword, $smtpPort = 25, $smtpEncryption)
    {
        $result = false;
        try {
            if ($smtpChecked)
            {
                $smtp = new Swift_Connection_SMTP($smtpServer, $smtpPort, ($smtpEncryption == 'off') ?
                    Swift_Connection_SMTP::ENC_OFF : (($smtpEncryption == 'tls') ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_SSL));
                $smtp->setUsername($smtpLogin);
                $smtp->setpassword($smtpPassword);
                $smtp->setTimeout(5);
                $swift = new Swift($smtp, Configuration::get('PS_MAIL_DOMAIN'));
            }
            else
                $swift = new Swift(new Swift_Connection_NativeMail(), Configuration::get('PS_MAIL_DOMAIN'));

            $message = new Swift_Message($subject, $content, $type);

            if ($swift->send($message, $to, $from))
                $result = true;

            $swift->disconnect();
        } catch (Swift_ConnectionException $e) {
            $result = $e->getMessage();
        } catch (Swift_Message_MimeException $e) {
            $result = $e->getMessage();
        }

        return $result;
    } 

或者,在任何conf或ini文件中,我可以将verify_peer设置为false吗?

2 个答案:

答案 0 :(得分:0)

这对我有用:

  1. 打开文件/tools/swift/classes/Swift/Transport/StreamBuffer.php
  2. 263的第$this->_stream =行上,添加以下两行:

    $options['ssl']['verify_peer'] = FALSE;

    $options['ssl']['verify_peer_name'] = FALSE;

代码现在应如下所示:

[...]

$options = array();
if (!empty($this->_params['sourceIp'])) {
    $options['socket']['bindto'] = $this->_params['sourceIp'].':0';
}
$options['ssl']['verify_peer'] = FALSE;
$options['ssl']['verify_peer_name'] = FALSE;
$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, stream_context_create($options));

[...]

我希望这会有所帮助!

答案 1 :(得分:0)

我完全删除了 Prestashop 1.6.1 附带的 Swiftmailer 3.3.2 版本,并使用 composer 安装了最新版本。 然后我修改了文件 classes/Mail.php 如下。 diff 只包含修改后的行。

- include_once(_PS_SWIFT_DIR_.'Swift.php');
- include_once(_PS_SWIFT_DIR_.'Swift/Connection/SMTP.php');
- include_once(_PS_SWIFT_DIR_.'Swift/Connection/NativeMail.php');
- include_once(_PS_SWIFT_DIR_.'Swift/Plugin/Decorator.php');
+ require_once dirname(__FILE__) . '/../vendor/autoload.php';

- $to_list = new Swift_RecipientList();
+ $message = new Swift_Message();

- $to_list->addTo($addr, $to_name);
+ $message->addTo($addr, $to_name);
(...) similar three times

- $connection = new Swift_Connection_SMTP(
-     $configuration['PS_MAIL_SERVER'],
-     $configuration['PS_MAIL_SMTP_PORT'],
-     $configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'ssl' ? Swift_Connection_SMTP::ENC_SSL : (($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'tls' ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_OFF))
- );
+ $connection = new Swift_SmtpTransport(
+     $configuration['PS_MAIL_SERVER'],
+     $configuration['PS_MAIL_SMTP_PORT'],
+     $configuration['PS_MAIL_SMTP_ENCRYPTION']
+ );

- $connection = new Swift_Connection_NativeMail();
+ $swift = new Swift_Mailer($connection);

- $message = new Swift_Message($subject);
+ $message->setSubject($subject);

- $message->headers->setEncoding('Q');

- $template_vars['{shop_logo}'] = $message->attach(new Swift_Message_EmbeddedFile(new Swift_File($logo), null, ImageManager::getMimeTypeByExtension($logo)));
+ $template_vars['{shop_logo}'] = $message->embed(Swift_Image::fromPath($logo));

- $swift->attachPlugin(new Swift_Plugin_Decorator(array($to_plugin => $template_vars)), 'decorator');
- if ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_TEXT) {
-     $message->attach(new Swift_Message_Part($template_txt, 'text/plain', '8bit', 'utf-8'));
- }
- if ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_HTML) {
-     $message->attach(new Swift_Message_Part($template_html, 'text/html', '8bit', 'utf-8'));
- }
+ $swift->registerPlugin(new Swift_Plugins_DecoratorPlugin(array($to_plugin => $template_vars)));
+ if ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_TEXT) {
+     $message->addPart($template_txt, 'text/plain', 'utf-8');
+ }
+ if ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_HTML) {
+     $message->addPart($template_html, 'text/html', 'utf-8');
+ }

- $message->attach(new Swift_Message_Attachment($attachment['content'], $attachment['name'], $attachment['mime']));
+ $message->attach(new Swift_Attachment($attachment['content'], $attachment['name'], $attachment['mime']));

- $send = $swift->send($message, $to_list, new Swift_Address($from, $from_name));
- $swift->disconnect();

+ $message->setFrom(array($from => $from_name));
+ $send = $swift->send($message);

- foreach (array_merge($to_list->getTo(), $to_list->getCc(), $to_list->getBcc()) as $recipient) {
-    /** @var Swift_Address $recipient */
-     $mail->id = null;
-     $mail->recipient = substr($recipient->getAddress(), 0, 126);
-     $mail->add();
- }
+ foreach (array_merge($message->getTo() ?: [], $message->getCc() ?: [], $message->getBcc() ?: []) as $recipient_mail => $recipient_name) {
+     $mail->id = null;
+     $mail->recipient = $recipient_mail;
+     $mail->add();
+ }

- public static function sendMailTest($smtpChecked, $smtpServer, $content, $subject, $type, $to, $from, $smtpLogin, $smtpPassword, $smtpPort = 25, $smtpEncryption)
+ public static function sendMailTest($smtpChecked, $smtpServer, $content, $subject, $type, $to, $from, $smtpLogin, $smtpPassword, $smtpPort = 25, $smtpEncryption)
+ {
+     $result = false;
+     try {
+         if ($smtpChecked) {
+             $smtp = new Swift_SmtpTransport($smtpServer, $smtpPort, $smtpEncryption);
+             $smtp->setUsername($smtpLogin);
+             $smtp->setpassword($smtpPassword);
+             $smtp->setTimeout(5);
+             $swift = new Swift_Mailer($smtp);
+         } else {
+             $swift = new Swift_Mailer(new Swift_SmtpTransport('localhost', 25));
+         }
+ 
+         $message = new Swift_Message();
+         $message->setFrom($from)->setTo($to)->setSubject($subject)->setBody($content);
+ 
+         if ($swift->send($message)) {
+             $result = true;
+         }
+ 
+     } catch (Swift_SwiftException $e) {
+         $result = $e->getMessage();
+     }
+ 
+     return $result;
+ }

- return vsprintf("<%s.%d.%s@%s>", $midparams);
+ return vsprintf("%s.%d.%s@%s", $midparams);