From 37723d041bd78a487d3efafd0449c88f3a78b251 Mon Sep 17 00:00:00 2001 From: randomdan Date: Mon, 22 Nov 2010 10:25:56 +0000 Subject: Send a plain text part in emails --- project2/sendmailTask.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/project2/sendmailTask.cpp b/project2/sendmailTask.cpp index 3fc0039..65d55d8 100644 --- a/project2/sendmailTask.cpp +++ b/project2/sendmailTask.cpp @@ -1,12 +1,15 @@ #include "sendmailTask.h" #include -#include #include "xmlObjectLoader.h" #include "appEngine.h" #include "presenter.h" #include +#include +#include #include #include +#include +#include ElementLoaderImpl<_SendMailTask> sendmailLoader("sendmail"); @@ -80,6 +83,44 @@ class HtmlContent : public _SendMailTask::MailPart { private: const XmlDocumentPtr doc; }; +class TextContent : public _SendMailTask::MailPart { + public: + TextContent(XmlDocumentPtr d) : doc(d) { + } + const char * write(char ** buf, int * pos) { + int fds[2]; + const char * callLynx[] = { +#ifdef STRACE_LYNX + "/usr/bin/strace", "-o", "/tmp/lynx", +#endif + "/usr/bin/lynx", "-dump", "-stdin", "-width=105", NULL }; + popenrw(callLynx, fds); + FILE * lynxIn = fdopen(fds[0], "w"); + FILE * lynxOut = fdopen(fds[1], "r"); + htmlNodeDumpFile(lynxIn, doc.get(), xmlDocGetRootElement(doc.get())); + fclose(lynxIn); + close(fds[0]); + int len = 0; + *pos = 0; + for (int ch ; ((ch = fgetc(lynxOut)) >= 0); ) { + if (*pos >= len) { + len += BUFSIZ; + *buf = (char*)realloc(*buf, len); + } + (*buf)[(*pos)++] = ch; + } + fclose(lynxOut); + close(fds[1]); + int status; + wait(&status); + if (status != 0) { + throw std::runtime_error("Lynx failed"); + } + return *buf; + } + private: + const XmlDocumentPtr doc; +}; void _SendMailTask::execute() const @@ -105,6 +146,8 @@ _SendMailTask::execute() const parts.push_back(new Header("Content-Type", "multipart/alternative; boundary=\"<>\"")); parts.push_back(new Header("MIME-Version", "1.0")); parts.push_back(new Header("Content-Transfer-Encoding", "binary")); + parts.push_back(new BoundaryBegin("text/plain; utf-8")); + parts.push_back(new TextContent(result)); parts.push_back(new BoundaryBegin("text/html; utf-8")); parts.push_back(new HtmlContent(result)); parts.push_back(new BoundaryEnd()); -- cgit v1.2.3