opencart mail.php,使用swiftMailer替换Opencart内置的mail方法

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();

}

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值