最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > OpenCart > OpenCart教程 >

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

时间:2014-07-24 23:11来源:未知 作者:最模板zuimoban 点击:
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();
}

}
}
?>

(责任编辑:最模板)
------分隔线----------------------------
栏目列表
推荐内容