一、安装PHPMailer
1)先给opencart项目安装vqmod
2)下载PHPMailerV2.1.zip
把PHPMailerV2.1.zip解压出来的文件夹 upload下的文件:
system/library/phpmailervqmod/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
到此完毕!