PHPMailer解决OpenCart发送邮件不成功

很多人反映Opencart后台配置邮件协议为MAIL和SMTP发送邮件都不成功,官方和中文论坛上也有人反映这个问题,本文索性把Opencart中发送邮件的类库替换成PHPMailer,经测试完美解决。

  1. 下载PHPMailer,解压以后,将里面的 class.phpmailer.phpclass.smtp.php 复制到Opencart的system/library/PHPMailer 目录下面
  1. 修改system/startup.php,注释掉原来引入mail类代码,添加引入class.phpmailer.php
1
2
3
//用phpmailer代替系统自带的mail和smtp的发送邮件方式
require_once(DIR_SYSTEM . 'library/PHPMailer/class.phpmailer.php');
//require_once(DIR_SYSTEM . 'library/mail.php');
  1. 在Opencart中需要发送邮件的代码段中,将原来new Mail()的发送邮件相应注释掉,重写PHPMailer的发送邮件实现方法。比如我这里以 admin/controller/sale/contact.php为例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
foreach ($emails as $email) {
//$mail = new Mail();
//$mail->protocol = $this>config>get('config_mail_protocol');
//$mail->parameter = $this>config>get('config_mail_parameter');
//$mail->hostname = $this>config>get('config_smtp_host');
//$mail->username = $this>config>get('config_smtp_username');
//$mail->password = $this>config>get('config_smtp_password');
//$mail->port = $this>config>get('config_smtp_port');
//$mail->timeout = $this>config>get('config_smtp_timeout');
//$mail->setTo($email);
//$mail->setFrom($this>config>get('config_email'));
//$mail->setSender($store_name);
//$mail->setSubject($this>request>post['subject']);
//
//foreach ($attachments as $attachment) {
// $mail->addAttachment($attachment['path'], $attachment['filename']);
//}
//
//$mail->setHtml($message);
//$mail->send();

/**
* 用phpmailer 发送邮件
* @author yuansir
*/
ob_start();
error_reporting(0);
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
$mail->IsSMTP();
$mail->Host = $this>config>get('config_smtp_host'); // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = $this>config>get('config_smtp_host');
$mail->Port = $this>config>get('config_smtp_port');
$mail->Username = $this>config>get('config_smtp_username');
$mail->Password = $this>config>get('config_smtp_password');
$mail->SetFrom($this>config>get('config_email'), $store_name); //发件人邮箱和发件人
$mail->Subject = $this>request>post['subject']; //邮件主题
$mail->MsgHTML($message); //邮件内容
$mail->AddAddress($email, ""); //收件人邮箱和收件人
foreach ($attachments as $attachment) {
$mail->AddAttachment($attachment['path']); //添加附件
}
$mail->Send(); //发送
}

然后在后台配置邮件发送SMTP即可,接下来要做的就是替换全站涉及到发送邮件的代码段为PHPMailer的代码,关于PHPMailer的还有其他方法可以去官方查阅。

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :