protected function sendReport($sendLogObject) { if (is_object($sendLogObject->report_processed->ex_schedule_report)) { $file_name = $sendLogObject->report_processed->getFileName($sendLogObject->report_processed->ex_schedule_report); $file_path = $sendLogObject->report_processed->full_file_name; if ($sendLogObject->destination->method === 'mail') { $this->logger->log(__METHOD__, array('method' => $sendLogObject->destination->method, 'email' => $sendLogObject->destination->destination_email)); $mail_params = array('actuality_time' => $sendLogObject->created, 'schedule_period' => '', 'report_file_name' => $file_name, 'link' => '', 'report_type' => $sendLogObject->report_processed->ex_schedule_report->report_type); $subject = Yii::t('letter', 'scheduled_report_mail_subject', $mail_params, null, 'en'); $settings = Settings::model()->findByPk(1); $mailSender = new mailSender('odss_reports', array()); $sendResult = $mailSender->setAttachments(array(array('file_path' => $file_path, 'file_name' => $file_name)))->setRecipient($sendLogObject->destination->destination_email)->setFrom($settings->mail__sender_address, $settings->mail__sender_name)->setSubject($subject)->setHtmlBody()->send(); if ($sendResult !== false) { $sendLogObject->sent = 1; $sendLogObject->save(); } $this->logger->log(__METHOD__ . ' $sendResult: ' . $sendResult); $this->logger->log(__METHOD__ . ' Message send with attached file'); $this->logger->log(__METHOD__ . ' Deliver via mail DONE.'); } else { if ($sendLogObject->destination->method === 'ftp') { // use it if you have some superstition about "../" // $fileCopier = new FileCopier; // $file_path = $fileCopier->rmPathSteps($file_path); $ftpClient = new FtpClient(); $errors = $ftpClient->connect($sendLogObject->destination->destination_ip, $sendLogObject->destination->destination_ip_port)->login($sendLogObject->destination->destination_ip_user, $sendLogObject->destination->destination_ip_password)->setFolder($sendLogObject->destination->destination_ip_folder)->openLocalFile($file_path)->upload($file_name)->closeLocalFile()->getErrors(); if (!count($errors)) { $sendLogObject->sent = 1; $sendLogObject->save(); } else { $sendLogObject->send_logs = serialize($errors); $sendLogObject->save(); } $this->logger->log(__METHOD__ . " ftp errors:" . print_r($errors, 1)); $this->logger->log(__METHOD__ . ' Deliver via ftp DONE.'); } else { if ($sendLogObject->destination->method === 'local_folder') { $this->logger->log(__METHOD__, array('report_type' => $sendLogObject->report_processed->ex_schedule_report->report_type, 'method' => $sendLogObject->destination->method, 'destination_folder' => $sendLogObject->destination->destination_local_folder)); $destinationPath = $sendLogObject->report_processed->getFileDir() . DIRECTORY_SEPARATOR . $sendLogObject->destination->destination_local_folder; $this->logger->log(__METHOD__ . ' $file_path: ' . $file_path); $this->logger->log(__METHOD__ . ' $destinationPath: ' . $destinationPath); $this->logger->log(__METHOD__ . ' $file_name: ' . $file_name); $fileCopier = new FileCopier(); $errors = $fileCopier->copy($file_path, $destinationPath . DIRECTORY_SEPARATOR . $file_name)->getErrors(); if (!count($errors)) { $sendLogObject->sent = 1; $sendLogObject->save(); } else { $sendLogObject->send_logs = serialize($errors); $sendLogObject->save(); } $this->logger->log(__METHOD__ . ' errors:' . print_r($errors, 1)); $this->logger->log(__METHOD__ . ' Deliver to local folder DONE.'); } } } } }
public function deliverReport() { $this->_logger->log(__METHOD__); if (count($this->data) == 0) { return false; } ini_set('memory_limit', '-1'); $this->_logger->log(__METHOD__, array('ex_schedule_id' => $this->schedule_type_report->ex_schedule_id)); $this->_logger->log(__METHOD__ . ' :' . print_r($this->schedule_type_report, 1)); $destinations = ScheduleTypeReportDestination::getList($this->schedule_type_report->ex_schedule_id); $total = count($destinations); $this->_logger->log(__METHOD__, array('destination_count' => $total)); //$this->_logger->log(__METHOD__ . ' :'. print_r($destinations,1)); $file_path = $this->schedule_type_report_processed->full_file_name; // $report_type = strtoupper($this->schedule_info->report_type); $file_name = $this->schedule_type_report_processed->check_period_start . '-' . $this->schedule_type_report_processed->check_period_end . '.' . $this->schedule_type_report->report_format; //$this->schedule_type_report->report_format; $report_type = $this->schedule_type_report->report_type; //$this->schedule_type_report_processed->file_content; if (count($destinations) > 0) { foreach ($destinations as $i => $destination) { if ($destination->method === 'mail') { $this->_logger->log(__METHOD__, array('destination' => $i + 1, 'report_type' => $report_type, 'method' => $destination->method, 'email' => $destination->destination_email)); $mail_params = array('station_id_code' => $this->schedule_type_report->station_type, 'actuality_time' => $this->schedule_type_report->created, 'schedule_period' => Yii::app()->params['schedule_generation_period'][$this->schedule_type_report->period], 'report_file_name' => $file_name, 'link' => '', 'report_type' => $report_type); $subject = Yii::t('letter', 'scheduled_report_mail_subject', $mail_params, null, 'en'); $settings = Settings::model()->findByPk(1); $mailSender = new mailSender('odss_reports', $mail_params); $sendResult = $mailSender->setAttachments(array(array('file_path' => $file_path, 'file_name' => $file_name)))->setRecipient($destination->destination_email)->setFrom($settings->mail__sender_address, $settings->mail__sender_name)->setSubject($subject)->setHtmlBody()->send(); $this->_logger->log(__METHOD__ . ' $sendResult: ' . $sendResult); $this->_logger->log(__METHOD__ . ' Message send with attached file'); $this->_logger->log(__METHOD__ . ' Deliver via mail DONE.'); } else { if ($destination->method === 'ftp') { // use it if you have some superstition about "../" // $fileCopier = new FileCopier; // $file_path = $fileCopier->rmPathSteps($file_path); $ftpClient = new FtpClient(); $errors = $ftpClient->connect($destination->destination_ip, $destination->destination_ip_port)->login($destination->destination_ip_user, $destination->destination_ip_password)->setFolder($destination->destination_ip_folder)->openLocalFile($file_path)->upload($file_name)->closeLocalFile()->getErrors(); $this->_logger->log(__METHOD__ . " ftp errors:" . print_r($errors, 1)); $this->_logger->log(__METHOD__ . ' Deliver via ftp DONE.'); } else { if ($destination->method === 'local_folder') { $this->_logger->log(__METHOD__, array('destination' => $i + 1, 'report_type' => $report_type, 'method' => $destination->method, 'destination_folder' => $destination->destination_local_folder)); $destinationPath = $this->schedule_type_report_processed->getFileDir() . DIRECTORY_SEPARATOR . $destination->destination_local_folder; $this->_logger->log(__METHOD__ . ' $file_path: ' . $file_path); $this->_logger->log(__METHOD__ . ' $destinationPath: ' . $destinationPath); $this->_logger->log(__METHOD__ . ' $file_name: ' . $file_name); $fileCopier = new FileCopier(); $errors = $fileCopier->copy($file_path, $destinationPath . DIRECTORY_SEPARATOR . $file_name)->getErrors(); $this->_logger->log(__METHOD__ . print_r($errors, 1)); $this->_logger->log(__METHOD__ . ' Deliver to local folder DONE.'); } } } } } $this->_logger->log(__METHOD__ . ' Delivery completed.'); }
<?php namespace cd; set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/../core/'); require_once 'FtpClient.php'; $x = new FtpClient(); $x->setAddress('sftp://*****:*****@hostname.com:12346/path/to/file'); if ($x->getUrl() != 'sftp://*****:*****@hostname.com:12346/path/to/file') { echo "FAIL 1\n"; }
private function ftpSend($excel_path) { $this->_logger->log(__METHOD__); $time = $this->now['mday'] . ' ' . $this->now['month'] . ' ' . $this->now['year']; $file_name = "HeartbeatReport-{$time}.xls"; $ftpClient = new FtpClient(); $errors = $ftpClient->connect($this->ftp, $this->ftpPort)->login($this->ftpUser, $this->ftpPassword)->setFolder($this->ftpFolder)->openLocalFile($excel_path)->upload($file_name)->closeLocalFile()->getErrors(); $this->_logger->log(__METHOD__ . "ERROR: " . print_r($errors, 1)); }