PHPMailer 解决 Opencart 发送邮件不成功

很多人反映Opencart后台配置邮件协议为MAIL和SMTP发送邮件都不成功,官方和中文论坛上也有人反映这个问题,但是我照着配置了很多次还是不行,然后我索性把Opencart中发送邮件的类库替换成PHPMailer,经测试完美解决。写一些实现过程,提供给需要的人参照。

1.下载下载PHPMailer,解压以后,将里面的class.phpmailer.php 和 class.smtp.php 复制到opencart的system/library/ 目录下面

2.修改system/startup.php,注释掉原来引入mail类代码,添加引入class.phpmailer.php

//by yuansir ;用phpmailer代替系统自带的mail和smtp的发送邮件方式
require_once(DIR_SYSTEM . 'library/class.phpmailer.php');
//require_once(DIR_SYSTEM . 'library/mail.php');

3.在Opencart中需要发送邮件的代码段中,将原来new Mail()的发送邮件相应注释掉,重写PHPMailer的发送邮件实现方法。比如我这里以 admin/controller/sale/contact.php为例:

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的还有其他方法可以去官方查阅。

转载自Ryan是菜鸟 | LNMP技术栈笔记

Comments

发表回复

Your email address will not be published. Name and email are required