Ejemplo n.º 1
0
        /// <summary>
        /// 寄送郵件
        /// </summary>
        /// <param name="userID">user id</param>
        /// <param name="model">Mail內容取代字串</param>
        /// <param name="systemMailType">郵件類型</param>
        /// <param name="validType">驗證碼類型</param>
        /// <param name="structureID">郵件類型structure</param>
        /// <param name="fromFn">來源</param>
        /// <returns></returns>
        public async Task <CiResult> SendEmail(Guid userID, ReplaceMailContent model,
                                               SystemMailType systemMailType, ValidType?validType = null, Guid?structureID = null,
                                               string fromFn = "")
        {
            var result = new CiResult {
                IsSuccess = true
            };
            var mailTool = new MailTool {
                email = model.UserEmail
            };

            //ReplaceMailContent
            model.WebsiteUrl = RouteHelper.BaseUrl();
            if (string.IsNullOrEmpty(model.UserName) || string.IsNullOrEmpty(model.UserEmail) || !_Check.IsEmail(model.UserEmail))
            {
                result.IsSuccess = false;
            }

            //get setting
            if (result.IsSuccess)
            {
                var smtpResult = settingService.Get <SmtpServerViewModel>(SystemSettingType.SmtpServer);
                mailTool.setting = smtpResult.Data;
                if (!smtpResult.IsSuccess || !smtpResult.Data.IsEnabled)
                {
                    result.IsSuccess = false;
                }
            }

            if (result.IsSuccess)
            {
                var siteResult = settingService.Get <SiteInfoViewModel>(SystemSettingType.SiteInfo);
                if (siteResult.IsSuccess)
                {
                    model.WebsiteName = siteResult.Data.SiteName;
                }
                else
                {
                    result.IsSuccess = false;
                }
            }

            //create validCode
            if (result.IsSuccess && validType != null)
            {
                var validCodeResult = userService.CreateValidCode(userID, validType.Value);
                if (validCodeResult.IsSuccess)
                {
                    model.ConfirmUrl = RouteHelper.GetConfirmUrl(validType.Value, validCodeResult.Data, model.UserEmail);
                }
                else
                {
                    result.IsSuccess = false;
                }
            }

            //get template
            var template = new cms_EmailTemplate();

            if (result.IsSuccess)
            {
                template = templateService.GetByType(systemMailType, structureID);
                if (template != null)
                {
                    // ckeditor
                    template.Content = HttpUtility.HtmlDecode(template.Content);

                    // set template
                    mailTool.subject = model.ReplaceContent(template.Subject);
                    mailTool.content = model.ReplaceContent(template.Content);
                    if (!string.IsNullOrEmpty(template.TemplateBcc))
                    {
                        mailTool.bccReceiver = template.TemplateBcc.Split(',');
                    }
                }
                else
                {
                    result.IsSuccess = false;
                    _Log.CreateText($"SendEmail no template: {systemMailType.ToString()}");
                }
            }

            //send mail (測試中:只能寄給開發者)
            if (result.IsSuccess)
            {
                if (!ApplicationHelper.IsLocal || (ApplicationHelper.IsLocal && mailTool.IsTestingMail(model.UserEmail)))
                {
                    result.IsSuccess = await mailTool.SendAsync(fromFn);
                }
                else
                {
                    _Log.CreateText($"Local not send: {mailTool.email}");
                }

                //add log
                emailService.CreateLog(new EmailViewModel
                {
                    Email = new cms_Email
                    {
                        Subject        = mailTool.subject,
                        Content        = mailTool.content,
                        SystemMailType = (int)systemMailType,
                        Status         = (int)MailStatus.Done,
                        SendTime       = DateTime.Now
                    },
                    SendUsers = new List <cms_EmailSendUser> {
                        new cms_EmailSendUser {
                            ToEmail  = model.UserEmail,
                            ToUser   = userID,
                            IsSend   = result.IsSuccess,//是否已發送
                            SendTime = DateTime.Now
                        }
                    }
                });
            }

            if (result.IsSuccess)
            {
                if (systemMailType == SystemMailType.ConfirmEmail)
                {
                    result.Message = string.Format(SystemMessage.EmailConfirm, model.UserEmail);
                }
                else if (systemMailType == SystemMailType.ForgotPassword)
                {
                    result.Message = string.Format(SystemMessage.PasswordReset, model.UserEmail);
                }
                else
                {
                    result.Message = SystemMessage.SendSuccess;
                }
            }
            else if (string.IsNullOrEmpty(result.Message))
            {
                result.Message = SystemMessage.MailServerError;
            }

            SessionManager.Captcha = "";

            return(result);
        }