<!--#include file="../Include/Conn.asp" -->
<!--#include file="../Include/Char.inc" -->
<!--#include file="MailListConst.asp" -->
<!--#include file="Email.asp" -->
<%
 m_ContentType = "text/html"
 m_Priority = 3 '普通级别
 m_Email = LCase(Trim(Request.Form("txtEmail")))
 m_Sender = MailListInfo(1)
 m_SenderName = MailListInfo(0)
 If Request.form("btnExit")="退订" then
   '退订
   If IsTake(m_Email)=0 then
     Call MsgBox("对不起，退订失败,因为您没有在本站订阅邮件列表！",1)
   Else
     If ExitTake(m_Email)="OK" Then
	   Call MsgBox("我们向你发送了退订的确认邮件，请进入您的邮箱表示确认!",1)
     Else
       Call MsgBox("对不起,退订失败,请您与本站管理员联系!",1)
     End if
   End if'//退订
 Else
   '订阅
   m_TakeContent = Request.Form("chkTake")
   m_TakeCateName = ReadTakeCateName(m_TakeContent)
   If IsTake(m_Email)=1 then'表示找到
     Call MsgBox("对不起，你已经在本站订阅邮件列表了!",1)
   Else
     '新增用户 
     m_errAddToMailList=AddToMailList()
     If m_errAddToMailList="OK" then
       '发送订阅邮件
       If SendTakeMail()="OK" then
         Call MsgBox("祝贺您，欢迎您订阅本站的邮件列表,您将会马上收到一封确认信!",1)
	   Else
	     Call MsgBox("对不起，邮件发送失败，导致失败的原因可能是邮箱提供商的问题，或其它不可知原因!",1)
	   End if
     Else
	   Call MsgBox("对不起，邮件订阅失败,请与本站管理员联系！",1)
	   'Response.write errAddToMailList
     End if
   End if'是否已订
End if'不退订
 

 'if Email IsTack 
 Function IsTake(m_Email)
   m_Sql = "Select F_Email,F_IsTake From MailList_User Where F_Email='"&m_Email&"' and F_IsTake=1"
   Set rst = conn.Execute(m_Sql)
   if Not rst.Eof then
     IsTake=1
   Else
     IsTake=0
   End if   
   rst.Close
 End Function

 'Write Data
 Function AddToMailList()
   On error resume next
     m_UserGroupID = 1  '表示邮件列有用户
	 m_UserName = mid(m_Email,1,instr(m_Email,"@")-1)
	 m_IsTake =1 '表示订阅
     m_Sql = "Select F_Email,F_IsTake From MailList_User Where F_Email='"&m_Email&"'"
     Set rst = conn.Execute(m_Sql)
     if not rst.eof then
       m_Sql = "Update MailList_User Set F_IsTake=1,F_Addtime='"&now()&"',F_IPInfo='"&Request.ServerVariables("REMOTE_ADDR")&"',F_TakeContent='"&m_TakeContent&"' Where F_Email='"&m_Email&"'"
     else
	   m_Sql = "Insert Into MailList_User(F_UserGroupID,F_Email,F_UserName,F_IsTake,F_Addtime,F_IPInfo,F_TakeContent) Values("&m_UserGroupID&",'"&m_Email&"','"&m_UserName&"',"&m_IsTake&",'"&now()&"','"&Request.ServerVariables("REMOTE_ADDR")&"','"&m_TakeContent&"')"
	 end if 
     conn.Execute(m_Sql)
   If err then 
	 Result = err.description
	 err.clear
   Else
	 Result = "OK"
   End if
   AddToMailList = Result
 End Function
 
 'Send TakeMail
 Function SendTakeMail()
   m_UserName = mid(m_Email,1,instr(m_Email,"@")-1)
   m_AD = HTMLEncode(MailListInfo(3))
   m_Subject = "欢迎您订阅『网络新接触』电子杂志！"
   m_Body = MailListInfo(4)
   m_Body = "<b>"&m_UserName&":</b><br>&nbsp;&nbsp;&nbsp;&nbsp;您好！<br>"&m_Body
   If m_TakeCateName<>"" then
     m_Body = m_Body & "<br><br><br>您已订阅以下几个栏目,我们会定期以邮件方式发送给您！<br><font color=red>"& m_TakeCateName
   End if
   m_Body = m_Body & "</font><br><br>"& m_AD
   m_Body = EmailBody(m_Body)
   m_SendOk = UseJmail(m_Email,m_Subject,m_Body,m_Sender,m_SenderName)
   SendTakeMail = m_SendOk
 End Function
 
 '读类别名称
 Function ReadTakeCateName(str)
   m_ArrTake = split(str,",")
   m_TakeCateName = ""
   for i = 0 to  ubound(m_ArrTake)
     m_Sql = "Select F_Value From T_SmallCategory Where F_ID=" & cint(Trim(m_ArrTake(i)))
 	 Set rst = conn.Execute(m_Sql)
	 m_TakeCateName = m_TakeCateName + rst(0) +"&nbsp;"
   Next
   ReadTakeCateName = m_TakeCateName
 End Function

 '退订
 Function ExitTake(m_Email)
   Randomize
   m_ExitConfirmRand = Int((999999999999 - 100000000000 + 1) * Rnd + 100000000000)
   m_ExitConfirmPath = "http://"+Request.ServerVariables("HTTP_HOST")&"/wzvtc/MailList/ExitConfirm.asp?ConfirmNumber="&m_ExitConfirmRand
   m_ExitConfirmPath = "<a target='_blank' href='"&m_ExitConfirmPath&"'>"&m_ExitConfirmPath&"</a>"
   m_UserName = mid(m_Email,1,instr(m_Email,"@")-1)
   m_AD = HTMLEncode(MailListInfo(3))
   m_Subject = "请确认是否退订『网络新接触』电子杂志?"
   m_Body = MailListInfo(7)
   m_Body = "<b>"&m_UserName&":</b><br>&nbsp;&nbsp;&nbsp;&nbsp;您好！<br>"&m_Body
   m_Body = m_Body + m_ExitConfirmPath
   m_Body = m_Body &"<br><br>"& m_AD
   m_Body = EmailBody(m_Body)
   m_SendOK = UseJmail(m_Email,m_Subject,m_Body,m_Sender,m_SenderName)
   'Modify F_IsTake Flag
   If m_SendOK="OK" then
     m_Sql = "Update MailList_User Set F_ConfirmNumber='"&m_ExitConfirmRand&"' Where F_Email='"&m_Email&"'"
	 conn.Execute(m_Sql)
   End if
   ExitTake=m_SendOK
 End function
%>