Exemplo n.º 1
0
 public function actionIndex()
 {
     $message = true;
     $model = new mail();
     if ($model->load(Yii::$app->request->post())) {
         $mailaccount = Setting::find()->one();
         $sendhost = $mailaccount->sendhost;
         $username = $mailaccount->username;
         $user = $mailaccount->user;
         $password = $mailaccount->password;
         error_reporting(E_ALL ^ E_WARNING);
         $request = Yii::$app->request;
         $body = $request->post();
         $to = $model->receiver;
         $subject = $model->subject;
         $model->file = UploadedFile::getInstance($model, 'file');
         if ($model->file != null) {
             $filename = $model->file->name;
             $encode = mb_detect_encoding($filename, array("ASCII", "UTF-8", "GBK", "GB2312"));
             if ($encode == "EUC-CN") {
                 $encode = "GB2312";
             }
             if ($encode != "GBK") {
                 $filename = iconv($encode, "GBK//IGNORE", $filename);
             }
             echo $filename . "before save" . "</br>";
             if ($model->file->saveAs('c:/sendattachment/' . $filename)) {
                 $mail = new sendmail();
                 //新建发送
                 $mail->setServer($sendhost, $user, $password);
                 $mail->setFrom($username);
                 $mail->setReceiver("{$to}");
                 $mail->setMailInfo($subject, $body['body'], 'c:/sendattachment/' . $filename);
                 $message = true;
                 if (!$mail->sendMail()) {
                     $message = false;
                 }
                 return $this->render('index', ['model' => $model, 'message' => $message]);
             }
         } else {
             $mail = new sendmail();
             //新建发送
             $mail->setServer($sendhost, $user, $password);
             $mail->setFrom($username);
             $mail->setReceiver("{$to}");
             $mail->setMailInfo($subject, $body['body'], "");
             if (!$mail->sendMail()) {
                 $message = false;
             }
             return $this->render('index', ['model' => $model, 'message' => $message]);
         }
     } else {
         return $this->render('index', ['model' => $model]);
     }
 }
Exemplo n.º 2
0
 public function actionWriteindex()
 {
     $model = new mail();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         error_reporting(E_ALL ^ E_WARNING);
         $request = Yii::$app->request;
         $body = $request->post();
         $to = $model->receiver;
         //            $text=$model->text;
         $subject = $model->subject;
         //            $body = $model->body;
         print_r($model);
         $mail = new sendmail();
         //新建发送
         $mail->setServer("smtp.126.com", "tclrg", "Luanruitest");
         $mail->setFrom("*****@*****.**");
         $mail->setReceiver("{$to}");
         $mail->setMailInfo($subject, $body['body'], false, NULL);
         $mail->sendMail();
         return $this->render('/Users\\dealer\\write', ['model' => $model]);
     } else {
         //print_r($model);
         return $this->render('/Users\\dealer\\write', ['model' => $model]);
     }
 }