使用swiftMailer替换Opencart内置的mail

Opencart中文问题交流,Opencart bug

版主: Huagu

使用swiftMailer替换Opencart内置的mail

帖子Alex » 2011-08-09 14:17

swiftMailer是一个很强大的开源邮件库,本文以swiftsmail作为替代方案。解决Opencart SMTP设置后不能发送邮件的问题。

1.下载swiftMailer的源码包,解压到Opencart的\system\mail目录下,mail是新建的目录。解压后,mail下会有以下文件
classes文件夹,dependency_maps文件夹,swift_init.php等文件。

2.分别在要使用的config.php增加define('DIR_MAIL', '你的Opencart路径/system/mail/');

3.修改mail.php,使用以下代码替换掉mail.php

代码: 全选
<?php
require_once DIR_MAIL.'/swift_required.php';

final class Mail {
   protected $to;
   protected $from;
   protected $sender;
   protected $subject;
   protected $text;
   protected $html;
   protected $attachments = array();
   public $protocol = 'mail';
   public $smtp_owner;
   public $hostname;
   public $username;
   public $password;
   public $port = 25;
   public $timeout = 5;
   public $newline = "\n";
   public $crlf = "\r\n";
   public $verp = FALSE;
   public $parameter = '';

   
   public function setTo($to) {
      $this->to = $to;
   }

   public function setBcc($bcc) {
      $this->bcc = $bcc;
   }

   public function setFrom($from) {
      $this->from = $from;
   }

   public function addheader($header, $value) {
      $this->headers[$header] = $value;
   }

   public function setSender($sender) {
      $this->sender = html_entity_decode($sender, ENT_COMPAT, 'UTF-8');
   }

   public function setSubject($subject) {
      $this->subject = html_entity_decode($subject, ENT_COMPAT, 'UTF-8');
   }

   public function setText($text) {
      $this->text = $text;
   }

   public function setHtml($html) {
      $this->html = $html;
   }

   public function addAttachment($file, $filename = '') {
      if (!$filename) {
         $filename = basename($file);
      }

      $this->attachments[] = array(
         'filename' => $filename,
         'file'     => $file
      );
   }

   public function send() {
      if (!$this->to) {
         exit('Error: E-Mail to required!');
      }

      if (!$this->from) {
         exit('Error: E-Mail from required!');
      }

      if (!$this->sender) {
         exit('Error: E-Mail sender required!');
      }

      if (!$this->subject) {
         exit('Error: E-Mail subject required!');
      }

      if ((!$this->text) && (!$this->html)) {
         exit('Error: E-Mail message required!');
      }

      if ($this->protocol == 'mail') {
          $transport=Swift_SmtpTransport::newInstance();
          $message = Swift_Message::newInstance();
          $mailer = Swift_Mailer::newInstance($transport);
      }else{
         $transport = Swift_SmtpTransport::newInstance($this->hostname,$this->port);
          $transport->setUsername( $this->username);
          $transport->setPassword($this->password);
          $mailer = Swift_Mailer::newInstance($transport);
          $message = Swift_Message::newInstance();
          if($this->sender=='')
             $message->setSender($this->username,$this->sender);
          else
            $message->setSender($this->username,$this->sender);
      }

      if (is_array($this->to)) {
          $message->setTo($this->to);
      } else {
          $message->setTo(explode(',',$this->to));
      }

      $message->setSubject($this->subject);
      if(isset($this->sender))
         $message->setFrom(array($this->from => $this->sender));
      else
         $message->setFrom(array($this->from => $this->sender));

      $message->setFormat('multipart/mixed');
      $message->setReplyTo($this->from,$this->sender);

        $message->setCharset('utf-8');

        foreach ($this->attachments as $attachment) {
         if (file_exists($attachment['file'])) {
            $message->attach(Swift_Attachment::fromPath($attachment['file'], 'image/jpeg')->setFilename( basename($attachment['filename'])));
         }
      }

      if (!$this->html) {
         $mail_body=$this->text;
      } else {
         $mail_body=$this->html;
      }


      if (!$this->html) {
         $message->setBody($mail_body,'text/plain');
      } else {
         $message->setBody($mail_body,'text/html' );

      }

      try{
         $mailer->send($message);
         /*// bcc mail to alert mail of store owner
         if($this->bcc==1){
            $message->addTo($this->config->get('config_email'), $this->sender);
            if ($this->config->get('config_alert_mail')) {
               $emails = explode(',', $this->config->get('config_alert_emails'));
               $message->setTo($emails);
            }
            $mailer->send($message);
         }*/
      }
      catch (Swift_ConnectionException $e){
           echo 'There was a problem communicating with SMTP: ' . $e->getMessage();
      }

   }
}
?>


4.配置好SMTP后,本地测试成功。
更多测试可到www.tylsoft.com/demo/lot18
深圳天宇林 http://www.tylsoft.com ,专业电子商务方案提供商
---------------------------------------------------------------------------------------
非商业用户谢绝私下邮件,qq联系,请在论坛上发帖交流。
Alex
 
帖子: 720
注册: 2009-12-29 13:05

Re: 使用swiftMailer替换Opencart内置的mail

帖子ioio325 » 2011-08-09 20:46

好东西,帮忙顶一下 :lol:
ioio325
 
帖子: 23
注册: 2011-04-06 13:33

Re: 使用swiftMailer替换Opencart内置的mail

帖子befree » 2011-08-22 3:51

感谢分享~~ alex

我用qq及163邮箱smtp在本地发送成功

但是用gmail就不行(地址:smtp.gmail.com ,端口465,其他正常设置) 查了些资料 gmail写明(请确保已经在您的邮件客户端中启用了 SMTP 的 SSL)还有什么 “您是否将 SMTP 服务器设为"允许身份验证"”
原文在这里https://mail.google.com/support/bin/answer.py?hl=zh-Hans&answer=78775

请问 Alex 有办法能够 用gmail的smtp发信?

thanks~~
befree
 
帖子: 1
注册: 2011-08-22 3:46

Re: 使用swiftMailer替换Opencart内置的mail

帖子Alex » 2011-08-22 16:18

gmail 没测试过。
深圳天宇林 http://www.tylsoft.com ,专业电子商务方案提供商
---------------------------------------------------------------------------------------
非商业用户谢绝私下邮件,qq联系,请在论坛上发帖交流。
Alex
 
帖子: 720
注册: 2009-12-29 13:05

Re: 使用swiftMailer替换Opencart内置的mail

帖子Alex » 2011-08-22 16:22

btw,gmail未知的问题比较多,比如GFW什么的都会影响到。所以我一直没去测试gmail,而用qq的比较多。
深圳天宇林 http://www.tylsoft.com ,专业电子商务方案提供商
---------------------------------------------------------------------------------------
非商业用户谢绝私下邮件,qq联系,请在论坛上发帖交流。
Alex
 
帖子: 720
注册: 2009-12-29 13:05

Re: 使用swiftMailer替换Opencart内置的mail

帖子acor » 2011-11-01 14:50

使用 swiftsmail 依旧不能发送成功!~!!~!~!~

smtp.qq.com
号码@qq.com
密码
465

============================
测试发送为
Fatal error: Uncaught exception 'Swift_IoException' with message 'Connection to smtp.qq.com:465 Timed Out' in F:\web\system\mail\classes\Swift\Transport\StreamBuffer.php:174 Stack trace: #0 F:\web\system\mail\classes\Swift\Transport\AbstractSmtpTransport.php(438): Swift_Transport_StreamBuffer->readLine(0) #1 F:\web\system\mail\classes\Swift\Transport\AbstractSmtpTransport.php(315): Swift_Transport_AbstractSmtpTransport->_getFullResponse(0) #2 F:\web\system\mail\classes\Swift\Transport\AbstractSmtpTransport.php(123): Swift_Transport_AbstractSmtpTransport->_readGreeting() #3 F:\web\system\mail\classes\Swift\Mailer.php(79): Swift_Transport_AbstractSmtpTransport->start() #4 F:\web\system\library\mail.php(143): Swift_Mailer->send(Object(Swift_Message)) #5 F:\shenzhens in F:\web\system\mail\classes\Swift\Transport\StreamBuffer.php on line 174
acor
 
帖子: 5
注册: 2011-10-25 22:37

Re: 使用swiftMailer替换Opencart内置的mail

帖子acor » 2011-11-01 14:52

请教 。。。。
应该跟版本没有关系的啊。。我使用的opencart v1.5.1.3.1
acor
 
帖子: 5
注册: 2011-10-25 22:37

Re: 使用swiftMailer替换Opencart内置的mail

帖子Alex » 2011-11-02 8:29

和版本没关系。Swift只支持SMTP发送邮件
深圳天宇林 http://www.tylsoft.com ,专业电子商务方案提供商
---------------------------------------------------------------------------------------
非商业用户谢绝私下邮件,qq联系,请在论坛上发帖交流。
Alex
 
帖子: 720
注册: 2009-12-29 13:05

Re: 使用swiftMailer替换Opencart内置的mail

帖子raylin68914 » 2011-12-02 19:09

Alex,您好:
  請問到那裡下載swiftMailer的源碼包?是否可以給我路徑,謝謝您!
raylin68914
 
帖子: 2
注册: 2011-09-24 11:30

Re: 使用swiftMailer替换Opencart内置的mail

帖子Alex » 2011-12-05 16:15

http://swiftmailer.org/

这是他们的官方网站
深圳天宇林 http://www.tylsoft.com ,专业电子商务方案提供商
---------------------------------------------------------------------------------------
非商业用户谢绝私下邮件,qq联系,请在论坛上发帖交流。
Alex
 
帖子: 720
注册: 2009-12-29 13:05

下一页

回到 Opencart 问题交流

在线用户

正在浏览此版面的用户:没有注册用户 和 1 位游客

cron