【问题标题】:Prestashop 1.6 and SwiftMailer with PHP version 5.6 - fsockopen errorPrestashop 1.6 和带有 PHP 5.6 版的 SwiftMailer - fsockopen 错误
【发布时间】:2019-05-31 10:00:13
【问题描述】:

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

Swift 错误: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 吗?

Prestashop 1.6.1.1 到 1.6.1.4 受到影响。

【问题讨论】:

  • 你找到解决办法了吗?

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


【解决方案1】:

这对我有用:

  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));

[...]

我希望这会有所帮助!

【讨论】:

  • 我的文件结构不是这样的。事实上,任何地方都没有 classes 文件夹,也没有 StreamBuffer.php 文件。这是 Prestashop 1.6 附带的 SwiftMailer 版本。
【解决方案2】:

我完全删除了 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);

【讨论】:

    猜你喜欢
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 2014-11-08
    • 2015-03-30
    • 2020-12-04
    • 2015-10-26
    相关资源
    最近更新 更多