webmail读信替换的代码

jackxiang 2008-5-13 15:26 | |
ret5 = code_convert(MAIL_CODE_FROM, MAIL_CODE_TO, (char*)(LPCSTR)txtbody, strlen((LPCSTR)txtbody), body_dest, blen);

int code_convert(char *from_charset, char *to_charset,
         char *input, int inlen,
         char *output,int outlen)
{

  if(!input || !(*input))
  {
    return -3;
  }
  size_t len_in = inlen;
      size_t len_out = outlen;
  const char *p_in = input;
  char *p_out = output;
     iconv_t cd;
  cd = iconv_open(to_charset,from_charset);
  if (cd==0) return -2;
  memset(output,0,outlen);
  if (iconv(cd, &p_in, &len_in, &p_out, &len_out)==-1) return -1;
  iconv_close(cd);
  return 0;     
}

  
void CSinaMail::DirectReadMsg(LPCSTR para,LPCSTR cgi_url,CStr& msg,CStr& from,CStr& to,CStr& date,CStr& cc,CStr& subject,CStr& txtbody,CStr& msgbody,CStr& reply, int msgbody_inc_attach) {
  CStr abmsg,charset,body,mime,tmp,htmlbody,base,link;
  int charsetLen = 0;
  //get mail file absolute path
  AbMsg(abmsg,(LPCSTR)msg);
  if(!IsDir(m_mimedir)) mkdir(m_mimedir,0700);
      CMimeMail mimeMail;
      LPCSTR lpFileName=mimeMail.OpenFile((LPCSTR)abmsg);
     CAttachmentList attachmentList;
  mimeMail.ReadMail((LPSTR)lpFileName, body, attachmentList);
  HandleAttachmenList(attachmentList,para,cgi_url,m_mimedir);
  if(!body.IsEmpty()) {

    charset.MidStr((LPCSTR)body,0,"\n");
    txtbody.MidStr((LPCSTR)body,"\n",0);
    charsetLen = strlen((LPCSTR)charset);
    MsgTxtContentToHtml(cgi_url,para,(LPCSTR)txtbody,msgbody);
  }
  if(!msgbody.IsEmpty())
  {
    msgbody.ReplaceAll("&lt;", "<");
    msgbody.ReplaceAll("&gt;", ">");

  }
  CAttachment* pAtt;
  int num=attachmentList.GetCount();

  for(int i=0;i<num;i++) {
    pAtt=attachmentList.GetItem(i);
    if (pAtt->m_nDisplayOrNot==0) continue; //if not need display
                if (pAtt->m_nDisplayOrNot==2 && msgbody=="") { //if is html content
      mime.Format("%s/%s",(LPCSTR)m_mimedir,(LPCSTR)pAtt->m_cstrFileName);
                       CTmpl htmlfile((LPCSTR)mime);
                        htmlbody="<pre>";
            htmlbody=htmlbody + (CStr)htmlfile;
            htmlbody=htmlbody + "</pre>";
            base.MidStrI((LPCSTR)htmlbody,"<BASE",">",1);

                        LPCSTR body=htmlbody.FindI("<HTML");
                        if(body) body=strchr(body,'>');
                        if(body) htmlbody=body+1;
                        htmlbody.ReplaceI("<body","<xbody");

                        htmlbody.ReplaceIAll("</BODY>","</xbody>");
                        htmlbody.ReplaceIAll("</HTML>","");
                        htmlbody.ReplaceIAll("<SCRIPT","<COMMENT");
                        htmlbody.ReplaceIAll("</SCRIPT>","</COMMENT>");
      if(pAtt->m_nCharSet==HZ_CODE) CheckForHZString(htmlbody);
      if (txtbody.IsEmpty())
      {
        txtbody=htmlbody;
        char* des=(char*)calloc(1,txtbody.GetLength());
        parsetext(des,(char*)(LPCSTR)txtbody);
        txtbody=des;
        free(des);
      }
                        // 4. add div
                        //add by liulei, output base_mark in mail content
                        msgbody+="<DIV>";
      if(!base.IsEmpty()) { // include base
              msgbody+="<base";
                          msgbody+=base;
        msgbody+=">";
                          msgbody+=htmlbody;
                          msgbody+="</DIV>";  
                          msgbody+="<BASE href=";
        msgbody+=cgi_url;
        msgbody+=">";
                  } else {
                          msgbody+=htmlbody;
                          msgbody+="</DIV>";  
                  }
                  //add end
                        continue;
                }
        CStr strFujian;
        strFujian = "附件";
        toLink((LPCSTR)pAtt->m_cstrFileName,link);

        CStr fit_displayName(pAtt->m_lpszDisplayName);
                    repBadchar(fit_displayName);

        CStr displayName;
        toLink(fit_displayName, displayName);

        //2005-11-5 add by zhirui 过滤无效的类型
        CStr tmpStr1;
        CStrList tmpList1;

        if(pAtt->m_lpszType.FindI("application/") && pAtt->m_lpszType.FindI("name="))
        {
          pAtt->m_lpszType.ReplaceAll(" ", "");
          pAtt->m_lpszType.Split("name=", tmpList1);

          if(tmpList1.GetCount() == 2)
          {
            pAtt->m_lpszType.Format("%s", (LPCSTR)*tmpList1.GetItem(0));

            //
            //pAtt->m_lpszType.ReplaceAll((LPCSTR)pAtt->m_lpszType, "application/vnd.ms-excel");
          }
          else
          {
            if(pAtt->m_lpszType.FindI("vnd.ms-excelname="))
            {
              pAtt->m_lpszType.ReplaceAll((LPCSTR)pAtt->m_lpszType, "application/vnd.ms-excel");
            }
            else if(pAtt->m_lpszType.FindI("x-excelname="))
            {
              pAtt->m_lpszType.ReplaceAll((LPCSTR)pAtt->m_lpszType, "application/x-excel");
            }            
            else if(pAtt->m_lpszType.FindI("mswordname="))
            {
              pAtt->m_lpszType.ReplaceAll((LPCSTR)pAtt->m_lpszType, "application/msword");
            }
          }
        }

        //changed by zhirui 2005-10-11
        if(msgbody_inc_attach == 1)
        {
                       tmp.Format("<BR><BR>\n--------------------------------------------------<BR>\n%s:<a href='/cgi-bin/email/webmailbox/attachment.cgi/%s/%s/%s/%s/%s'>%s</a>(%s, %ld bytes) <BR>\n--------------------------------------------------<BR>\n", (LPCSTR)strFujian, para, (LPCSTR)pAtt->m_lpszType, (LPCSTR)link, (LPCSTR)displayName, (LPCSTR)displayName, (LPCSTR)pAtt->m_lpszDisplayName, (LPCSTR)pAtt->m_lpszType,pAtt->m_nFileSize);
        }

        msgbody += tmp;
  }
  if(charsetLen > 30)
  {
    charset.Empty();
    charset.Format("%s", "gb2312");
  }

  //+20080124 add get msgId from mail head
  mimeMail.GetHeader("Message-ID", this->msgIdStr,(LPCSTR)charset);
  mimeMail.GetHeader("X-Mailer", this->xMailerStr,(LPCSTR)charset);

  mimeMail.GetHeader("from",from,(LPCSTR)charset);
  mimeMail.GetHeader("to",to,(LPCSTR)charset);
  mimeMail.GetHeader("subject",subject,(LPCSTR)charset);
  mimeMail.GetHeader("cc",cc,(LPCSTR)charset);
  mimeMail.GetHeader("date",date,(LPCSTR)charset);

  GetStandardTime(8,date);
  mimeMail.GetHeader("disposition-notification-to",reply,(LPCSTR)charset);
  if(reply.IsEmpty())
    mimeMail.GetHeader("Return-Receipt-To",reply,(LPCSTR)charset);
}
  

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1033/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]