5#include "viewemail_p.h"
7#include <Cutelyst/application.h>
8#include <Cutelyst/context.h>
9#include <Cutelyst/response.h>
10#include <SimpleMail/emailaddress.h>
11#include <SimpleMail/mimemessage.h>
12#include <SimpleMail/mimetext.h>
13#include <SimpleMail/serverreply.h>
15#include <QtCore/QLoggingCategory>
17Q_LOGGING_CATEGORY(CUTELYST_VIEW_EMAIL,
"cutelyst.view.email", QtWarningMsg)
20using namespace SimpleMail;
23 :
View(new ViewEmailPrivate, parent, name)
44 return d->defaultContentType;
50 d->defaultContentType = contentType;
57 return d->defaultCharset;
63 d->defaultCharset = charset;
70 return d->defaultEncoding;
76 d->defaultEncoding = encoding;
83 return d->server->host();
89 d->server->setHost(host);
95 return d->server->port();
101 d->server->setPort(quint16(port));
113 d->server->setConnectionType(
static_cast<Server::ConnectionType
>(ct));
125 d->server->setAuthMethod(
static_cast<Server::AuthMethod
>(method));
131 return d->server->username();
137 d->server->setUsername(user);
143 return d->server->password();
149 d->server->setPassword(password);
156 QVariantHash email = c->
stash(d->stashKey).toHash();
157 if (email.isEmpty()) {
159 "Cannot render template, template name or template stash key not defined"));
166 value = email.value(QStringLiteral(
"to"));
168 message.addTo(SimpleMail::EmailAddress{value.toString()});
170 const auto rcpts = value.toStringList();
171 for (
const QString &rcpt : rcpts) {
172 message.addTo(SimpleMail::EmailAddress{rcpt});
176 value = email.value(QStringLiteral(
"cc"));
178 message.addCc(SimpleMail::EmailAddress{value.toString()});
180 const auto rcpts = value.toStringList();
181 for (
const QString &rcpt : rcpts) {
182 message.addCc(SimpleMail::EmailAddress{rcpt});
186 value = email.value(QStringLiteral(
"bcc"));
188 message.addBcc(SimpleMail::EmailAddress{value.toString()});
190 const auto rcpts = value.toStringList();
191 for (
const QString &rcpt : rcpts) {
192 message.addBcc(SimpleMail::EmailAddress{rcpt});
196 message.setSender(SimpleMail::EmailAddress{email.value(QStringLiteral(
"from")).toString()});
197 message.setSubject(email.value(QStringLiteral(
"subject")).toString());
202 c->
appendError(QStringLiteral(
"Can't send email without parts or body, check stash"));
207 const QVariantList partsVariant = parts.
toList();
208 for (
const QVariant &part : partsVariant) {
209 auto mime = part.value<std::shared_ptr<MimePart>>();
211 message.addPart(mime);
213 qCCritical(CUTELYST_VIEW_EMAIL) <<
"Failed to cast MimePart";
217 auto contentTypeIt = email.constFind(QStringLiteral(
"content_type"));
218 if (contentTypeIt != email.constEnd() && !contentTypeIt.value().isNull() &&
219 !contentTypeIt.value().toString().isEmpty()) {
220 const QByteArray contentType = contentTypeIt.value().toString().toLatin1();
221 qCDebug(CUTELYST_VIEW_EMAIL) <<
"Using specified content_type" << contentType;
222 message.getContent().setContentType(contentType);
223 }
else if (!d->defaultContentType.isEmpty()) {
224 qCDebug(CUTELYST_VIEW_EMAIL) <<
"Using default content_type" << d->defaultContentType;
225 message.getContent().setContentType(d->defaultContentType);
228 auto part = std::make_shared<MimeText>(body.
toString());
229 d->setupAttributes(part, email);
230 message.setContent(part);
233 ServerReply *reply = d->server->sendMail(message);
234 connect(reply, &ServerReply::finished, reply, &ServerReply::deleteLater);
240 :
View(d, parent, name)
245void ViewEmail::initSender()
248 d->server =
new Server(
this);
251 const auto app = qobject_cast<Application *>(
parent());
253 config = app->config(QStringLiteral(
"VIEW_EMAIL")).toHash();
256 d->stashKey = config.value(QStringLiteral(
"stash_key"), QStringLiteral(
"email")).toString();
258 if (!config.value(QStringLiteral(
"sender_host")).isNull()) {
259 d->server->setHost(config.value(QStringLiteral(
"sender_host")).toString());
261 if (!config.value(QStringLiteral(
"sender_port")).isNull()) {
262 d->server->setPort(quint16(config.value(QStringLiteral(
"sender_port")).toInt()));
264 if (!config.value(QStringLiteral(
"sender_username")).isNull()) {
265 d->server->setUsername(config.value(QStringLiteral(
"sender_username")).toString());
267 if (!config.value(QStringLiteral(
"sender_password")).isNull()) {
268 d->server->setPassword(config.value(QStringLiteral(
"sender_password")).toString());
272void ViewEmailPrivate::setupAttributes(std::shared_ptr<MimePart> part,
273 const QVariantHash &attrs)
const
275 auto contentTypeIt = attrs.constFind(QStringLiteral(
"content_type"));
276 if (contentTypeIt != attrs.constEnd() && !contentTypeIt.value().isNull() &&
277 !contentTypeIt.value().toString().isEmpty()) {
278 const QByteArray contentType = contentTypeIt.value().toString().toLatin1();
279 qCDebug(CUTELYST_VIEW_EMAIL) <<
"Using specified content_type" << contentType;
280 part->setContentType(contentType);
281 }
else if (!defaultContentType.isEmpty()) {
282 qCDebug(CUTELYST_VIEW_EMAIL) <<
"Using default content_type" << defaultContentType;
283 part->setContentType(defaultContentType);
286 auto charsetIt = attrs.constFind(QStringLiteral(
"charset"));
287 if (charsetIt != attrs.constEnd() && !charsetIt.value().isNull() &&
288 !charsetIt.value().toString().isEmpty()) {
289 const QByteArray charset = charsetIt.value().toString().toLatin1();
290 qCDebug(CUTELYST_VIEW_EMAIL) <<
"Using specified charset" << charset;
291 part->setCharset(charset);
292 }
else if (!defaultCharset.isEmpty()) {
293 qCDebug(CUTELYST_VIEW_EMAIL) <<
"Using default charset" << defaultCharset;
294 part->setCharset(defaultCharset);
297 auto encodingIt = attrs.constFind(QStringLiteral(
"encoding"));
298 if (encodingIt != attrs.constEnd() && !encodingIt.value().isNull() &&
299 !encodingIt.value().toString().isEmpty()) {
300 const QByteArray encoding = encodingIt.value().toString().toLatin1();
301 qCDebug(CUTELYST_VIEW_EMAIL) <<
"Using specified encoding" << encoding;
302 setupEncoding(part, encoding);
303 }
else if (!defaultEncoding.isEmpty()) {
304 qCDebug(CUTELYST_VIEW_EMAIL) <<
"Using default charset" << defaultEncoding;
305 setupEncoding(part, defaultEncoding);
309void ViewEmailPrivate::setupEncoding(std::shared_ptr<MimePart> part,
312 if (encoding ==
"7bit") {
313 part->setEncoding(MimePart::_7Bit);
314 }
else if (encoding ==
"8bit") {
315 part->setEncoding(MimePart::_8Bit);
316 }
else if (encoding ==
"base64") {
317 part->setEncoding(MimePart::Base64);
318 }
else if (encoding ==
"quoted-printable") {
319 part->setEncoding(MimePart::QuotedPrintable);
321 qCCritical(CUTELYST_VIEW_EMAIL) <<
"Unknown encoding" << encoding;
325#include "moc_viewemail.cpp"
void stash(const QVariantHash &unite)
void appendError(const QString &error)
A view that sends stash data via e-mail.
QString senderUser() const
void setSenderConnectionType(ConnectionType ct)
QByteArray defaultEncoding
QString senderPassword() const
void setSenderUser(const QString &user)
QByteArray defaultContentType
QByteArray defaultCharset
void setSenderHost(const QString &host)
ConnectionType senderConnectionType() const
QString senderHost() const
void setStashKey(const QString &stashKey)
void setDefaultContentType(const QByteArray &contentType)
void setSenderPort(int port)
void setSenderAuthMethod(AuthMethod method)
void setDefaultCharset(const QByteArray &charset)
void setDefaultEncoding(const QByteArray &encoding)
AuthMethod senderAuthMethod() const
ViewEmail(QObject *parent, const QString &name=QString())
void setSenderPassword(const QString &password)
QByteArray render(Context *c) const override
Abstract View component for Cutelyst.
The Cutelyst namespace holds all public Cutelyst API.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
bool isNull() const const
QList< QVariant > toList() const const
QString toString() const const