Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

March 24, 2008

C++ Source for Online Spam-Assassin Score Tester

A Chilkat customer had questions about SPAM Assassin scoring, and given that all email received at chilkatsoft.com is analyzed by SPAM Assassin, it turned out to be a simple task to provide an online tool for SPAM Assassin score testing.

An email address was created for this purpose: sat@chilkatsoft.com. Anybody may send an email to this address, and then check it using this online tool: http://www.chilkatsoft.com/satCheck.html
The only way somebody else can read your email is if the correct "FROM" address is provided in the online tool. Emails are automatically deleted from the mailbox after a few days.

How was this tool implemented? It’s nothing more than a simple C++ CGI that receives an HTTP POST, reads the mailbox, and displays the SPAM-Assassin related headers. Here’s the complete source code:

// SatCheck.cpp : Defines the entry point for the console application.
//
	
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include "c:/ck2000/components/ChilkatLib/package/include/CkString.h"
#include "c:/ck2000/components/ChilkatLib/package/include/CkMailMan.h"
#include "c:/ck2000/components/ChilkatLib/package/include/CkEmail.h"
#include "c:/ck2000/components/ChilkatLib/package/include/CkEmailBundle.h"
#include "c:/ck2000/components/ChilkatLib/package/include/CkCGI.h"
	
static void handleRequest(CkString &html)
    {
    CkCGI cgi;
	
    bool b = cgi.ReadRequest();
    if (!b)
	{
	html.append("<html><body>Failed to read request.</body></html>");
	return;
	}
	
    html.append("<html><head>");
    html.append("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\" />");
    html.append("<link href=\"/css/sat.css\" rel=\"stylesheet\" type=\"text/css\" />");
    html.append("<title>SPAM Assassin Test Results</title>");
    html.append("</head><body>");
	
    html.append("<p><a href=\"http://www.chilkatsoft.com/\"><img src=\"/images/logoSat.jpg\" border=0 /></a></p>");
    html.append("<h1>SPAM Assassin Test Results <span id=\"comp\">Compliments of <a id=\"comp\" href=\"http://www.chilkatsoft.com/\">Chilkat Software</a></span></h1>");
	
const char *l1 = "<h4>Chilkat SMTP / POP3 Components for <a href=\"/email-dotnet.asp\">.NET</a>   \
<a href=\"/email-activex.asp\">ActiveX</a>   \
<a href=\"/email-library.asp\">C++ Libs</a>   \
<a href=\"/ruby-email.asp\">Ruby</a>   \
<a href=\"/python-email.asp\">Python</a>   \
<a href=\"/perl-email.asp\">Perl</a>   \
<a href=\"/java-email.asp\">Java</a></h4>";
	
const char *l2 = "<h4>Chilkat IMAP Components for <a href=\"/imap-dotnet.asp\">.NET</a>   \
<a href=\"/imap-activex.asp\">ActiveX</a>   \
<a href=\"/imap-library.asp\">C++ Libs</a>   \
<a href=\"/ruby-imap.asp\">Ruby</a>   \
<a href=\"/python-imap.asp\">Python</a>   \
<a href=\"/perl-imap.asp\">Perl</a>   \
<a href=\"/java-imap.asp\">Java</a></h4>";
	
    html.append(l1);
    html.append(l2);
	
    CkString sFromAddr;
    sFromAddr.append(cgi.getParam("emailAddr"));
    sFromAddr.trim2();
    if (!sFromAddr.getNumChars())
	{
 	html.append("<p>Error: FROM address is empty</body></html>");
	return;
	}
	
    CkMailMan mailman;
	
    bool success;
    success = mailman.UnlockComponent("Anything for 30-day trial.");
    if (success != true)
	{
        html.append("<p>Error: Internal error - please notify support@chilkatsoft.com.");
	html.append("</body></html>");
        return;
	}
	
    //  Set the POP3 server's hostname
    mailman.put_MailHost("mail.chilkatsoft.com");
	
    //  Set the POP3 login/password.
    mailman.put_PopUsername("sat@chilkatsoft.com");
    mailman.put_PopPassword("myPassword");
	
    CkEmailBundle *bundle = 0;
    //  Read mail headers and one line of the body.
    bundle = mailman.GetAllHeaders(1);
	
    if (bundle == 0 )
	{
	html.append("<h2>Error:</h2><p>");
        html.append(mailman.lastErrorHtml());
 	html.append("<p>Please notify support@chilkatsoft.com</body></html>");
	return;
	}
	
    int i;
    CkEmail *email = 0;
    CkEmail *tempEmail = 0;
    CkString eFromAddr;
    for (i = bundle->get_MessageCount() - 1; i >= 0; i--)
	{
        tempEmail = bundle->GetEmail(i);
	if (!tempEmail) continue;
	
	eFromAddr.clear();
	tempEmail->get_FromAddress(eFromAddr);
	if (eFromAddr.equalsIgnoreCaseStr(sFromAddr))
	    {
	    if (!email)
		{
		email = tempEmail;
		tempEmail = 0;
		}
	    else
		{
		// Delete all other emails with the same email address.
		mailman.DeleteEmail(*tempEmail);
		}
	    }
	
        delete tempEmail;
	tempEmail = 0;
    }
	
    mailman.Pop3EndSession();
	
    if (email)
	{
	CkString strTemp;
	strTemp.put_Utf8(true);
	
	email->put_Utf8(true);
	
	//X-Spam-Status, X-Spam-Level, X-Spam-Checker-Version and (conditionally) X-Spam-Flag
	
	html.append("<table>");
	
	// Get the headers.
	html.append("<tr>");
	html.append("<td valign=\"top\">");
	html.append("From: ");
	html.append("</td>");
	html.append("<td valign=\"top\">");
	html.append(email->fromAddress());
	html.append("</td>");
	html.append("</tr>");
	
	html.append("<tr>");
	html.append("<td valign=\"top\">");
	html.append("Subject: ");
	html.append("</td>");
	html.append("<td valign=\"top\">");
	html.append(email->subject());
	html.append(" </td>");
	html.append("</tr>");
	
	html.append("<tr>");
	html.append("<td valign=\"top\">");
	html.append("Date: ");
	html.append("</td>");
	html.append("<td valign=\"top\">");
	SYSTEMTIME sysTime;
	email->get_EmailDate(sysTime);
	strTemp.clear();
	strTemp.appendDateRfc822(sysTime);
	html.append(strTemp.getString());
	html.append("</td>");
	html.append("</tr>");
	
	//
	// X-Spam-Status
	//
	html.append("<tr>");
	html.append("<td valign=\"top\">");
	html.append("X-Spam-Status: ");
	html.append("</td>");
	html.append("<td valign=\"top\">");
	
	strTemp.clear();
	email->GetHeaderField("X-Spam-Status",strTemp);
	strTemp.replaceAllOccurances(",","<br>");
	strTemp.replaceAllOccurances(" autolearn","<br>autolearn");
	if (!strTemp.containsSubstring("<br>autolearn"))
	    {
	    strTemp.replaceFirstOccurance("autolearn","<br>autolearn");
	    }
	strTemp.replaceAllOccurances("\tversion","<br>version");
	strTemp.replaceAllOccurances(" version","<br>version");
	strTemp.replaceAllOccurances(" hits","<br>hits");
	strTemp.replaceAllOccurances(" required","<br>required");
	strTemp.replaceAllOccurances(" tests","<br>tests");
	html.append(strTemp.getString());
	
	html.append("</td>");
	html.append("</tr>");
	
	//
	// X-Spam-Flag
	//
	html.append("<tr>");
	html.append("<td valign=\"top\">");
	html.append("X-Spam-Flag: ");
	html.append("</td>");
	html.append("<td valign=\"top\">");
	
	strTemp.clear();
	email->GetHeaderField("X-Spam-Flag",strTemp);
	strTemp.replaceAllOccurances(",","<br>");
	if (strTemp.getNumChars())
	    {
	    html.append(strTemp.getString());
	    }
	else
	    {
	    html.append("Header field not present.");
	    }
	
	html.append("</td>");
	html.append("</tr>");
	
	//
	// X-Spam-Report
	//
	html.append("<tr>");
	html.append("<td valign=\"top\">");
	html.append("X-Spam-Report: ");
	html.append("</td>");
	html.append("<td valign=\"top\">");
	
	strTemp.clear();
	email->GetHeaderField("X-Spam-Report",strTemp);
	strTemp.replaceAllOccurances("*","<br>*");
	strTemp.replaceFirstOccurance("<br>*","*");
	if (strTemp.getNumChars())
	    {
	    html.append(strTemp.getString());
	    }
	else
	    {
	    html.append("Header field not present.");
	    }
	
	html.append("</td>");
	html.append("</tr>");
	
	html.append("<tr>");
	html.append("<td valign=\"top\">");
	html.append("X-Spam-Checker-Version: ");
	html.append("</td>");
	html.append("<td valign=\"top\">");
	
	strTemp.clear();
	email->GetHeaderField("X-Spam-Checker-Version",strTemp);
	strTemp.chopAtFirstChar(')');
	strTemp.appendChar(')');
	html.append(strTemp.getUtf8());
	
	html.append("</td>");
	html.append("</tr>");
	
	html.append("</table>");
	
	}
    else
	{
	html.append("<p>Failed to find email with FROM address: ");
	html.append(sFromAddr.getString());
	html.append("</p>");
	}
	
    delete bundle;
	
    html.append("<h2><a href=\"/satCheck.html\">Back</a></h2>");
    html.append("<center>Copyright © 2008 Chilkat Software, Inc.</center>");
    html.append("</body></html>");
	
    return;
    }
	
int main(int argc, char* argv[])
{
    // Make sure stdout is binary so line-endings aren't modified
    // when calling printf.
    setmode(fileno(stdout), O_BINARY);
	
    CkString html;
    handleRequest(html);
	
    printf("Content-type: text/html; charset=utf-8\n");
	
    CkString currentDate;
    currentDate.appendCurrentDateRfc822();
    printf("Last-Modified: %s\n",currentDate.getString());
	
    // Add some additional headers...
    printf("Cache-Control: private, s-maxage=0, max-age=0, must-revalidate\n");
    printf("Connection: close\n");
	
    printf("Content-Length: %d\n\n",html.getSizeUtf8());
	
    // Output the HTTP response body.
    fwrite(html.getUtf8(),html.getSizeUtf8(),1,stdout);
	
    return 0;
}


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.