00001 00034 #include "emailsender.h" 00035 00036 #include <iostream> 00037 #include <stdexcept> 00038 #include <string> 00039 00040 #include "alarmconfiguration.h" 00041 #include "mimemessage.h" 00042 #include "mimetext.h" 00043 #include "smtpclient.h" 00044 00045 using namespace AlarmNotifications; 00046 00047 EMailSender& EMailSender::instance() noexcept 00048 { 00049 static EMailSender global_instance; 00050 return global_instance; 00051 } 00052 00053 void EMailSender::sendAlarmNotification ( const std::vector< AlarmStatusEntry > alarms ) noexcept 00054 { 00055 try { 00056 instance().sendAlarmNotification_internal ( std::move ( alarms ) ); 00057 } 00058 catch ( std::exception& e ) 00059 { 00060 std::cerr << "Exception in e-mail sending procedure: " << e.what() << std::endl; 00061 } 00062 catch ( ... ) 00063 { 00064 std::cerr << "Unknown error in e-mail sending procedure: " << std::endl; 00065 } 00066 } 00067 00068 EMailSender::EMailSender() 00069 { 00070 00071 } 00072 00073 EMailSender::~EMailSender() 00074 { 00075 00076 } 00077 00078 void EMailSender::sendAlarmNotification_internal ( const std::vector< AlarmStatusEntry > alarms ) 00079 { 00080 SmtpClient smtp ( 00081 QString::fromUtf8 ( AlarmConfiguration::instance().getEMailNotificationServerName().c_str() ), 00082 AlarmConfiguration::instance().getEMailNotificationServerPort(), 00083 SmtpClient::TcpConnection 00084 ); 00085 EmailAddress sender ( 00086 QString::fromUtf8 ( AlarmConfiguration::instance().getEMailNotificationFrom().c_str() ), 00087 QString::fromUtf8 ( "Alarm Notification Daemon" ) 00088 ); 00089 EmailAddress recipient ( 00090 QString::fromUtf8 ( AlarmConfiguration::instance().getEMailNotificationTo().c_str() ), 00091 QString::fromUtf8 ( "Alarm Notification Mailing List" ) 00092 ); 00093 MimeMessage email; 00094 email.setSender ( &sender ); 00095 email.addTo ( &recipient ); 00096 email.setSubject ( "Detector Control System Alarm" ); 00097 MimeText text; 00098 text.setEncoding(MimePart::QuotedPrintable); 00099 text.setCharset(QString::fromUtf8("utf8")); 00100 text.setText ( composeMessageText ( alarms ) ); 00101 email.addPart ( &text ); 00102 00103 std::cout << "Sending alarm notification by e-mail!" << std::endl; 00104 const bool connection = smtp.connectToHost(); 00105 if ( !connection ) 00106 { 00107 QString errormessage = QString::fromUtf8 ( "An error occured while connecting to the SMTP server.\n" ); 00108 errormessage += "Server response: " + smtp.getResponseCode() + QString::fromUtf8 ( " " ) + smtp.getResponseText(); 00109 std::string stderrormessage ( errormessage.toUtf8().data() ); 00110 throw std::runtime_error ( stderrormessage ); 00111 } 00112 const bool sending = smtp.sendMail(email); 00113 if ( !sending ) 00114 { 00115 QString errormessage = QString::fromUtf8 ( "An error occured while sending the e-mail.\n" ); 00116 errormessage += "Server response: " + smtp.getResponseCode() + QString::fromUtf8 ( " " ) + smtp.getResponseText(); 00117 std::string stderrormessage ( errormessage.toUtf8().data() ); 00118 throw std::runtime_error ( stderrormessage ); 00119 } 00120 smtp.quit(); 00121 } 00122 00123 QString EMailSender::composeMessageText ( const std::vector< AlarmStatusEntry >& alarms ) 00124 { 00125 QString text; 00126 text += QString::fromUtf8 ( "Hello,\n\nthe following PV(s) triggered an alarm:\n\n" ); 00127 for ( auto i = alarms.begin(); i != alarms.end(); i++ ) 00128 { 00129 text+= QString::fromUtf8 ( ( *i ).getPVName().c_str() ) + QString::fromUtf8 ( "\n" ); 00130 } 00131 text += QString::fromUtf8 ( "\nPlease remember to acknowledge the alarms if you go solving the problem.\n\n\nYour Alarm Notification Service\n" ); 00132 return text; 00133 }