summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2010-11-22 10:25:56 +0000
committerrandomdan <randomdan@localhost>2010-11-22 10:25:56 +0000
commit39dc2bcc49d109816623c4ef7106ec53a1157cb6 (patch)
tree114423c7959de8d4376ead9a436f2fc9289667e4
parentTidy up sendmail process ready for sending plain text version too (diff)
downloadproject2-39dc2bcc49d109816623c4ef7106ec53a1157cb6.tar.bz2
project2-39dc2bcc49d109816623c4ef7106ec53a1157cb6.tar.xz
project2-39dc2bcc49d109816623c4ef7106ec53a1157cb6.zip
Send a plain text part in emails
-rw-r--r--project2/sendmailTask.cpp45
1 files changed, 44 insertions, 1 deletions
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 <boost/foreach.hpp>
-#include <stdexcept>
#include "xmlObjectLoader.h"
#include "appEngine.h"
#include "presenter.h"
#include <libxslt/transform.h>
+#include <misc.h>
+#include <stdexcept>
#include <libesmtp.h>
#include <libxslt/xsltutils.h>
+#include <libxml/HTMLtree.h>
+#include <sys/wait.h>
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=\"<<divider>>\""));
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());