1、安裝PHPMailerphp
1)先給opencart項目安裝vqmodhtml
2)下載PHPMailerV2.1.zip函數
把PHPMailerV2.1.zip解壓出來的文件夾 upload下的文件:ui
system/library/phpmailer
vqmod/xml/PHPMailerV2.xmlthis
分別放到opencart項目對應的目錄下:google
system/library/spa
vqmod/xml/.net
2、修復PHPMailerV2.1.zip的PHPMailerV2.xml的2個bug,不知道爲啥開發改插件的人沒發現插件
1)PHPMailerV2.xml文件中添加的send()函數,編譯生成會致使多出一個'}',去掉126行的'}';code
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>
3、配置opencart後臺的mail設置
系統設置->網店設置->編輯
協議選擇SMTP,我使用的是smtp.126.com,用戶名是你的郵箱 xxx@126.com。注意:你的郵箱必須是開通了smtp權限的才能夠
最重要的一點:常規 設置裏的E-Mail必須填寫成跟 郵件 上面的用戶的郵箱一致,由於opencart發郵件所使用的是 常規 設置裏的E-Mail
到此完畢!