The header's encoding can be now selected.

The subject, sender's and the recipient's names can be also ecoded with
base64 and quoted-printable.
See the MimeMessage::setHeaderEncoding() function.
This commit is contained in:
bluetiger9 2011-11-18 21:58:16 +02:00 committed by Attila
parent 2b7f475766
commit 735fa2f4dd
2 changed files with 64 additions and 12 deletions

View File

@ -17,6 +17,7 @@
#include "mimemessage.h" #include "mimemessage.h"
#include <QDateTime> #include <QDateTime>
#include "quotedprintable.h"
/* [1] Constructors and Destructors */ /* [1] Constructors and Destructors */
@ -54,9 +55,9 @@ void MimeMessage::addPart(MimePart *part)
this->parts << part; this->parts << part;
} }
void MimeMessage::useBase64InHeaders(bool use) void MimeMessage::setHeaderEncoding(MimePart::Encoding hEnc)
{ {
this->base64headers = use; this->hEncoding = hEnc;
} }
const EmailAddress & MimeMessage::getSender() const const EmailAddress & MimeMessage::getSender() const
@ -87,24 +88,69 @@ const QList<MimePart*> & MimeMessage::getParts() const
QString MimeMessage::toString() QString MimeMessage::toString()
{ {
QString mime; QString mime;
/* =========== MIME HEADER ============ */
/* ---------- Sender / From ----------- */
mime = "From:"; mime = "From:";
mime += (base64headers) ? ("=?utf-8?B?" + QByteArray().append(sender->getName()).toBase64() + "?=") if (sender->getName() != "")
: sender->getName(); {
switch (hEncoding)
{
case MimePart::Base64:
mime += " =?utf-8?B?" + QByteArray().append(sender->getName()).toBase64() + "?=";
break;
case MimePart::QuotedPrintable:
mime += " =?utf-8?Q?" + QuotedPrintable::encode(QByteArray().append(sender->getName())).replace(' ', "_").replace(':',"=3A") + "?=";
break;
default:
mime += " " + sender->getName();
}
}
mime += " <" + sender->getAddress() + ">\n"; mime += " <" + sender->getAddress() + ">\n";
/* ---------------------------------- */
/* ------- Recipients / To ---------- */
QList<EmailAddress*>::iterator it; QList<EmailAddress*>::iterator it;
for (it = recipients.begin(); it != recipients.end(); ++it) for (it = recipients.begin(); it != recipients.end(); ++it)
{ {
mime += "To:"; mime += "To:";
mime += (base64headers) ? ("=?utf-8?B?" + QByteArray().append((*it)->getName()).toBase64() + "?=")
: (*it)->getName(); if ((*it)->getName() != "")
{
switch (hEncoding)
{
case MimePart::Base64:
mime += " =?utf-8?B?" + QByteArray().append((*it)->getName()).toBase64() + "?=";
break;
case MimePart::QuotedPrintable:
mime += " =?utf-8?Q?" + QuotedPrintable::encode(QByteArray().append((*it)->getName())).replace(' ', "_").replace(':',"=3A") + "?=";
break;
default:
mime += " " + (*it)->getName();
}
}
mime += " <" + (*it)->getAddress() + ">\n"; mime += " <" + (*it)->getAddress() + ">\n";
} }
/* ---------------------------------- */
/* ------------ Subject ------------- */
mime += "Subject: "; mime += "Subject: ";
mime += (base64headers) ? ("=?utf-8?B?" + QByteArray().append(subject).toBase64() + "?=")
: subject; switch (hEncoding)
{
case MimePart::Base64:
mime += "=?utf-8?B?" + QByteArray().append(subject).toBase64() + "?=";
break;
case MimePart::QuotedPrintable:
mime += "=?utf-8?Q?" + QuotedPrintable::encode(QByteArray().append(subject)).replace(' ', "_").replace(':',"=3A") + "?=";
break;
default:
mime += subject;
}
/* ---------------------------------- */
mime += "\n"; mime += "\n";
QString boundary = "----MIME-part-boundary=" + QByteArray().append(QDateTime::currentDateTime().toString()).toBase64() + "-end"; QString boundary = "----MIME-part-boundary=" + QByteArray().append(QDateTime::currentDateTime().toString()).toBase64() + "-end";
@ -112,14 +158,20 @@ QString MimeMessage::toString()
mime += "MIME-Version: 1.0\n"; mime += "MIME-Version: 1.0\n";
mime += "Content-type: multipart/mixed; boundary=\"" + boundary + "\"\n\n"; mime += "Content-type: multipart/mixed; boundary=\"" + boundary + "\"\n\n";
/* ====== END OF MIME HEADER ======== */
boundary = "--" + boundary; boundary = "--" + boundary;
/* ========== MIME BODY ============= */
QList<MimePart*>::iterator i; QList<MimePart*>::iterator i;
for (i = parts.begin(); i != parts.end(); ++i) for (i = parts.begin(); i != parts.end(); ++i)
mime += boundary + "\n" + (*i)->toString(); mime += boundary + "\n" + (*i)->toString();
mime += boundary + "--\n"; mime += boundary + "--\n";
/* ====== END OF MIME BODY ========= */
return mime; return mime;
} }

View File

@ -40,7 +40,7 @@ public:
void setSubject(const QString & subject); void setSubject(const QString & subject);
void addPart(MimePart* part); void addPart(MimePart* part);
void useBase64InHeaders(bool use); void setHeaderEncoding(MimePart::Encoding);
const EmailAddress & getSender() const; const EmailAddress & getSender() const;
const QList<EmailAddress*> & getRecipients() const; const QList<EmailAddress*> & getRecipients() const;
@ -65,7 +65,7 @@ protected:
QString subject; QString subject;
QList<MimePart*> parts; QList<MimePart*> parts;
bool base64headers; MimePart::Encoding hEncoding;
/* [4] --- */ /* [4] --- */