Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Thinking In C++, 2nd Edition, Volume 2 Standard Libraries& Advanced Topics - Eckel B..pdf
Скачиваний:
314
Добавлен:
24.05.2014
Размер:
2.09 Mб
Скачать

Both the input file and the output file in main( ) are manipulated with standard I/O, since it’s not a good idea to mix I/O types in a program. Each line is read using fgets( ), and if one of them matches with the starts StringSet, then what follows will contain email addresses, until you see some dashes (I figured this out empirically, by hunting through a file full of bounced email). The continues StringSet contains strings whose lines should be ignored. For each of the lines that potentially contains an addresses, each address is extracted using the Standard C Library function strtok( ) and then it is added to the set<string> called names. Using a set eliminates duplicates (you may have duplicates based on case, but those are dealt with by RemoveGroup.cpp. The resulting set of names is then printed to the output file.

Mailing to your list

There are a number of ways to connect to your system’s mailer, but the following program just takes the simple approach of calling an external command (“fastmail,” which is part of Unix) using the Standard C library function system( ). The program spends all its time building the external command.

When people don’t want to be on a list anymore they will often ignore instructions and just reply to the message. This can be a problem if the email address they’re replying with is different than the one that’s on your list (sometimes it has been routed to a new or aliased address). To solve the problem, this program prepends the text file with a message that informs them that they can remove themselves from the list by visiting a URL. Since many email programs will present a URL in a form that allows you to just click on it, this can produce a very simple removal process. If you look at the URL, you can see it’s a call to the mlm.exe CGI program, including removal information that incorporates the same email address the message was sent to. That way, even if the user just replies to the message, all you have to do is click on the URL that comes back with their reply (assuming the message is automatically copied back to you).

//: C10:Batchmail.cpp

// Sends

mail to a

list using Unix fastmail

#include

"../require.h"

#include

<iostream>

 

#include

<fstream>

 

#include

<string>

 

#include

<strstream>

#include

<cstdlib>

// system() function

using namespace std;

string subject("New Intensive Workshops"); string from("Bruce@EckelObjects.com"); string replyto("Bruce@EckelObjects.com"); ofstream logfile("BatchMail.log");

int main(int argc, char* argv[]) {

Appendix B: Programming Guidelines

558

requireArgs(argc, 2,

"Usage: Batchmail namelist mailfile"); ifstream names(argv[1]);

assure(names, argv[1]); string name; while(getline(names, name)) {

ofstream msg("m.txt"); assure(msg, "m.txt");

msg << "To be removed from this list, "

"DO NOT REPLY TO THIS MESSAGE. Instead, \n" "click on the following URL, or visit it " "using your Web browser. This \n"

"way, the proper email address will be " "removed. Here's the URL:\n"

<<"http://www.mindview.net/cgi-bin/" "mlm.exe?subject-field=workshop-email-list" "&command-field=remove&email-address="

<<name << "&submit=submit\n\n"

"------------------------------------\n\n"; ifstream text(argv[2]);

assure(text, argv[1]);

msg << text.rdbuf() << endl; msg.close();

string command("fastmail -F " + from +

" -r " + replyto + " -s \"" + subject + "\" m.txt " + name);

system(command.c_str()); logfile << command << endl; static int mailcounter = 0; const int bsz = 25;

char buf[bsz];

// Convert mailcounter to a char string: ostrstream mcounter(buf, bsz);

mcounter << mailcounter++ << ends; if((++mailcounter % 500) == 0) {

string command2("fastmail -F " + from +

"-r " + replyto + " -s \"Sent " + string(buf) +

"messages \" m.txt eckel@aol.com"); system(command2.c_str());

}

}

} ///:~

Appendix B: Programming Guidelines

559

Соседние файлы в предмете Программирование