From b1c035600b6255b03a4b6930ce61698274ba9de2 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Thu, 30 Apr 2015 09:46:02 +0200 Subject: email sending fixes --- src/mail.cpp | 18 ++++++++++++++++-- src/mail.hpp | 4 ++++ src/user.cpp | 11 +++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/mail.cpp b/src/mail.cpp index 84e1210..0c868d2 100644 --- a/src/mail.cpp +++ b/src/mail.cpp @@ -29,11 +29,25 @@ void mailer::send( ) } if( !body.empty( ) ) { - quickmail_add_body_memory( mailobj, "plain/text", const_cast( body.c_str( ) ), body.size( ), 0 ); + quickmail_add_body_memory( mailobj, "text/plain", const_cast( body.c_str( ) ), body.size( ), 0 ); } + _hasError = false; const char *errmsg; errmsg = quickmail_send( mailobj, server.c_str( ), port, username.c_str( ), password.c_str( ) ); - std::cerr << "ERROR: " << errmsg << std::endl; + if( errmsg != NULL ) { + _hasError = true; + lastErrorMsg = std::string( errmsg ); + } +} + +bool mailer::hasError( ) +{ + return _hasError; +} + +std::string mailer::getLastError( ) +{ + return lastErrorMsg; } diff --git a/src/mail.hpp b/src/mail.hpp index c9bd8ef..3445d76 100644 --- a/src/mail.hpp +++ b/src/mail.hpp @@ -13,6 +13,8 @@ class mailer { std::string password; std::string from; quickmail mailobj; + bool _hasError; + std::string lastErrorMsg; public: std::string to; @@ -23,6 +25,8 @@ class mailer { mailer( std::string server, unsigned short port, std::string username, std::string password, std::string from ); ~mailer( ); void send( ); + bool hasError( ); + std::string getLastError( ); }; diff --git a/src/user.cpp b/src/user.cpp index 3a21504..d98d0f3 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -64,10 +64,17 @@ void user::register_user( ) 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.body = "Your registration code is: CODE\n"; cms.mail.to = c.register_user.email.value( ); cms.mail.send( ); - response( ).set_redirect_header( cms.root( ) + "/confirm_register" ); + if( cms.mail.hasError( ) ) { + c.register_user.email.valid( false ); + c.register_user.email.error_message( "Can't send email to this address" ); + booster::ptime::sleep( booster::ptime( 5, 0 ) ); + std::cerr << "SEND MAIL ERROR: " << cms.mail.getLastError( ) << std::endl; + } else { + response( ).set_redirect_header( cms.root( ) + "/confirm_register" ); + } } } -- cgit v1.2.3-54-g00ecf