码迷,mamicode.com
首页 > Web开发 > 详细

opencart安装和使用PHPMailer

时间:2015-08-27 13:00:45      阅读:962      评论:0      收藏:0      [点我收藏+]

标签:

一、安装PHPMailer

 1)先给opencart项目安装vqmod

  1. 下载最新版本: http://code.google.com/p/vqmod (目前最新版本是vqmod-2.5.1-standard)
  2. 把解压出来的文件放到opencart根目录下
  3. 执行 http://www.yoursite.com/vqmod/install
  4. 你能看到成功信息“VQMOD ALREADY INSTALLED!”. 如果没有,就检查3,4步

2)下载PHPMailerV2.1.zip

  把PHPMailerV2.1.zip解压出来的文件夹 upload下的文件:

  system/library/phpmailer
  vqmod/xml/PHPMailerV2.xml

  分别放到opencart项目对应的目录下:

  system/library/

  vqmod/xml/

二、修复PHPMailerV2.1.zip的PHPMailerV2.xml的2个bug,不知道为啥开发改插件的人没发现

1)PHPMailerV2.xml文件中添加的send()函数,编译生成会导致多出一个‘}‘,去掉126行的‘}‘;

2)xml中的host_name,port,username,password跟opencart项目的变量不对应,应该改成smtp_hostname,smtp_port,smtp_username,smtp_password;

旧xml

  1 <modification>
  2 
  3     <id>PHPMailer</id>
  4     <version>2.0</version>
  5     <vqmver>1.0.4</vqmver>
  6     <author>SpotOnSolutions.net</author>
  7     
  8     <file name="system/library/mail.php">
  9         <operation>
 10             <search position="before"><![CDATA[
 11             class Mail
 12             ]]></search>
 13             <add><![CDATA[include_once(DIR_SYSTEM . ‘library/phpmailer/class.phpmailer.php‘);]]></add>
 14         </operation>
 15 
 16         <operation>
 17             <search position="before"><![CDATA[
 18                 protected $subject;
 19             ]]></search>
 20             <add><![CDATA[    protected $readreceipt;]]></add>
 21         </operation>
 22 
 23         <operation>
 24             <search position="before"><![CDATA[
 25                 public function setSubject
 26             ]]></search>
 27             <add><![CDATA[    public function setReadReceipt($readreceipt) {
 28         $this->readreceipt = $readreceipt;
 29     }
 30             ]]></add>
 31         </operation>
 32 
 33         <operation>
 34             <search position="before"><![CDATA[public function send() {]]></search>
 35             <add><![CDATA[/*]]></add>
 36         </operation>
 37         
 38         <operation>
 39             <search position="bottom" offset="2"><![CDATA[
 40             public function send() {
 41             ]]></search>
 42             <add><![CDATA[*/
 43     public function send() {
 44         if (!$this->to) {
 45             trigger_error(‘Error: E-Mail to required!‘);
 46             exit();            
 47         }
 48 
 49         if (!$this->from) {
 50             trigger_error(‘Error: E-Mail from required!‘);
 51             exit();                    
 52         }
 53 
 54         if (!$this->sender) {
 55             trigger_error(‘Error: E-Mail sender required!‘);
 56             exit();                    
 57         }
 58 
 59         if (!$this->subject) {
 60             trigger_error(‘Error: E-Mail subject required!‘);
 61             exit();                    
 62         }
 63 
 64         if ((!$this->text) && (!$this->html)) {
 65             trigger_error(‘Error: E-Mail message required!‘);
 66             exit();                    
 67         }
 68         
 69         $mail  = new PHPMailer();
 70         $mail->CharSet = "UTF-8";
 71         
 72         if (is_array($this->to)) {
 73             foreach ($this->to as $toTmp){
 74                 $mail->AddAddress($toTmp);
 75             }
 76         } else {
 77             $mail->AddAddress($this->to);
 78         }
 79 
 80         if(!empty($this->readreceipt)) {
 81             $mail->ConfirmReadingTo = $this->readreceipt;
 82         }
 83 
 84         $mail->Subject = $this->subject;
 85         
 86         $mail->AddReplyTo($this->from, $this->sender);
 87         $mail->SetFrom($this->from, $this->sender);
 88         $mail->AddReplyTo($this->from, $this->sender);
 89         
 90         if (!$this->html) {
 91             $mail->Body = $this->text;
 92         } else {
 93             $mail->MsgHTML($this->html);
 94             if ($this->text) {
 95                 $mail->AltBody = $this->text;
 96             } else {
 97                 $mail->AltBody = ‘This is a HTML email and your email client software does not support HTML email!‘;
 98             }
 99         }
100 
101         foreach ($this->attachments as $attachment) {
102             if (file_exists($attachment[‘file‘])) {
103                 $mail->AddAttachment($attachment[‘file‘]);
104             }
105         }
106 
107         if ($this->protocol == ‘smtp‘) {
108             $mail->IsSMTP();
109             $mail->Host = $this->hostname;
110             $mail->Port = $this->port;
111             if($this->port == ‘587‘){
112                 $mail->SMTPAuth = true;
113                 $mail->SMTPSecure = "tls";
114             } elseif ($this->port == ‘465‘) {
115                 $mail->SMTPAuth = true;
116                 $mail->SMTPSecure = "ssl";
117             }
118             if (!empty($this->username)  && !empty($this->password)) {
119                 $mail->SMTPAuth = true;
120                 $mail->Host = $this->hostname;
121                 $mail->Username = $this->username;
122                 $mail->Password = $this->password;
123             }
124         }
125         $mail->Send();
126     }
127             ]]></add>
128         </operation>
129 
130     </file>
131     
132 </modification>

 

修复后的xml:

  1 <modification>
  2 
  3     <id>PHPMailer</id>
  4     <version>2.0</version>
  5     <vqmver>1.0.4</vqmver>
  6     <author>SpotOnSolutions.net</author>
  7     
  8     <file name="system/library/mail.php">
  9         <operation>
 10             <search position="before"><![CDATA[
 11             class Mail
 12             ]]></search>
 13             <add><![CDATA[include_once(DIR_SYSTEM . ‘library/phpmailer/class.phpmailer.php‘);]]></add>
 14         </operation>
 15 
 16         <operation>
 17             <search position="before"><![CDATA[
 18                 protected $subject;
 19             ]]></search>
 20             <add><![CDATA[    protected $readreceipt;]]></add>
 21         </operation>
 22 
 23         <operation>
 24             <search position="before"><![CDATA[
 25                 public function setSubject
 26             ]]></search>
 27             <add><![CDATA[    public function setReadReceipt($readreceipt) {
 28         $this->readreceipt = $readreceipt;
 29     }
 30             ]]></add>
 31         </operation>
 32 
 33         <operation>
 34             <search position="before"><![CDATA[public function send() {]]></search>
 35             <add><![CDATA[/*]]></add>
 36         </operation>
 37         
 38         <operation>
 39             <search position="bottom" offset="2"><![CDATA[
 40             public function send() {
 41             ]]></search>
 42             <add><![CDATA[*/
 43     public function send() {
 44         if (!$this->to) {
 45             trigger_error(‘Error: E-Mail to required!‘);
 46             exit();            
 47         }
 48 
 49         if (!$this->from) {
 50             trigger_error(‘Error: E-Mail from required!‘);
 51             exit();                    
 52         }
 53 
 54         if (!$this->sender) {
 55             trigger_error(‘Error: E-Mail sender required!‘);
 56             exit();                    
 57         }
 58 
 59         if (!$this->subject) {
 60             trigger_error(‘Error: E-Mail subject required!‘);
 61             exit();                    
 62         }
 63 
 64         if ((!$this->text) && (!$this->html)) {
 65             trigger_error(‘Error: E-Mail message required!‘);
 66             exit();                    
 67         }
 68         
 69         $mail  = new PHPMailer();
 70         $mail->CharSet = "UTF-8";
 71         
 72         if (is_array($this->to)) {
 73             foreach ($this->to as $toTmp){
 74                 $mail->AddAddress($toTmp);
 75             }
 76         } else {
 77             $mail->AddAddress($this->to);
 78         }
 79 
 80         if(!empty($this->readreceipt)) {
 81             $mail->ConfirmReadingTo = $this->readreceipt;
 82         }
 83 
 84         $mail->Subject = $this->subject;
 85         
 86         $mail->AddReplyTo($this->from, $this->sender);
 87         $mail->SetFrom($this->from, $this->sender);
 88         $mail->AddReplyTo($this->from, $this->sender);
 89         
 90         if (!$this->html) {
 91             $mail->Body = $this->text;
 92         } else {
 93             $mail->MsgHTML($this->html);
 94             if ($this->text) {
 95                 $mail->AltBody = $this->text;
 96             } else {
 97                 $mail->AltBody = ‘This is a HTML email and your email client software does not support HTML email!‘;
 98             }
 99         }
100 
101         foreach ($this->attachments as $attachment) {
102             if (file_exists($attachment[‘file‘])) {
103                 $mail->AddAttachment($attachment[‘file‘]);
104             }
105         }
106 
107         if ($this->protocol == ‘smtp‘) {
108             $mail->IsSMTP();
109             $mail->Host = $this->smtp_hostname;
110             $mail->Port = $this->smtp_port;
111             if($this->smtp_port == ‘587‘){
112                 $mail->SMTPAuth = true;
113                 $mail->SMTPSecure = "tls";
114             } elseif ($this->smtp_port == ‘465‘) {
115                 $mail->SMTPAuth = true;
116                 $mail->SMTPSecure = "ssl";
117             }
118             if (!empty($this->smtp_username)  && !empty($this->smtp_password)) {
119                 $mail->SMTPAuth = true;
120                 $mail->Host = $this->smtp_hostname;
121                 $mail->Username = $this->smtp_username;
122                 $mail->Password = $this->smtp_password;
123             }
124         }
125         $mail->Send();
126             ]]></add>
127         </operation>
128 
129     </file>
130     
131 </modification>

三、配置opencart后台的mail设置

系统设置->网店设置->编辑

协议选择SMTP,我使用的是smtp.126.com,用户名是你的邮箱 xxx@126.com。注意:你的邮箱必须是开通了smtp权限的才可以

技术分享

 

 

 

最重要的一点:常规 设置里的E-Mail必须填写成跟 邮件 上面的用户的邮箱一致,因为opencart发邮件所使用的是 常规 设置里的E-Mail

技术分享

 

到此完毕!

 

opencart安装和使用PHPMailer

标签:

原文地址:http://www.cnblogs.com/gulibao/p/4762822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!