From cb69299b9d59db0888e942025ae4915c6f32d066 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Wed, 29 Apr 2015 18:58:07 +0200 Subject: added libquickmail and email registration --- src/mail.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/mail.hpp | 29 +++++++++++++++++++++++++++++ src/strusCms.cpp | 8 +++++++- src/strusCms.hpp | 3 ++- src/user.cpp | 10 ++++++++++ src/user_content.hpp | 1 + 6 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/mail.cpp create mode 100644 src/mail.hpp (limited to 'src') diff --git a/src/mail.cpp b/src/mail.cpp new file mode 100644 index 0000000..84e1210 --- /dev/null +++ b/src/mail.cpp @@ -0,0 +1,39 @@ +#include "mail.hpp" + +#include + +mailer::mailer( std::string server, unsigned short port, std::string username, std::string password, std::string from ) + : server( server ), port( port ), username( username ), password( password ), from( from ) +{ + quickmail_initialize( ); + mailobj = quickmail_create( NULL, NULL ); +} + +mailer::~mailer( ) +{ + quickmail_destroy( mailobj ); +} + +void mailer::send( ) +{ + if( !from.empty( ) ) { + quickmail_set_from( mailobj, from.c_str( ) ); + } + + if( !to.empty( ) ) { + quickmail_add_to( mailobj, to.c_str( ) ); + } + + if( !subject.empty( ) ) { + quickmail_set_subject( mailobj, subject.c_str( ) ); + } + + if( !body.empty( ) ) { + quickmail_add_body_memory( mailobj, "plain/text", const_cast( body.c_str( ) ), body.size( ), 0 ); + } + + const char *errmsg; + errmsg = quickmail_send( mailobj, server.c_str( ), port, username.c_str( ), password.c_str( ) ); + std::cerr << "ERROR: " << errmsg << std::endl; +} + diff --git a/src/mail.hpp b/src/mail.hpp new file mode 100644 index 0000000..c9bd8ef --- /dev/null +++ b/src/mail.hpp @@ -0,0 +1,29 @@ +#ifndef MAIL_HPP +#define MAIL_HPP + +#include + +#include "quickmail.h" + +class mailer { + private: + std::string server; + unsigned short port; + std::string username; + std::string password; + std::string from; + quickmail mailobj; + + public: + std::string to; + std::string subject; + std::string body; + + public: + mailer( std::string server, unsigned short port, std::string username, std::string password, std::string from ); + ~mailer( ); + void send( ); +}; + + +#endif diff --git a/src/strusCms.cpp b/src/strusCms.cpp index e5792ec..137a209 100644 --- a/src/strusCms.cpp +++ b/src/strusCms.cpp @@ -10,7 +10,13 @@ strusCms::strusCms( cppcms::service &srv ) : cppcms::application( srv ), intro( *this ), user( *this ), - conn( settings( ).get( "strusCms.db_connection" ) ) + conn( settings( ).get( "strusCms.db_connection" ) ), + mail( settings( ).get( "strusCms.mail.server" ), + settings( ).get( "strusCms.mail.port" ), + settings( ).get( "strusCms.mail.username" ), + settings( ).get( "strusCms.mail.password" ), + settings( ).get( "strusCms.mail.from" ) + ) { locale_name = "en"; script = settings( ).get( "strusCms.script" ); diff --git a/src/strusCms.hpp b/src/strusCms.hpp index 9a93e99..579b2d7 100644 --- a/src/strusCms.hpp +++ b/src/strusCms.hpp @@ -5,6 +5,7 @@ #include "intro.hpp" #include "user.hpp" +#include "mail.hpp" namespace apps { @@ -17,11 +18,11 @@ class strusCms : public cppcms::application { apps::intro intro; apps::user user; std::string conn; + mailer mail; private: std::string script; std::string locale_name; - }; } diff --git a/src/user.cpp b/src/user.cpp index dddde30..3a21504 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -63,9 +63,14 @@ void user::register_user( ) if( request( ).request_method( ) == "POST" ) { c.register_user.load( context( ) ); if( c.register_user.validate( ) ) { + cms.mail.subject = "Registration request"; + cms.mail.body = "Your registration code is: CODE"; + cms.mail.to = c.register_user.email.value( ); + cms.mail.send( ); response( ).set_redirect_header( cms.root( ) + "/confirm_register" ); } } + render( "register_user", c ); } @@ -202,6 +207,8 @@ register_user_form::register_user_form( apps::strusCms &cms ) password.error_message( "Your password is illegal" ); password2.message( "Your password (again)" ); password2.error_message( "Your password doesn't match" ); + email.message( "Email" ); + email.error_message( "Your email address is not valid" ); captcha.message( "Enter the correct captcha" ); captcha.error_message( "Captcha didn't match" ); submit.value( "Register user" ); @@ -209,12 +216,14 @@ register_user_form::register_user_form( apps::strusCms &cms ) add( username ); add( password ); add( password2 ); + add( email ); add( captcha ); add( submit ); username.non_empty( ); password.non_empty( ); password2.non_empty( ); + email.non_empty( ); captcha.non_empty( ); } @@ -243,6 +252,7 @@ bool register_user_form::validate( ) if( captcha.value( ).compare( cms.user.last_captcha ) != 0 ) { captcha.valid( false ); captcha.clear( ); + booster::ptime::sleep( booster::ptime( 5, 0 ) ); return false; } diff --git a/src/user_content.hpp b/src/user_content.hpp index e86e2fd..756f314 100644 --- a/src/user_content.hpp +++ b/src/user_content.hpp @@ -24,6 +24,7 @@ struct register_user_form : public cppcms::form { cppcms::widgets::text username; cppcms::widgets::password password; cppcms::widgets::password password2; + cppcms::widgets::text email; cppcms::widgets::text captcha; cppcms::widgets::submit submit; -- cgit v1.2.3-54-g00ecf