Skip to content

Instantly share code, notes, and snippets.

@nathan-osman
Last active April 8, 2016 05:56
Show Gist options
  • Select an option

  • Save nathan-osman/4b42091d215521131d024f2d8cc94aad to your computer and use it in GitHub Desktop.

Select an option

Save nathan-osman/4b42091d215521131d024f2d8cc94aad to your computer and use it in GitHub Desktop.
Send an email to Hectane with Qt
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
project(emailer)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
find_package(Qt5Network 5.4 REQUIRED)
add_executable(emailer emailer.cpp)
set_property(TARGET emailer PROPERTY CXX_STANDARD 11)
qt5_use_modules(emailer Network)
#include <iostream>
#include <QCoreApplication>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
// Create the email
QJsonObject message({
{ "from", "alice@example.com" },
{ "to", QJsonArray({"bob@example.com"}) },
{ "subject", "Test Email" },
{ "text", "This is the body of the email!" }
});
QByteArray payload(QJsonDocument(message).toJson());
// Create a network access manager
QNetworkAccessManager manager;
// Create the request for Hectane, running on port 8025 by default
QNetworkRequest request(QUrl("http://localhost:8025/v1/send"));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
// Send the request
QNetworkReply *reply = manager.post(request, payload);
// Indicate what happened when the request completes and then shutdown
QObject::connect(reply, &QNetworkReply::finished, [&app, reply]() {
if (reply->error() == QNetworkReply::NoError) {
std::cout << "Success! Email delivered." << std::endl;
} else {
std::cout << "Error: " << reply->errorString().toStdString() << std::endl;
}
app.quit();
});
return app.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment