Added utf-8 support for email headers (sender, recipients, subject).

This commit is contained in:
bluetiger9 2011-11-10 20:50:38 +02:00 committed by Attila
parent e339755a84
commit fbd6eb26a8
6 changed files with 60 additions and 13 deletions

View File

@ -21,7 +21,7 @@
MimeHtml::MimeHtml(const QString &html)
{
this->html = html;
this->encoding = PlainText;
this->encoding = _7Bit;
this->charset = "utf-8";
}
@ -73,9 +73,22 @@ void MimeHtml::prepare()
+ this->charset + "\n";
this->header += "Content-Transfer-Encoding: ";
this->header += (this->encoding == PlainText) ? "7bit\n" : "base64\n";
this->content = (this->encoding == PlainText) ? this->html.toAscii() : QByteArray().append(this->html).toBase64();
switch (encoding)
{
case _7Bit:
header += "7bit\n";
content = this->html.toAscii();
break;
case _8Bit:
header += "8bit\n";
content = this->html.toUtf8();
break;
case Base64:
header += "base64\n";
content = QByteArray().append(this->html).toBase64();
}
this->content += "\n";
}

View File

@ -54,6 +54,11 @@ void MimeMessage::addPart(MimePart *part)
this->parts << part;
}
void MimeMessage::useBase64InHeaders(bool use)
{
this->base64headers = use;
}
const EmailAddress & MimeMessage::getSender() const
{
return *sender;
@ -82,13 +87,25 @@ const QList<MimePart*> & MimeMessage::getParts() const
QString MimeMessage::toString()
{
QString mime;
mime = "From: " + sender->getName() + " <" + sender->getAddress() + ">\n";
mime = "From: ";
mime += (base64headers) ? ("=?utf-8?B?" + QByteArray().append(sender->getName()).toBase64() + "?=")
: sender->getName();
mime += " <" + sender->getAddress() + ">\n";
QList<EmailAddress*>::iterator it;
for (it = recipients.begin(); it != recipients.end(); ++it)
mime += "To: " + (*it)->getName() + " <" + (*it)->getAddress() + ">\n";
{
mime += "To: ";
mime += (base64headers) ? ("=?utf-8?B?" + QByteArray().append((*it)->getName()).toBase64() + "?=")
: (*it)->getName();
mime += " <" + (*it)->getAddress() + ">\n";
}
mime += "Subject: " + subject + "\n";
mime += "Subject: ";
mime += (base64headers) ? ("=?utf-8?B?" + QByteArray().append(subject).toBase64() + "?=")
: subject;
mime += "\n";
QString boundary = "----MIME-part-boundary=" + QByteArray().append(QDateTime::currentDateTime().toString()).toBase64() + "-end";

View File

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

View File

@ -65,6 +65,7 @@ const QString& MimePart::getContent() const
QString MimePart::toString()
{
prepare();
return header + "\n" + content;
}

View File

@ -27,13 +27,12 @@ public:
/* [0] Enumerations */
enum Encoding {
PlainText,
Base64,
_7Bit,
_8Bit,
QuotedPrintable // not implemented
_8Bit
};
/* [0] --- */

View File

@ -22,7 +22,7 @@ MimeText::MimeText(const QString &text)
{
this->text = text;
this->charset = "utf-8";
this->encoding = PlainText;
this->encoding = _7Bit;
}
MimeText::~MimeText() { }
@ -69,13 +69,26 @@ const QString & MimeText::getCharset() const
void MimeText::prepare()
{
this->header = "Content-Type: text/plain; charset: "
this->header = "Content-Type: text/plain; charset="
+ this->charset + "\n";
this->header += "Content-Transfer-Encoding: ";
this->header += (this->encoding == PlainText) ? "7bit\n" : "base64\n";
this->content = (this->encoding == PlainText) ? this->text.toAscii() : QByteArray().append(this->text).toBase64();
switch (encoding)
{
case _7Bit:
header += "7bit\n";
content = this->text.toAscii();
break;
case _8Bit:
header += "8bit\n";
content = this->text.toUtf8();
break;
case Base64:
header += "base64\n";
content = QByteArray().append(this->text).toBase64();
}
this->content += "\n";
}