Exemple #1
0
 public function actionSave()
 {
     if (Yii::$app->request->isAjax) {
         $model = new Mailbox();
         $model->load(Yii::$app->request->post());
         $model->time = time();
         if ($model->validate() && $model->save()) {
             return json_encode(['state' => true, 'type' => 'add', 'class' => $model->type == 1 ? 'addressee' : 'sender', 'html' => $this->renderAjax('/admin/mailer/_list', ['type' => 'ajax', 'data' => $model])]);
         }
         return json_encode(['state' => false]);
     }
 }
 public function store(Request $request)
 {
     $name = $request->input('data.username');
     if ($name != null) {
         $address = $name . '@' . $request->input('data.domain');
         //usuario@dominio
         $usuario = Mailbox::where('address', '=', $address)->first();
         if ($usuario != null) {
             return response()->json(['error' => 'mailbox_exists'], 409);
         }
         $current_timestamp = date("Y-m-d H:i:s");
         $mailbox = new Mailbox();
         $mailbox->id = $request->input('data.username') . '@' . $request->input('data.domain');
         $pass = $request->input('data.password');
         $hash = crypt($pass);
         $mailbox->crypt = $hash;
         $mailbox->address = $request->input('data.username') . '@' . $request->input('data.domain');
         $mailbox->name = $request->input('data.name');
         $mailbox->maildir = $request->input('data.username') . '/';
         $mailbox->quota = $request->input('data.quota', '0');
         $mailbox->created = $current_timestamp;
         $mailbox->modified = $current_timestamp;
         if ($request->input('data.enabled') == '') {
             $mailbox->enabled = '0';
         } else {
             $mailbox->enabled = '1';
         }
         $mailbox->save();
         $alias = new Aliases();
         $alias->mail = $mailbox->address;
         $alias->destination = $mailbox->address;
         $alias->created = $mailbox->created;
         $alias->modified = $mailbox->modified;
         $alias->enabled = $mailbox->enabled;
         $alias->save();
         Mail::send('email.welcome', ['mailbox' => $mailbox], function ($m) use($mailbox) {
             $m->from('*****@*****.**', 'Administrador');
             $m->to($mailbox->address, $mailbox->name)->subject('Bienvenido');
         });
         return response()->json(['success' => 'mailbox_created'], 200);
     } else {
         return response()->json(['error' => 'username_not_provided'], 200);
     }
 }