您的位置:首页 > 编程语言 > PHP开发

opencart安装和使用PHPMailer

2015-08-27 11:36 731 查看
一、安装PHPMailer

1)先给opencart项目安装vqmod

下载最新版本: http://code.google.com/p/vqmod (目前最新版本是vqmod-2.5.1-standard)

把解压出来的文件放到opencart根目录下

执行 http://www.yoursite.com/vqmod/install

你能看到成功信息“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

<modification>

<id>PHPMailer</id>
<version>2.0</version>
<vqmver>1.0.4</vqmver>
<author>SpotOnSolutions.net</author>

<file name="system/library/mail.php">
<operation>
<search position="before"><![CDATA[
class Mail
]]></search>
<add><![CDATA[include_once(DIR_SYSTEM . 'library/phpmailer/class.phpmailer.php');]]></add>
</operation>

<operation>
<search position="before"><![CDATA[
protected $subject;
]]></search>
<add><![CDATA[    protected $readreceipt;]]></add>
</operation>

<operation>
<search position="before"><![CDATA[
public function setSubject
]]></search>
<add><![CDATA[    public function setReadReceipt($readreceipt) {
$this->readreceipt = $readreceipt;
}
]]></add>
</operation>

<operation>
<search position="before"><![CDATA[public function send() {]]></search>
<add><![CDATA[/*]]></add>
</operation>

<operation>
<search position="bottom" offset="2"><![CDATA[
public function send() {
]]></search>
<add><![CDATA[*/
public function send() {
if (!$this->to) {
trigger_error('Error: E-Mail to required!');
exit();
}

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

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

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

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

$mail  = new PHPMailer();
$mail->CharSet = "UTF-8";

if (is_array($this->to)) {
foreach ($this->to as $toTmp){
$mail->AddAddress($toTmp);
}
} else {
$mail->AddAddress($this->to);
}

if(!empty($this->readreceipt)) {
$mail->ConfirmReadingTo = $this->readreceipt;
}

$mail->Subject = $this->subject;

$mail->AddReplyTo($this->from, $this->sender);
$mail->SetFrom($this->from, $this->sender);
$mail->AddReplyTo($this->from, $this->sender);

if (!$this->html) {
$mail->Body = $this->text;
} else {
$mail->MsgHTML($this->html);
if ($this->text) {
$mail->AltBody = $this->text;
} else {
$mail->AltBody = 'This is a HTML email and your email client software does not support HTML email!';
}
}

foreach ($this->attachments as $attachment) {
if (file_exists($attachment['file'])) {
$mail->AddAttachment($attachment['file']);
}
}

if ($this->protocol == 'smtp') {
$mail->IsSMTP();
$mail->Host = $this->hostname;
$mail->Port = $this->port;
if($this->port == '587'){
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
} elseif ($this->port == '465') {
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
}
if (!empty($this->username)  && !empty($this->password)) {
$mail->SMTPAuth = true;
$mail->Host = $this->hostname;
$mail->Username = $this->username;
$mail->Password = $this->password;
}
}
$mail->Send();
}
]]></add>
</operation>

</file>

</modification>


修复后的xml:

<modification>

<id>PHPMailer</id>
<version>2.0</version>
<vqmver>1.0.4</vqmver>
<author>SpotOnSolutions.net</author>

<file name="system/library/mail.php">
<operation>
<search position="before"><![CDATA[
class Mail
]]></search>
<add><![CDATA[include_once(DIR_SYSTEM . 'library/phpmailer/class.phpmailer.php');]]></add>
</operation>

<operation>
<search position="before"><![CDATA[
protected $subject;
]]></search>
<add><![CDATA[    protected $readreceipt;]]></add>
</operation>

<operation>
<search position="before"><![CDATA[
public function setSubject
]]></search>
<add><![CDATA[    public function setReadReceipt($readreceipt) {
$this->readreceipt = $readreceipt;
}
]]></add>
</operation>

<operation>
<search position="before"><![CDATA[public function send() {]]></search>
<add><![CDATA[/*]]></add>
</operation>

<operation>
<search position="bottom" offset="2"><![CDATA[
public function send() {
]]></search>
<add><![CDATA[*/
public function send() {
if (!$this->to) {
trigger_error('Error: E-Mail to required!');
exit();
}

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

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

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

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

$mail  = new PHPMailer();
$mail->CharSet = "UTF-8";

if (is_array($this->to)) {
foreach ($this->to as $toTmp){
$mail->AddAddress($toTmp);
}
} else {
$mail->AddAddress($this->to);
}

if(!empty($this->readreceipt)) {
$mail->ConfirmReadingTo = $this->readreceipt;
}

$mail->Subject = $this->subject;

$mail->AddReplyTo($this->from, $this->sender);
$mail->SetFrom($this->from, $this->sender);
$mail->AddReplyTo($this->from, $this->sender);

if (!$this->html) {
$mail->Body = $this->text;
} else {
$mail->MsgHTML($this->html);
if ($this->text) {
$mail->AltBody = $this->text;
} else {
$mail->AltBody = 'This is a HTML email and your email client software does not support HTML email!';
}
}

foreach ($this->attachments as $attachment) {
if (file_exists($attachment['file'])) {
$mail->AddAttachment($attachment['file']);
}
}

if ($this->protocol == 'smtp') {
$mail->IsSMTP();
$mail->Host = $this->smtp_hostname;
$mail->Port = $this->smtp_port;
if($this->smtp_port == '587'){
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
} elseif ($this->smtp_port == '465') {
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
}
if (!empty($this->smtp_username)  && !empty($this->smtp_password)) {
$mail->SMTPAuth = true;
$mail->Host = $this->smtp_hostname;
$mail->Username = $this->smtp_username;
$mail->Password = $this->smtp_password;
}
}
$mail->Send();
]]></add>
</operation>

</file>

</modification>


三、配置opencart后台的mail设置

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

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



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



到此完毕!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: