42 logger.setAppname(
"Contact form app");
55 auto randVal = connection.
session()[
"randVal"];
64 resp <<
"<!DOCTYPE html><head><title>Contact Form</title></head><body>";
67 if (randVal.has_value() && post.count(
"rand_val") == 1) {
70 bool randValid =
false;
72 if (any_cast<unsigned int>(randVal) == stoul(post[
"rand_val"])) {
78 resp <<
"<p>No spamming, please!</p>";
83 if (post[
"name"].empty() || post[
"email"].empty() || post[
"subject"].empty() || post[
"message"].empty()) {
84 resp <<
"<p>Please go back and fill in all required fields!</p></body></html>";
90 EmailAddress from(post[
"name"],
"contactform@example.com");
95 email.
headers()[
"Content-Type"] =
"text/plain; charset=UTF-8";
99 email.
text() =
"This contact form was sent via an example nawa application!\r\n\r\n"
100 "Name of the sender: " +
101 post[
"name"] +
"\r\n"
102 "Email of the sender: " +
103 post[
"email"] +
"\r\n\r\n" +
107 mail::SmtpMailer smtp(
"example.com", 587, mail::SmtpMailer::TlsMode::REQUIRE_STARTTLS,
108 true,
"test@example.com",
"12345");
109 smtp.enqueue(std::make_shared<SimpleEmail>(email), to, std::make_shared<EmailAddress>(from));
112 resp <<
"<p>Message sent successfully!</p>";
114 resp <<
"<p>Message could not be sent due to a technical problem :(</p>";
119 resp <<
"</body></html>";
130 resp <<
"<p>Please fill in the following form in order to contact us! All fields are required.</p>\r\n"
131 "<form name=\"contact\" method=\"post\" action=\"?\">"
132 "<input type=\"hidden\" name=\"rand_val\" value=\""
133 << any_cast<unsigned int>(randVal)
135 "<p>Your name: <input type=\"text\" name=\"name\" /></p>"
136 "<p>Email address: <input type=\"email\" name=\"email\" /></p>"
137 "<p>Subject: <input type=\"text\" name=\"subject\" /></p>"
138 "<p>Message: <textarea name=\"message\" rows=\"5\" cols=\"30\"></textarea></p>"
139 "<p><input type=\"submit\" name=\"go\" value=\"Submit\" /></p>"
140 "</form></body></html>";
Exception class that can be used by apps to catch errors resulting from nawa function calls.
Simple class for (not (yet) thread-safe) logging to stderr or to any other output stream.
#define NLOG_DEBUG(Logger, Message)
#define NLOG_ERROR(Logger, Message)
Structure representing a basic email.
For establishing a connection to an SMTP server and sending emails.
This file will be configured by CMake and contains the necessary properties to ensure that a loaded a...
nawa::Session & session() noexcept
void setCookiePolicy(Cookie policy)
std::ostream & responseStream() noexcept
nawa::Request const & request() const noexcept
virtual std::string getMessage() const noexcept
virtual std::string getDebugMessage() const noexcept
request::Post const & post() const noexcept
void set(std::string key, const std::any &value)
std::string start(std::string sessionId, std::optional< unsigned long > keepalive=std::nullopt)
std::string get(bool includeName=true, bool applyPunycode=true) const
HeadersMap & headers() noexcept
bool & quotedPrintableEncode() noexcept
std::string & text() noexcept
Namespace containing functions for text encoding and decoding.
std::string makeEncodedWord(std::string const &input, bool base64=false, bool onlyIfNecessary=true)