public function backupAction() { $log = $this->getServiceLocator()->get('log'); $log->addInfo('备份数据' . "\t" . $this->getRequest()->getServer('REMOTE_ADDR') . "\t" . $this->getRequest()->getHeaders()->get('User-Agent')->getFieldValue()); $dbconf = $this->getServiceLocator()->get('config')['mysqli']; $dump = new \MySQLDump(new \mysqli($dbconf['host'], $dbconf['username'], $dbconf['password'], $dbconf['dbname'])); $filename = date("Y-m-d_H-i-s") . "-db.sql"; $tmpFile = dirname(__FILE__) . "\\" . $filename; $dump->save($tmpFile); $body = new Message(); $part = new Part(); $part->setType(Mime::TYPE_OCTETSTREAM); $part->setContent(file_get_contents($tmpFile)); $part->setDisposition(Mime::DISPOSITION_ATTACHMENT); $part->setFileName($filename); $part2 = new Part(); $part2->setType(Mime::TYPE_TEXT); $part2->setContent('小秋来发数据了'); $body->addPart($part); $body->addPart($part2); $newmessage = new \Zend\Mail\Message(); $newmessage->addTo($this->getServiceLocator()->get('MailOptions')->getMailTo()); $newmessage->addFrom($this->getServiceLocator()->get('MailOptions')->getMailFrom()); $newmessage->setBody($body); $newmessage->setSubject('备份数据'); $transport = new SmtpTransport(); $options = new SmtpOptions($this->getServiceLocator()->get('config')['mail']); $transport->setOptions($options); try { $transport->send($newmessage); echo 1; } catch (\Exception $e) { echo -1; } exit; }
public function sendEmailsFromQueue($developmentMode = false) { $transport = $this->serviceManager->get('SlmMail\\Mail\\Transport\\SesTransport'); $entity = new $this->config['database']['entity'](); $tableName = $this->entityManager->getClassMetadata(get_class($entity))->getTableName(); $dql = 'SELECT m FROM ' . $this->config['database']['entity'] . ' m WHERE m.send = 0 AND m.scheduleDate <= :now ORDER BY m.prio, m.createDate DESC'; $query = $this->entityManager->createQuery($dql)->setParameter('now', date('Y-m-d H:i:s'))->setMaxResults($this->config['numberOfEmailsPerRun']); $queue = $query->getResult(); foreach ($queue as $mail) { // In development mode we only send emails to predefined email addresses to prevent "strange" unrequested // emails to users. if ($developmentMode === true && !in_array($mail->getRecipientEmail(), $this->config['developmentEmails'])) { $this->entityManager->getConnection()->update($tableName, array('send' => 1), array('id' => $mail->getId())); continue; } $message = new \Zend\Mail\Message(); $message->addFrom($mail->getSenderEmail(), $mail->getSenderName())->addTo($mail->getRecipientEmail(), $mail->getRecipientName())->setSubject($mail->getSubject()); if (trim($mail->getBodyHTML()) !== '') { $bodyPart = new \Zend\Mime\Message(); $bodyMessage = new \Zend\Mime\Part($mail->getBodyHTML()); $bodyMessage->type = 'text/html'; $bodyPart->setParts(array($bodyMessage)); $message->setBody($bodyPart); $message->setEncoding('UTF-8'); } else { $message->setBody($mail->getBodyText()); } try { $transport->send($message); $this->entityManager->getConnection()->update($tableName, array('send' => 1, 'sendDate' => date('Y-m-d H:i:s')), array('id' => $mail->getId())); } catch (\Exception $e) { $this->entityManager->getConnection()->update($tableName, array('send' => 2, 'error' => $e->getMessage()), array('id' => $mail->getId())); $this->queueNewMessage('MailAdmin', $this->config['adminEmail'], $e->getMessage(), $e->getMessage(), 'MailQueue Error', 9); } } }
/** * Send an email to any email address * * @param mixed $from Email address or string: "name <email>" * @param mixed $to Email address or string: "name <email>" * @param string $subject The subject of the message * @param string $body The message body * @param array $params Optional parameters * @return bool * @throws NotificationException */ function notifications_html_handler_send_email($from, $to, $subject, $body, array $params = null) { $options = array('to' => $to, 'from' => $from, 'subject' => $subject, 'body' => $body, 'params' => $params, 'headers' => array("Content-Type" => "text/html; charset=UTF-8; format=flowed", "MIME-Version" => "1.0", "Content-Transfer-Encoding" => "8bit")); // $mail_params is passed as both params and return value. The former is for backwards // compatibility. The latter is so handlers can now alter the contents/headers of // the email by returning the array $options = elgg_trigger_plugin_hook('email', 'system', $options, $options); if (!is_array($options)) { // don't need null check: Handlers can't set a hook value to null! return (bool) $options; } try { if (empty($options['from'])) { $msg = "Missing a required parameter, '" . 'from' . "'"; throw new \NotificationException($msg); } if (empty($options['to'])) { $msg = "Missing a required parameter, '" . 'to' . "'"; throw new \NotificationException($msg); } $options['to'] = \Elgg\Mail\Address::fromString($options['to']); $options['from'] = \Elgg\Mail\Address::fromString($options['from']); $options['subject'] = elgg_strip_tags($options['subject']); $options['subject'] = html_entity_decode($options['subject'], ENT_QUOTES, 'UTF-8'); // Sanitise subject by stripping line endings $options['subject'] = preg_replace("/(\r\n|\r|\n)/", " ", $options['subject']); $options['subject'] = elgg_get_excerpt(trim($options['subject'], 80)); $message = new \Zend\Mail\Message(); foreach ($options['headers'] as $headerName => $headerValue) { $message->getHeaders()->addHeaderLine($headerName, $headerValue); } $message->setEncoding('UTF-8'); $message->addFrom($options['from']); $message->addTo($options['to']); $message->setSubject($options['subject']); $body = new Zend\Mime\Message(); $html = new \Zend\Mime\Part($options['body']); $html->type = "text/html"; $body->addPart($html); $files = elgg_extract('attachments', $options['params']); if (!empty($files) && is_array($files)) { foreach ($files as $file) { if (!$file instanceof \ElggFile) { continue; } $attachment = new \Zend\Mime\Part(fopen($file->getFilenameOnFilestore(), 'r')); $attachment->type = $file->getMimeType() ?: $file->detectMimeType(); $attachment->filename = $file->originalfilename ?: basename($file->getFilename()); $attachment->disposition = Zend\Mime\Mime::DISPOSITION_ATTACHMENT; $attachment->encoding = Zend\Mime\Mime::ENCODING_BASE64; $body->addPart($attachment); } } $message->setBody($body); $transport = notifications_html_handler_get_transport(); if (!$transport instanceof Zend\Mail\Transport\TransportInterface) { throw new \NotificationException("Invalid Email transport"); } $transport->send($message); } catch (\Exception $e) { elgg_log($e->getMessage(), 'ERROR'); return false; } return true; }
/** * Creates a new user * @param \Zend\Db\Adapter\Adapter $db * @param type $login * @param type $password * @return \login\user\User */ public static function createLoginInstance(\Zend\Db\Adapter\Adapter $db, $login, $password) { $user = self::getLoginInstance($db, $login); if (is_object($user) && $user->getData('username') != '') { throw new \Exception('Username already used ' . $login, 1409011238); } $adminColl = new \login\user\LoginColl($db); $adminColl->loadAll(array('role_id' => 3)); $role_id = 3; $role_description = 'User'; $active = 0; if ($adminColl->count() == 0) { $role_id = 1; $role_description = 'Administrator'; $active = 1; } $profileRole = new \login\user\ProfileRole($db); $profileRole->loadFromId($role_id); if ($profileRole->getData('id') != $role_id) { $defaultRuleFile = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'sql' . DIRECTORY_SEPARATOR . 'profile_role.sql'; if (is_file($defaultRuleFile)) { $db->query(file_get_contents($defaultRuleFile), \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE); } $profileRole->loadFromId($role_id); if ($profileRole->getData('id') != $role_id) { $profileRole->setData(array('id' => $role_id, 'description' => $role_description)); $profileRole->insert(); } } $user = new \login\user\Login($db); $user->setData(array('username' => $login, 'password' => md5($password), 'creation_datetime' => date('Y-m-d H:i:s'), 'confirm_code' => md5(serialize($_SERVER) . time()))); $user->insert(); $profile = $user->getProfile(); $profile->setData(array('role_id' => $role_id, 'active' => $active)); $profile->update(); ob_start(); require $db->baseDir . DIRECTORY_SEPARATOR . 'mail' . DIRECTORY_SEPARATOR . 'register.php'; $html = new \Zend\Mime\Part(ob_get_clean()); $html->type = 'text/html'; $body = new \Zend\Mime\Message(); $body->setParts(array($html)); $message = new \Zend\Mail\Message(); $message->addTo($login)->addFrom($GLOBALS['config']->mail_from)->setSubject('Registrazione sul sito ' . $GLOBALS['config']->siteName)->setBody($body); $GLOBALS['transport']->send($message); if ($adminColl->count() > 0) { ob_start(); require $db->baseDir . DIRECTORY_SEPARATOR . 'mail' . DIRECTORY_SEPARATOR . 'new_user.php'; $html = new \Zend\Mime\Part(ob_get_clean()); $html->type = 'text/html'; $body = new \Zend\Mime\Message(); $body->setParts(array($html)); $message = new \Zend\Mail\Message(); foreach ($adminColl->getItems() as $admin) { $message->addTo($admin->getData('username')); } $message->addFrom($GLOBALS['config']->mail_from)->setSubject('Registrazione sul sito ' . $GLOBALS['config']->siteName)->setBody($body); $GLOBALS['transport']->send($message); } return $user; }