function connect($params = array()) { if (!isset($this->status)) { $obj = new EcsSmtp($params); if ($obj->connect()) { $obj->status = SMTP_STATUS_CONNECTED; } return $obj; } else { $smtp_ssl = C('smtp_ssl'); if (!empty($smtp_ssl)) { $this->host = "ssl://" . $this->host; } $this->connection = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); if ($this->connection === false) { $this->errors[] = 'Access is denied.'; return false; } @socket_set_timeout($this->connection, 0, 250000); $greeting = $this->get_data(); if (is_resource($this->connection)) { $this->status = 2; return $this->auth ? $this->ehlo() : $this->helo(); } else { log_write($errstr, __FILE__, __LINE__); $this->errors[] = 'Failed to connect to server: ' . $errstr; return false; } } }
/** * 邮件发送 * * @param: $name[string] 接收人姓名 * @param: $email[string] 接收人邮件地址 * @param: $subject[string] 邮件标题 * @param: $content[string] 邮件内容 * @param: $type[int] 0 普通邮件, 1 HTML邮件 * @param: $notification[bool] true 要求回执, false 不用回执 * * @return boolean */ function send_mail($name, $email, $subject, $content, $type = 0, $notification = false) { /* 如果邮件编码不是EC_CHARSET,创建字符集转换对象,转换编码 */ if (C('mail_charset') != EC_CHARSET) { $name = ecs_iconv(EC_CHARSET, C('mail_charset'), $name); $subject = ecs_iconv(EC_CHARSET, C('mail_charset'), $subject); $content = ecs_iconv(EC_CHARSET, C('mail_charset'), $content); $shop_name = ecs_iconv(EC_CHARSET, C('mail_charset'), C('shop_name')); } $charset = C('mail_charset'); /** * 使用mail函数发送邮件 */ if (C('mail_service') == 0 && function_exists('mail')) { /* 邮件的头部信息 */ $content_type = ($type == 0) ? 'Content-Type: text/plain; charset=' . $charset : 'Content-Type: text/html; charset=' . $charset; $headers = array(); $headers[] = 'From: "' . '=?' . $charset . '?B?' . base64_encode($shop_name) . '?=' . '" <' . C('smtp_mail') . '>'; $headers[] = $content_type . '; format=flowed'; if ($notification) { $headers[] = 'Disposition-Notification-To: ' . '=?' . $charset . '?B?' . base64_encode($shop_name) . '?=' . '" <' . C('smtp_mail') . '>'; } $res = @mail($email, '=?' . $charset . '?B?' . base64_encode($subject) . '?=', $content, implode("\r\n", $headers)); if (!$res) { ECTouch::err()->add(L('sendemail_false')); return false; } else { return true; } } /** * 使用smtp服务发送邮件 */ else { /* 邮件的头部信息 */ $content_type = ($type == 0) ? 'Content-Type: text/plain; charset=' . $charset : 'Content-Type: text/html; charset=' . $charset; $content = base64_encode($content); $headers = array(); $headers[] = 'Date: ' . gmdate('D, j M Y H:i:s') . ' +0000'; $headers[] = 'To: "' . '=?' . $charset . '?B?' . base64_encode($name) . '?=' . '" <' . $email . '>'; $headers[] = 'From: "' . '=?' . $charset . '?B?' . base64_encode($shop_name) . '?=' . '" <' . C('smtp_mail') . '>'; $headers[] = 'Subject: ' . '=?' . $charset . '?B?' . base64_encode($subject) . '?='; $headers[] = $content_type . '; format=flowed'; $headers[] = 'Content-Transfer-Encoding: base64'; $headers[] = 'Content-Disposition: inline'; if ($notification) { $headers[] = 'Disposition-Notification-To: ' . '=?' . $charset . '?B?' . base64_encode($shop_name) . '?=' . '" <' . C('smtp_mail') . '>'; } /* 获得邮件服务器的参数设置 */ $params['host'] = C('smtp_host'); $params['port'] = C('smtp_port'); $params['user'] = C('smtp_user'); $params['pass'] = C('smtp_pass'); if (empty($params['host']) || empty($params['port'])) { // 如果没有设置主机和端口直接返回 false ECTouch::err()->add(L('smtp_setting_error')); return false; } else { // 发送邮件 if (!function_exists('fsockopen')) { //如果fsockopen被禁用,直接返回 ECTouch::err()->add(L('disabled_fsockopen')); return false; } static $smtp; $send_params['recipients'] = $email; $send_params['headers'] = $headers; $send_params['from'] = C('smtp_mail'); $send_params['body'] = $content; if (!isset($smtp)) { $smtp = new EcsSmtp($params); } if ($smtp->connect() && $smtp->send($send_params)) { return true; } else { $err_msg = $smtp->error_msg(); if (empty($err_msg)) { ECTouch::err()->add('Unknown Error'); } else { if (strpos($err_msg, 'Failed to connect to server') !== false) { ECTouch::err()->add(sprintf(L('smtp_connect_failure'), $params['host'] . ':' . $params['port'])); } else if (strpos($err_msg, 'AUTH command failed') !== false) { ECTouch::err()->add(L('smtp_login_failure')); } elseif (strpos($err_msg, 'bad sequence of commands') !== false) { ECTouch::err()->add(L('smtp_refuse')); } else { ECTouch::err()->add($err_msg); } } return false; } } } }