服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > OpenCart教程 >

使用swiftMailer替换Opencart内置的mail方法

时间:2016-01-29 12:06来源: 作者: 点击:
swiftMailer是一个很强大的开源邮件库,本文以swiftsmail作为替代方案。解决Opencart SMTP设置后不能发送邮件的问题。 1.下载swiftMailer的源码包,解压到Opencart的\system\mail目录下,mail是新建的

swiftMailer是一个很强大的开源邮件库,本文以swiftsmail作为替代方案。解决Opencart SMTP设置后不能发送邮件的问题。

1.下载swiftMailer的源码包,解压到Opencart的\system\mail目录下,mail是新建的目录。解压后,mail下会有以下文件
classes文件夹,dependency_maps文件夹,swift_init.php等文件。

2.分别在要使用的config.php增加define(‘DIR_MAIL’, ‘你的Opencart路径/system/mail/’);

3.修改mail.php,使用以下代码替换掉mail.php

<?php
require_once DIR_MAIL.’/swift_required.php’;

final class Mail {
protected $to;
protected $from;
protected $sender;
protected $subject;
protected $text;
protected $html;
protected $attachments = array();
public $protocol = ‘mail’;
public $smtp_owner;
public $hostname;
public $username;
public $password;
public $port = 25;
public $timeout = 5;
public $newline = “\n”;
public $crlf = “\r\n”;
public $verp = FALSE;
public $parameter = ”;

public function setTo($to) {
$this->to = $to;
}

public function setBcc($bcc) {
$this->bcc = $bcc;
}

public function setFrom($from) {
$this->from = $from;
}

public function addheader($header, $value) {
$this->headers[$header] = $value;
}

public function setSender($sender) {
$this->sender = html_entity_decode($sender, ENT_COMPAT, ‘UTF-8′);
}

public function setSubject($subject) {
$this->subject = html_entity_decode($subject, ENT_COMPAT, ‘UTF-8′);
}

public function setText($text) {
$this->text = $text;
}

public function setHtml($html) {
$this->html = $html;
}

public function addAttachment($file, $filename = ”) {
if (!$filename) {
$filename = basename($file);
}

$this->attachments[] = array(
‘filename’ => $filename,
‘file’     => $file
);
}

public function send() {
if (!$this->to) {
exit(‘Error: E-Mail to required!’);
}

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

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

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

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

if ($this->protocol == ‘mail’) {
$transport=Swift_SmtpTransport::newInstance();
$message = Swift_Message::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
}else{
$transport = Swift_SmtpTransport::newInstance($this->hostname,$this->port);
$transport->setUsername( $this->username);
$transport->setPassword($this->password);
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
if($this->sender==”)
$message->setSender($this->username,$this->sender);
else
$message->setSender($this->username,$this->sender);
}

if (is_array($this->to)) {
$message->setTo($this->to);
} else {
$message->setTo(explode(‘,’,$this->to));
}

$message->setSubject($this->subject);
if(isset($this->sender))
$message->setFrom(array($this->from => $this->sender));
else
$message->setFrom(array($this->from => $this->sender));

$message->setFormat(‘multipart/mixed’);
$message->setReplyTo($this->from,$this->sender);

$message->setCharset(‘utf-8′);

foreach ($this->attachments as $attachment) {
if (file_exists($attachment['file'])) {
$message->attach(Swift_Attachment::fromPath($attachment['file'], ‘image/jpeg’)->setFilename( basename($attachment['filename'])));
}
}

if (!$this->html) {
$mail_body=$this->text;
} else {
$mail_body=$this->html;
}

if (!$this->html) {
$message->setBody($mail_body,’text/plain’);
} else {
$message->setBody($mail_body,’text/html’ );

}

try{
$mailer->send($message);
/*// bcc mail to alert mail of store owner
if($this->bcc==1){
$message->addTo($this->config->get(‘config_email’), $this->sender);
if ($this->config->get(‘config_alert_mail’)) {
$emails = explode(‘,’, $this->config->get(‘config_alert_emails’));
$message->setTo($emails);
}
$mailer->send($message);
}*/
}
catch (Swift_ConnectionException $e){
echo ‘There was a problem communicating with SMTP: ‘ . $e->getMessage();
}

}
}
?>

(责任编辑:最模板)
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容