In the context of a personal ad-hoc app (I will come to that later) that I wrote for my Nokia N9, I needed to send an email to a specific person with an attachment. After the first research at Harmattan APIs you come to QMessageService.
The first thing I did was writing Mixed QML/Qt object that I could instantiate from the QML code so that I could do something like:
Message { id: message from: "my@Address" to: [ "destination@Address" ] subject: "This is not Spam for sure!" body: "Trolled! Enlarge...!" attachments: [ "/a/path/to/an/attachment" ] } Button { onClicked: message.compose() // onClicked: message.send() }
There we have an object with two send
and compose
methods, three string properties representing the from, subject and body and two string list properties representing the to and attachments (we leave the CC and BCC as an exercise 😉 ). As I already explained how to create the objects with the properties in previous posts, I’ll go directly to the compose
and send
methods.
const QMessage Message::createMessage() { QMessage message; message.setFrom(QMessageAddress(QMessageAddress::Email, m_from)); QMessageAddressList toList; foreach (const QString &item, m_to) { toList.append(QMessageAddress(QMessageAddress::Email, item)); } message.setTo(toList); message.setSubject(m_subject); message.setBody(m_body); message.appendAttachments(m_attachments); return message; } bool Message::send() { QMessage message = createMessage(); service.send(message); return true; } bool Message::compose() { QMessage message = createMessage(); service.compose(message); return true; }
Easy, right?
If you want to be completely trolled stop reading here. If not, please continue.
That does not work, at least I could not make it work. I might be missing something, but I could not. I hooked to the signal and saw the state changes, that were going from sending to Ok and no email composer was showing up. No error was being returned either.
Finally, I got an easier solution by using Qt.openUrlExternally and mailto:
directly from QML. You can use obscure parameters to compose the message the way you want. Example:
Button { onClicked: Qt.openUrlExternally("mailto:destionation@Address" + "?subject=Subject" + "&attach=/path/to/the/attachment" + "&body=Body") }
That does the job and it is less complicated than creating a whole class. I leave the whole message class code in a pastebin in case somebody wants to try it or develop something from there in a platform where the QMessageService
really works.
Ad-hoc program
My wife tracks all our finances using Home Bank and I needed an easy way to write down the cash expenses. I decided to code a simple program easying me to manage expenses by adding them to a list (and deleting them) and export them as CSV in order to be sent through email when she requested them. As the CSV and the expenses type were so ad-hoc, I decided not to complicate it and cable them in the code and database.
I don’t think it can be interesting to anybody, but I might end up uploading it to some git repo so that it can be maintained by someone else or even forked.