OPENCART无法发邮件-MAIL使用SMTP类文件

通过清心醉

OPENCART无法发邮件-MAIL使用SMTP类文件

最近发现OPENCART发邮件系统有点问题,不知道是配置问题还是怎么的,老是出现错误.

由于邮件系统都是使用/system/library/mail.php里的mail函数

而默认使用方法是:

$mail = new Mail();
$mail->protocol = $this->config->get(‘config_mail_protocol’);
$mail->parameter = $this->config->get(‘config_mail_parameter’);
$mail->smtp_hostname = $this->config->get(‘config_mail_smtp_hostname’);
$mail->smtp_username = $this->config->get(‘config_mail_smtp_username’);
$mail->smtp_password = html_entity_decode($this->config->get(‘config_mail_smtp_password’), ENT_QUOTES, ‘UTF-8’);
$mail->smtp_port = $this->config->get(‘config_mail_smtp_port’);
$mail->smtp_timeout = $this->config->get(‘config_mail_smtp_timeout’);

$mail->setTo($this->request->post[’email’]);
$mail->setFrom($this->config->get(‘config_email’));
$mail->setSender(html_entity_decode($this->config->get(‘config_name’), ENT_QUOTES, ‘UTF-8′));
$mail->setSubject($subject);
$mail->setText($message);
$mail->send();

在类里对成员protocol 进行判断使用MSTP还是SENDMAIL方法

为了保证OPENCART的核心可以正常的使用,作者修改了SMTP的类方法,但只能使用SMTP的类操作,暂未对SENDMAIL进行操作.

使用方法和默认的操作相同

$mail=new mail();
$mail->parameter=’13823819185@139.com’; #抄送一份
$mail->smtp_hostname=’smtp.ym.163.com’;  #邮箱地址
$mail->smtp_username=’admin@qingxinzui.com’;  #用户名
$mail->smtp_password=’************’;  #密码
$mail->smtp_port=’25’; #端口
$mail->smtp_timeout=’30’; #超时施加 //默认5,进行重写
$mailto=”收件人@qingxinzui.com”;
$mailtitle=”测试邮件”; //邮件标题
$mailbody =”<center><div style=’font-size:24px;color:red;’>标题</div></center>”;
$mailbody.=”<table align=’center’ width=’50%’ border=’1′ cellpadding=’2′ cellspacing=’0′>”;
$mailbody.=”<tr><td><center>联系人</center></td><td>名字</td></tr>”;
$mailbody.=”<tr><td><center>联系电话</center></td><td>电话</td></tr>”;
$mailbody.=”<tr><td><center>联系地址</center></td><td>地址</td></tr>”;
$mailbody.=”<tr><td><center>店铺名称</center></td><td>店名</td></tr>”;
$mailbody.=”</table>”;
$mail->setTo($mailto); //收件人
$mail->setFrom($mail->smtp_username); //发件人
$mail->setSubject(html_entity_decode($mailtitle, ENT_QUOTES, ‘UTF-8′));
$mail->setText($mailbody);
$mail->send();

以下为class mail类的封装:

class mail
{
protected $to; //收件人
protected $from; //发件人
protected $subject;
protected $text;
public $protocol;
public $smtp_hostname; #主机地址 smtp.qq.com
public $smtp_username; #用户名
public $smtp_password; #密码
public $smtp_port;  #邮件端口
public $smtp_timeout; #超时时间
public $newline;
public $verp;
public $parameter; //#附加发送
private $ishostname; //主机值保留localhost
public $log_file;
public $debug;
public $auth; //验证
private $sock;

function __construct()
{
$this->smtp_port=25; //构造方法给予端口赋默认值
$this->smtp_timeout=30; //构造方法给予超时赋默认值
$this->ishostname  = “localhost”; //默认值,禁止外部修改
$this->debug=TRUE; //开启调试
$this->log_file=””; //日志文件
$this->sock=FALSE;
$this->protocol=’SMTP’; #邮件类型
$this->auth=TRUE;
$this->newline = “\n”;
$this->verp = false;
$this->parameter=”;
}

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

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

public function setReplyTo($reply_to) {
$this->reply_to = $reply_to;
}

public function setSubject($subject) {
$this->subject = $subject;
}

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

function send()
{
$this->sendmail($this->to, $this->from, $this->subject,$this->text,’HTML’,$this->parameter);
}

private function sendmail($to, $from, $subject = “”, $body = “”, $mailtype, $cc = “”, $bcc = “”, $additional_headers = “”)
{
//收件人,发件人,邮件标题,邮件内容,邮件类型,抄送等参数
$header= “”;
$mail_from = $this->get_address($this->strip_comment($from));
$body= mb_ereg_replace(“(^|(\r\n))(\\.)”, “\\1.\\3”, $body);
$header .= “MIME-Version:1.0\r\n”;
if ($mailtype == “HTML”) {
$header .= ‘Content-type: text/html; charset=utf-8’ . “\r\n”;
}
$header .= “To: ” . $to . “\r\n”;
if ($cc != “”) {
$header .= “Cc: ” . $cc . “\r\n”;
}
$header .= “From: ” .$from . “\r\n”;
$header .= “Subject: ” . $subject . “\r\n”;
$header .= $additional_headers;
$header .= “Date: ” . date(“r”) . “\r\n”;
$header .= “X-Mailer:By (PHP/” . phpversion() . “)\r\n”;
list($msec, $sec) = explode(” “, microtime());
$header .= “Message-ID: <” . date(“YmdHis”, $sec) . “.” . ($msec * 1000000) . “.” . $mail_from . “>\r\n”;
$TO = explode(“,”, $this->strip_comment($to));

if ($cc != “”) {
$TO = array_merge($TO, explode(“,”, $this->strip_comment($cc))); //合并一个或多个数组
}

if ($bcc != “”) {
$TO = array_merge($TO, explode(“,”, $this->strip_comment($bcc)));
}
$sent = TRUE;
foreach ($TO as $rcpt_to) {
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write(“Error: Cannot send email to ” . $rcpt_to . “\n”);
$sent = FALSE;
continue;

}
if ($this->smtp_send($this->ishostname, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write(“E-mail has been sent to <” . $rcpt_to . “>\n”);
} else {
$this->log_write(“Error: Cannot send email to <” . $rcpt_to . “>\n”);
$sent = FALSE;
}
fclose($this->sock);
$this->log_write(“Disconnected from remote host\n”);
}
echo “<br>”;
return $sent;
}

private function smtp_send($helo, $from, $to, $header, $body = “”)
{
if (!$this->smtp_putcmd(“HELO”, $helo)) {
return $this->smtp_error(“sending HELO command”);
}
#auth
if ($this->auth) {
if (!$this->smtp_putcmd(“AUTH LOGIN”, base64_encode($this->smtp_username))) {
return $this->smtp_error(“sending HELO command”);
}

if (!$this->smtp_putcmd(“”, base64_encode($this->smtp_password))) {
return $this->smtp_error(“sending HELO command”);
}
}
#
if (!$this->smtp_putcmd(“MAIL”, “FROM:<” . $from . “>”)) {
return $this->smtp_error(“sending MAIL FROM command”);
}

if (!$this->smtp_putcmd(“RCPT”, “TO:<” . $to . “>”)) {
return $this->smtp_error(“sending RCPT TO command”);
}

if (!$this->smtp_putcmd(“DATA”)) {
return $this->smtp_error(“sending DATA command”);
}

if (!$this->smtp_message($header, $body)) {
return $this->smtp_error(“sending message”);
}

if (!$this->smtp_eom()) {
return $this->smtp_error(“sending <CR><LF>.<CR><LF> [EOM]”);
}

if (!$this->smtp_putcmd(“QUIT”)) {
return $this->smtp_error(“sending QUIT command”);
}

return TRUE;
}

private function smtp_sockopen($address)
{
if ($this->smtp_hostname == “”) {
return $this->smtp_sockopen_mx($address);
} else {
return $this->smtp_sockopen_relay();
}
}

private function smtp_sockopen_relay()
{
$this->log_write(“Trying to ” . $this->smtp_hostname . “:” . $this->smtp_port . “\n”);
if (empty($this->smtp_timeout))
{
$this->smtp_timeout=5;
}
$this->sock = @fsockopen($this->smtp_hostname, $this->smtp_port, $errno, $errstr, $this->smtp_timeout);

//var_dump($this->sock);die();

if (!($this->sock && $this->smtp_ok())) {
$this->log_write(“Error: Cannot connenct to relay host ” . $this->smtp_hostname . “\n”);
$this->log_write(“Error: ” . $errstr . ” (” . $errno . “)\n”);
return FALSE;
}
$this->log_write(“Connected to relay host ” . $this->smtp_hostname . “\n”);
return TRUE;
}

private function smtp_sockopen_mx($address)
{
$domain = ereg_replace(“^.+@([^@]+)$”, “\\1”, $address);
if (!@getmxrr($domain, $MXHOSTS)) {
$this->log_write(“Error: Cannot resolve MX \”” . $domain . “\”\n”);
return FALSE;
}
foreach ($MXHOSTS as $host) {
$this->log_write(“Trying to ” . $host . “:” . $this->smtp_port . “\n”);
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->smtp_timeout);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write(“Warning: Cannot connect to mx host ” . $host . “\n”);
$this->log_write(“Error: ” . $errstr . ” (” . $errno . “)\n”);
continue;
}
$this->log_write(“Connected to mx host ” . $host . “\n”);
return TRUE;
}
$this->log_write(“Error: Cannot connect to any mx hosts (” . implode(“, “, $MXHOSTS) . “)\n”);
return FALSE;
}

private function smtp_message($header, $body)
{
fputs($this->sock, $header . “\r\n” . $body);
$this->smtp_debug(“> ” . str_replace(“\r\n”, “\n” . “> “, $header . “\n> ” . $body . “\n> “));

return TRUE;
}

private function smtp_eom()
{
fputs($this->sock, “\r\n.\r\n”);
$this->smtp_debug(“. [EOM]\n”);

return $this->smtp_ok();
}

private function smtp_ok()
{
$response = str_replace(“\r\n”, “”, fgets($this->sock, 512));
$this->smtp_debug($response . “\n”);

if (!mb_ereg(“^[23]”, $response)) {
fputs($this->sock, “QUIT\r\n”);
fgets($this->sock, 512);
$this->log_write(“Error: Remote host returned \”” . $response . “\”\n”);
return FALSE;
}
return TRUE;
}

private function smtp_putcmd($cmd, $arg = “”)
{
if ($arg != “”) {
if ($cmd == “”)
$cmd = $arg;
else
$cmd = $cmd . ” ” . $arg;
}

fputs($this->sock, $cmd . “\r\n”);
$this->smtp_debug(“> ” . $cmd . “\n”);

return $this->smtp_ok();
}

private function smtp_error($string)
{
$this->log_write(“Error: Error occurred while ” . $string . “.\n”);
return FALSE;
}

private function log_write($message)
{
$this->smtp_debug($message);

if ($this->log_file == “”) {
return TRUE;
}

$message = date(“M d H:i:s “) . get_current_user() . “[” . getmypid() . “]: ” . $message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, “a”))) {
$this->smtp_debug(“Warning: Cannot open log file \”” . $this->log_file . “\”\n”);
return FALSE;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);

return TRUE;
}

private function strip_comment($address)
{
$comment = “\\([^()]*\\)”;
while (mb_ereg($comment, $address)) {
$address = mb_ereg_replace($comment, “”, $address);
}

return $address;
}

private function get_address($address)
{
$address = mb_ereg_replace(“([ \t\r\n])+”, “”, $address);
$address = mb_ereg_replace(“^.*<(.+)>.*$”, “\\1”, $address);

return $address;
}

private function smtp_debug($message)
{
if ($this->debug) {
echo $message . “<br>”;
}
}

private function get_attach_type($image_tag) //
{

$filedata = array();

$img_file_con = fopen($image_tag, “r”);
unset($image_data);
while ($tem_buffer = AddSlashes(fread($img_file_con, filesize($image_tag))))
$image_data .= $tem_buffer;
fclose($img_file_con);
$filedata[‘context’]  = $image_data;
$filedata[‘filename’] = basename($image_tag);
$extension            = substr($image_tag, strrpos($image_tag, “.”), strlen($image_tag) – strrpos($image_tag, “.”));
switch ($extension) {
case “.gif”:
$filedata[‘type’] = “image/gif”;
break;
case “.gz”:
$filedata[‘type’] = “application/x-gzip”;
break;
case “.htm”:
$filedata[‘type’] = “text/html”;
break;
case “.html”:
$filedata[‘type’] = “text/html”;
break;
case “.jpg”:
$filedata[‘type’] = “image/jpeg”;
break;
case “.tar”:
$filedata[‘type’] = “application/x-tar”;
break;
case “.txt”:
$filedata[‘type’] = “text/plain”;
break;
case “.zip”:
$filedata[‘type’] = “application/zip”;
break;
default:
$filedata[‘type’] = “application/octet-stream”;
break;
}
return $filedata;
}
}

关于作者

清心醉 administrator

发表评论

如果喜欢作者的文章,您可以打赏给作者:

TRC20(虚拟货币):


ERC20(虚拟货币):


Bitcoin(BTC):