- 论坛徽章:
- 0
|
常看到有人问如何用 C 程序自动登录论坛, 邮件服务器, xxx服务器之类的问题.
前不久新浪的 mail 用 foxmail 经常发不出去, 而通过 web 方式则没问题.
新浪邮箱 web 方式登录, 编写, 退出太麻烦, 于是就做了个简单的 WEB 邮件登录/发送代理.
供后来者参考.
工作原理:
(1) smail 连接 sina 登录服务器, 进行登录
(2) 取出登录 id, 请求一个 php 会话服务器(就是它的写邮件的 web站点).
(3) 该站点会返回一个 PHP ID, 和重定向的地址.
(4) 把邮件构造成 multipart/form-data 格式,
(5) 将 登录ID, PHP ID 一起发送给WEB邮件服务器.
(6) 把新浪邮件服务器应答取出, 显示给作用者.
OK 啦.
![]()
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <!--
- /* Copyright(C) eyBuild Group, 2005-2007. All Rights Reserved. */
- 源码来自: [url]http://www.eybuild.com[/url]
- 支持平台: Unix/Windows
- Unix编译: make clean all
- 变更请通知作者: [email]eybuild@hotmail.com[/email]
- -->
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>新浪邮件</title>
- <script language="javascript" src="/js/eyForm.js"></script>
- </head>
- <style><!--
- body,td,a,p,{font-family:arial,sans-serif;font-size:14px}
- --></style>
- <script language="javascript"><!--
- function update_text() {
- form1.msgtxt.value = editbox.document.EditCtrl.document.body.innerHTML;
- }
- function update_state() {
- msg.innerHTML = "邮件发送中...";
- }
- --></script>
- <body>
- <p>
- <!--
- -->
- <center><img src="/img/msina.jpg"></center>
- <form name="form1" method="post" action="" ENCTYPE="multipart/form-data" onsubmit="update_state()">
- <input type="hidden" name="type" value="html" />
- <input type="hidden" name="issend" value="1" />
- <input type="hidden" name="fid" value="" />
- <input type="hidden" name="mid" value="" />
- <input type="hidden" name="youxj" value="3" />
- <table width="615" border="0" align="center" cellspacing="0">
- <tr>
- <td colspan="2"><strong>发送新浪邮件</strong></td>
- </tr>
- <tr>
- <td width=100>发送到:</td>
- <td><input name="to" type="text" size="75" value="<% =#G("to") %>" notnull="true"></td>
- <tr>
- <td width=100>抄送到:</td>
- <td><input name="cc" type="text" size="75" value="<% =#G("cc") %>"></td>
- <tr><td width=100>邮件主题:</td>
- <td><input name="subj" type="text" size="75" value="<% /* =G("subj") */ %>" notnull="true"></td>
- <tr>
- <td width=100>发送附件:</td>
- <td><input name="atth0" type="file" size="63" value="<% =G("atth0") %>"></td>
- <tr>
- <td width=100>发送者邮箱:</td>
- <td><input type="text" name="from" size="20" value="<% =G("from") %>" notnull="true">
- 登录密码:<input type="password" name="pswd" size="20" value="<% =G("pswd") %>" notnull="true"></td>
- <tr>
- <td colspan=2><textarea id="msgtxt" name="msgtxt" value="" rows=10 cols=65 style="display:none"><% =G("msgtxt") %></textarea>
- <iframe name="editbox" src="edit.htm" height=280 width=100% frameBorder=0 style="border:0px;padding-right: 0pt; padding-left: 0pt; padding-bottom: 0pt; margin: 0pt; "></iframe></td>
- <tr>
- <td><input type="submit" name="sendmail" value="发送邮件" onclick="update_text()"></td>
- <td><input type="button" value="重写邮件"
- onclick="location.href='?from=<% =urlEncode(G("from")) %>&to=<% =urlEncode(G("to")) %>&pswd=<% =G("pswd") %>'" />
- <% CHECK_BOX("nokeep", "1", G("nokeep"), "") %> 不保存已发送邮件.</td>
- </tr>
- <tr><td colspan=2> </td></tr>
- <tr>
- <td colspan=2><font color=red><div id="msg">
- <% if (!isblankstr(errmsg)) /* report error */ {
- char * success = "\x91\xe9\x80\x81";
- print("<b>结果:</b> %s", strstr(errmsg, success) ? "发送成功": errmsg);
- } %></div></font></td>
- </tr>
- </table>
- </form>
- <div id="mail_content" style="display: none;">
- <% = strissp(G("nokeep")) ? G("msgtxt"): "" %>
- </div>
- <xmp><% /* =buff */ %></xmp>
- <% @e
- #if !defined(REDEF_ENTRY)
- sock_clean();
- #endif /* WIN32 */
- %>
- </body>
- </html>
- <% @b
- @include <socklib.h>
- char buff[65536] = ""; /* large enougth */
- int maxlen = sizeof(buff);
- char errmsg[256] = "";
-
- if (!strcmp(getRequestMethod(), "POST")) {
- login_send_mail(errmsg, buff, maxlen);
- }
- %>
- <% @g
- /* Copyright(C) eyBuild Group, 2005-2007. All Rights Reserved. */
- static
- THIS_POST * recv_post(char * errmsg)
- {
- int max_post_size = 15 * (1024*1024); /* nM */
- POST_PARAM * p_param = NULL;
- /* has no enough memory */
- if (NULL == (p_param=createPostParam(max_post_size, max_post_size/3, 3, NULL, 0)))
- return NULL;
-
- /* set max size for mail content */
- if (OK != addPostItem(p_param, "msgtxt", 64000)) /* keep 1536 bytes */
- return NULL;
-
- /* done post */
- return dopost(p_param, errmsg);
- }
- static
- int get_login_cookie(char * pbuf, char * cookie)
- {
- char * qstart;
- int qlen = 0;
-
- /* separate SID */
- if (NULL == (qstart=strstr(pbuf, "SID=")))
- return ERROR;
- qlen = strstr(qstart, ";") - qstart;
- cookie += sprintf(cookie, "%.*s;", qlen, qstart);
- /* separate SPRIAL */
- if (NULL == (qstart=strstr(pbuf, "SPRIAL=")))
- return ERROR;
- qlen = strstr(qstart, ";") - qstart;
- cookie += sprintf(cookie, "%.*s;", qlen, qstart);
- /* separate SINA_USER */
- if (NULL == (qstart=strstr(pbuf, "SINA_USER=")))
- return ERROR;
- qlen = strstr(qstart, ";") - qstart;
- cookie += sprintf(cookie, "%.*s;", qlen, qstart);
- return OK;
- }
-
- static
- int get_phpurl(char * pbuf, char * phpurl)
- {
- char * qstart;
- int qlen = 0;
-
- /* separate Location: */
- if (NULL == (qstart=strstr(pbuf, "Location: http://")))
- return ERROR;
-
- qstart += 17;
- qlen = strstr(qstart, "/") - qstart;
- strncpy(phpurl, qstart, qlen);
-
- return OK;
- }
- static
- int get_phpsid(char * pbuf, char * cookie)
- {
- char * qstart;
- int qlen = 0;
-
- /* separate PHPSESSID */
- if (NULL == (qstart=strstr(pbuf, "PHPSESSID=")))
- return ERROR;
-
- qlen = strstr(qstart, ";") - qstart;
- sprintf(cookie+strlen(cookie), "%.*s", qlen, qstart);
-
- return OK;
- }
- static
- int mail_login(char * errmsg, char * pbuf, int maxlen, char * cookie, char * phpurl)
- {
- int sock = 0;
- char query[512] = "";
- int len;
- /* sendmail */
- if (strissp(G("sendmail")))
- return OK;
-
- if ((sock = sock_connect("mail.sina.com.cn", 80)) < 0)
- return ERROR;
-
- /* make query and http header */
- len = sprintf(query, "site=com&chatlogin=in&l=&user=&nick=&pass=&product=mail&grp=2&"
- "u=%.16s&psw=%.16s&mynum=1", G("from"), G("pswd"));
- len = sprintf(pbuf, "POST [url]http://mail.sina.com.cn/cgi-bin/login.cgi[/url] HTTP/1.0\r\n"
- "Content-Type: application/x-www-form-urlencoded\r\n"
- "Content-Length: %d\r\n"
- "Host: mail.sina.com.cn\r\n"
- "\r\n"
- "%.256s", len, query);
- /* send "login" to server,
- * receive the respond
- */
- if (sock_send(sock, pbuf, len) < 0 || sock_recv(sock, pbuf, maxlen) < 0)
- return ERROR;
- sock_close(sock);
-
- if (OK != get_login_cookie(pbuf, cookie) || OK != get_phpurl(pbuf, phpurl))
- {
- sprintf(errmsg, "Login error.");
- return ERROR;
- }
-
- /*
- * get "PHPSESSID" from mail3-xxx.sinamail.sina.com.cn
- */
- len = sprintf(pbuf, "GET http://%s/cgi/index.php?stype=jingdian HTTP/1.0\r\n"
- "Host: mail.sina.com.cn\r\n"
- "Cookie: %s\r\n\r\n",
- phpurl, cookie);
- /* connect phpserver, to get PHPSESSID cookie */
- if ((sock = sock_connect(phpurl, 80)) < 0)
- return ERROR;
- if (sock_send(sock, pbuf, len)<0 || sock_recv(sock, pbuf, maxlen)<0)
- return ERROR;
- sock_close(sock);
- if (OK != get_phpsid(pbuf, cookie))
- {
- sprintf(errmsg, "Get PHPSESSID error");
- return ERROR;
- }
-
- return OK;
- }
- static
- int parse_send_result(char * errmsg, char * pbuf)
- {
- char * qstart;
- int qlen = 0;
- char * str = "<div class=\"sendok_msg\">";
- char * pstr;
-
- /* separate result */
- if (NULL == (qstart=strstr(pbuf, str)))
- {
- sprintf(errmsg, "%s", "mail server no respond");
- return ERROR;
- }
-
- qstart += strlen(str);
- if (NULL == (pstr=strstr(qstart, "</div>")))
- {
- sprintf(errmsg, "%s", "mail body invalid.");
- return ERROR;
- }
-
- qlen = pstr - qstart;
- strncpy(errmsg, qstart, qlen);
- strtrim(errmsg);
-
- return OK;
- }
- static
- int mail_send(char * errmsg, char * pbuf, int maxlen, const char * cookie, char * phpurl, THIS_POST * p_post)
- {
- int sock = 0;
- int qlen = 0; /* query header length */
- char query[8192] = "";
- char * boundary = "---------------------------7d7138e4400b6";
- char * list[] = {"type", "issend", "fid", "mid",
- "to", "cc", "bcc", "subj", "msgtxt", "youxj"};
- char * attfile = NULL;
- char tail[256] = "";
- char * pstr;
- int i, n;
- FILE * fp;
- char buf[256];
- int len;
-
- if (NULL != (pstr=getContentType()) && NULL != (pstr=strstr(pstr, "boundary=")))
- boundary = pstr + strlen("boundary=");
- n = sizeof(list)/sizeof(list[0]);
- for (len=i=0; i<n; i++)
- {
- len += sprintf(pbuf+len, "--%s\r\nContent-Disposition: form-data; "
- "name=\"%s\"\r\n\r\n%s\r\n", boundary, list[i], G(list[i]));
- }
-
- if (getPostFileCount(p_post) > 0)
- {
- len += sprintf(pbuf+len, "--%s\r\nContent-Disposition: form-data; "
- "name=\"atth0\"; filename=\"%s\"\r\n\r\n",
- boundary, getPostSrcFileName(p_post, 0));
- len += file_size(attfile = getPostTempFileName(p_post, 0));
- }
-
- len += sprintf(tail, "--%s--\r\n\r\n", boundary);
-
- qlen = sprintf(query, "POST http://%s/cgismarty/sendmail.php HTTP/1.0\r\n"
- "Accept-Language: zh-cn\r\n"
- "Accept: */*\r\n"
- "Content-Type: multipart/form-data; boundary=%s\r\n"
- "Host: %s\r\n"
- "Content-Length: %d\r\n"
- "Pragma: no-cache\r\n"
- "Cookie: %s\r\n\r\n",
- phpurl, boundary, phpurl, len-2, cookie);
-
- /* send ==> receive ==> parse result */
- if ((sock = sock_connect(phpurl, 80)) < 0)
- {
- return ERROR;
- }
-
- /* send post header and post body */
- if (sock_send(sock, query, qlen) < 0 || sock_send(sock, pbuf, 0) < 0)
- return ERROR;
- /* send attached file */
- if (NULL != attfile && NULL != (fp=fopen(attfile, "rb")))
- {
- while(!feof(fp) && (len=fread(buf, 1, sizeof(buf), fp)))
- {
- if (sock_send(sock, buf, len) < 0)
- return ERROR;
- }
-
- sock_send(sock, "\r\n", 0);
- fclose(fp);
- }
-
- /* send body tail */
- if (sock_send(sock, tail, 0) < 0)
- return ERROR;
-
- if (sock_recv(sock, pbuf, maxlen) < 0)
- return ERROR;
- sock_close(sock);
-
- if (parse_send_result(errmsg, pbuf) < 0)
- return ERROR;
- return OK;
- }
- int login_send_mail(char * errmsg, char * pbuf, int maxlen)
- {
- THIS_POST * p_post = NULL;
- char cookie[4096] = "";
- char phpurl[256] = "";
-
- /* done post */
- if (NULL == (p_post=recv_post(errmsg)) )
- {
- if (strissp(errmsg))
- sprintf(errmsg, "receive post data error");
- return ERROR;
- }
-
- if (mail_login(errmsg, pbuf, maxlen, cookie, phpurl) < 0)
- {
- if (strissp(errmsg))
- sock_error(errmsg);
- return ERROR;
- }
-
- if (OK != mail_send(errmsg, pbuf, maxlen, cookie, phpurl, p_post))
- {
- if (strissp(errmsg))
- sock_error(errmsg);
- return ERROR;
- }
-
- return OK;
- }
- %>
复制代码
附件是编译好的 CGI(windows版), 可在任何支持 cgi 的服务器使用.
[ 本帖最后由 newzy 于 2007-9-19 17:18 编辑 ] |
-
-
modip.rar
61.13 KB, 下载次数: 134
可用的 cgi(windows版), 可在任何支持 cgi 的服务器使用.
|