by bob » 14 Jan 2011, 23:00
I needed a way to set the 'Content-Id' on message parts to allow cid urls to work. The following patch allows adding additional headers to mime parts.
- Code: Select all
Index: Net/include/Poco/Net/MailMessage.h
===================================================================
--- Net/include/Poco/Net/MailMessage.h (revision 1431)
+++ Net/include/Poco/Net/MailMessage.h (working copy)
@@ -161,6 +161,11 @@
bool isMultipart() const;
/// Returns true iff the message is a multipart message.
+ void setPartHeader( const std::string& part, const std::string& name, const std::string& value);
+ /// Adds the specified name/value pair to the part headers
+ ///
+ /// Passing an empty string as the value will delete the header value
+
void addPart(const std::string& name, PartSource* pSource, ContentDisposition disposition, ContentTransferEncoding encoding);
/// Adds a part/attachment to the mail message.
///
@@ -222,12 +227,14 @@
/// consists only of ASCII characters.
protected:
+ typedef std::map<std::string, std::string> StringMap;
struct Part
{
std::string name;
PartSource* pSource;
ContentDisposition disposition;
ContentTransferEncoding encoding;
+ StringMap headers;
};
typedef std::vector<Part> PartVec;
Index: Net/src/MailMessage.cpp
===================================================================
--- Net/src/MailMessage.cpp (revision 1431)
+++ Net/src/MailMessage.cpp (working copy)
@@ -226,7 +226,23 @@
_parts.push_back(part);
}
+void MailMessage::setPartHeader( const std::string& part, const std::string& name, const std::string& value)
+{
+ if ( !part.length() || !name.length() )
+ return;
+ for (PartVec::iterator it = _parts.begin(); it != _parts.end(); ++it)
+ if (part == it->name)
+ {
+ if ( value.length() )
+ it->headers[ name ] = value;
+ else
+ it->headers.erase( name );
+
+ return;
+ }
+}
+
void MailMessage::addContent(PartSource* pSource, ContentTransferEncoding encoding)
{
addPart("", pSource, CONTENT_INLINE, encoding);
@@ -335,6 +351,8 @@
}
else disposition = "inline";
partHeader.set(HEADER_CONTENT_DISPOSITION, disposition);
+ for (StringMap::const_iterator it = part.headers.begin(); it != part.headers.end(); ++it)
+ partHeader.set(it->first, it->second);
writer.nextPart(partHeader);
writeEncoded(part.pSource->stream(), writer.stream(), part.encoding);
}