AlarmNotifications
PANDA Slow Control Alarm Daemon
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
emailsender.cpp
Go to the documentation of this file.
1 
34 #include "emailsender.h"
35 
36 #include <iostream>
37 #include <stdexcept>
38 #include <string>
39 
40 #include "alarmconfiguration.h"
41 #include "mimemessage.h"
42 #include "mimetext.h"
43 #include "smtpclient.h"
44 
45 using namespace AlarmNotifications;
46 
48 {
49  static EMailSender global_instance;
50  return global_instance;
51 }
52 
53 void EMailSender::sendAlarmNotification ( const std::vector< AlarmStatusEntry > alarms ) noexcept
54 {
55  try {
56  instance().sendAlarmNotification_internal ( std::move ( alarms ) );
57  }
58  catch ( std::exception& e )
59  {
60  std::cerr << "Exception in e-mail sending procedure: " << e.what() << std::endl;
61  }
62  catch ( ... )
63  {
64  std::cerr << "Unknown error in e-mail sending procedure: " << std::endl;
65  }
66 }
67 
69 {
70 
71 }
72 
74 {
75 
76 }
77 
78 void EMailSender::sendAlarmNotification_internal ( const std::vector< AlarmStatusEntry > alarms )
79 {
80  SmtpClient smtp (
81  QString::fromUtf8 ( AlarmConfiguration::instance().getEMailNotificationServerName().c_str() ),
82  AlarmConfiguration::instance().getEMailNotificationServerPort(),
83  SmtpClient::TcpConnection
84  );
85  EmailAddress sender (
86  QString::fromUtf8 ( AlarmConfiguration::instance().getEMailNotificationFrom().c_str() ),
87  QString::fromUtf8 ( "Alarm Notification Daemon" )
88  );
89  EmailAddress recipient (
90  QString::fromUtf8 ( AlarmConfiguration::instance().getEMailNotificationTo().c_str() ),
91  QString::fromUtf8 ( "Alarm Notification Mailing List" )
92  );
93  MimeMessage email;
94  email.setSender ( &sender );
95  email.addTo ( &recipient );
96  email.setSubject ( "Detector Control System Alarm" );
97  MimeText text;
98  text.setEncoding(MimePart::QuotedPrintable);
99  text.setCharset(QString::fromUtf8("utf8"));
100  text.setText ( composeMessageText ( alarms ) );
101  email.addPart ( &text );
102 
103  std::cout << "Sending alarm notification by e-mail!" << std::endl;
104  const bool connection = smtp.connectToHost();
105  if ( !connection )
106  {
107  QString errormessage = QString::fromUtf8 ( "An error occured while connecting to the SMTP server.\n" );
108  errormessage += "Server response: " + smtp.getResponseCode() + QString::fromUtf8 ( " " ) + smtp.getResponseText();
109  std::string stderrormessage ( errormessage.toUtf8().data() );
110  throw std::runtime_error ( stderrormessage );
111  }
112  const bool sending = smtp.sendMail(email);
113  if ( !sending )
114  {
115  QString errormessage = QString::fromUtf8 ( "An error occured while sending the e-mail.\n" );
116  errormessage += "Server response: " + smtp.getResponseCode() + QString::fromUtf8 ( " " ) + smtp.getResponseText();
117  std::string stderrormessage ( errormessage.toUtf8().data() );
118  throw std::runtime_error ( stderrormessage );
119  }
120  smtp.quit();
121 }
122 
123 QString EMailSender::composeMessageText ( const std::vector< AlarmStatusEntry >& alarms )
124 {
125  QString text;
126  text += QString::fromUtf8 ( "Hello,\n\nthe following PV(s) triggered an alarm:\n\n" );
127  for ( auto i = alarms.begin(); i != alarms.end(); i++ )
128  {
129  text+= QString::fromUtf8 ( ( *i ).getPVName().c_str() ) + QString::fromUtf8 ( "\n" );
130  }
131  text += QString::fromUtf8 ( "\nPlease remember to acknowledge the alarms if you go solving the problem.\n\n\nYour Alarm Notification Service\n" );
132  return text;
133 }
static EMailSender & instance() noexcept
Get singleton instance.
Definition: emailsender.cpp:47
Send alarm notifications via e-mail.
QString composeMessageText(const std::vector< AlarmStatusEntry > &alarms)
Compose message text.
Namespace for Alarm Notifications application.
static void sendAlarmNotification(const std::vector< AlarmStatusEntry > alarms) noexcept
Send an alarm notification via e-mail.
Definition: emailsender.cpp:53
#define noexcept
Allow using the noexcept keyword with GCC < 4.6.
Definition: oldgcccompat.h:52
Send alarm notifications via e-mail.
Definition: emailsender.h:57
Singleton to read and change the configuration of this application.
void sendAlarmNotification_internal(const std::vector< AlarmStatusEntry > alarms)
Compose and send the e-mail notification.
Definition: emailsender.cpp:78
static AlarmConfiguration & instance() noexcept
Get singleton instance.