Esempio n. 1
0
void print_status()
{
    smtp_status_t const* status = smtp_message_transfer_status(message);
    printf ("%d %s", status->code,
            (status->text != NULL) ? status->text : "\n");
    smtp_enumerate_recipients (message, print_recipient_status, NULL);
}
Esempio n. 2
0
static int notify_email_notification (const notification_t *n)
{
  smtp_recipient_t recipient;

  struct tm timestamp_tm;
  char timestamp_str[64];

  char severity[32];
  char subject[MAXSTRING];

  char buf[4096] = "";
  int  buf_len = sizeof (buf);
  int i;

  ssnprintf (severity, sizeof (severity), "%s",
      (n->severity == NOTIF_FAILURE) ? "FAILURE"
      : ((n->severity == NOTIF_WARNING) ? "WARNING"
        : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));

  ssnprintf (subject, sizeof (subject),
      (email_subject == NULL) ? DEFAULT_SMTP_SUBJECT : email_subject,
      severity, n->host);

  localtime_r (&n->time, &timestamp_tm);
  strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
      &timestamp_tm);
  timestamp_str[sizeof (timestamp_str) - 1] = '\0';

  /* Let's make RFC822 message text with \r\n EOLs */
  ssnprintf (buf, buf_len,
      "MIME-Version: 1.0\r\n"
      "Content-Type: text/plain;\r\n"
      "Content-Transfer-Encoding: 8bit\r\n"
      "Subject: %s\r\n"
      "\r\n"
      "%s - %s@%s\r\n"
      "\r\n"
      "Message: %s",
      subject,
      timestamp_str,
      severity,
      n->host,
      n->message);

  if (!(message = smtp_add_message (session))) {
    ERROR ("notify_email plugin: cannot set SMTP message");
    return (-1);   
  }
  smtp_set_reverse_path (message, email_from);
  smtp_set_header (message, "To", NULL, NULL);
  smtp_set_message_str (message, buf);

  for (i = 0; i < recipients_len; i++)
    recipient = smtp_add_recipient (message, recipients[i]);

  /* Initiate a connection to the SMTP server and transfer the message. */
  if (!smtp_start_session (session)) {
    char buf[MAXSTRING];
    ERROR ("notify_email plugin: SMTP server problem: %s",
        smtp_strerror (smtp_errno (), buf, sizeof buf));
    return (-1);
  } else {
    const smtp_status_t *status;
    /* Report on the success or otherwise of the mail transfer. */
    status = smtp_message_transfer_status (message);
    DEBUG ("notify_email plugin: SMTP server report: %d %s",
        status->code, (status->text != NULL) ? status->text : "\n");
    smtp_enumerate_recipients (message, print_recipient_status, NULL);
  }

  return (0);
} /* int notify_email_notification */
Esempio n. 3
0
bool send(const std::string& from, const std::string& to, 
	std::vector<std::string>& recipients, 
	const std::string& subject, const std::string& data)
{
	BOOSTER_LOG(debug,__FUNCTION__);
	message_ = smtp_add_message(session_);
	/* Request MDN sent to the same address as the reverse path */
	//smtp_set_header(message, "Disposition-Notification-To", NULL, NULL);

	/* Set the reverse path for the mail envelope.  (NULL is ok) */
	smtp_set_reverse_path(message_, from.c_str());

#if 0
	/* The message-id is OPTIONAL but SHOULD be present.  By default
	libESMTP supplies one.  If this is not desirable, the following
	prevents one making its way to the server.
	N.B. It is not possible to prohibit REQUIRED headers.  Furthermore,
	the submission server will probably add a Message-ID header,
	so this cannot prevent the delivered message from containing
	the message-id.  */
	smtp_set_header_option(message_, "Message-Id", Hdr_PROHIBIT, 1);
#endif

	/* RFC 2822 doesn't require recipient headers but a To: header would
	be nice to have if not present. */
	if(!to.empty())
		smtp_set_header(message_, "To", NULL, NULL);
	else
		smtp_set_header(message_, "To", NULL, to.c_str());

	/* Set the Subject: header.  For no reason, we want the supplied subject
	to override any subject line in the message headers. */
	if (!subject.empty())
	{
		smtp_set_header(message_, "Subject", subject.c_str());
		smtp_set_header_option(message_, "Subject", Hdr_OVERRIDE, 1);
	}

	std::string msg = std::string("MIME-Version: 1.0\r\n") +
             "Content-Type: text/plain;\r\n" +
             "Content-Transfer-Encoding: 8bit\r\n" +
             "Subject: " + subject + "\r\n" +
             "\r\n" + 
             "Message: " + data;

	char s[ESMTP_MAX_STR];
	strcpy(s, msg.c_str());
	smtp_set_message_str(message_, s);
	/* Add remaining program arguments as message recipients. */
	std::vector<std::string>::const_iterator it;
	for(it = recipients.begin(); it != recipients.end(); ++it )
	{
		recipient_ = smtp_add_recipient(message_, it->c_str());

		/* Recipient options set here */
		if (notify_ != Notify_NOTSET)
			smtp_dsn_set_notify(recipient_, notify_);
	}
	/* Initiate a connection to the SMTP server and transfer the message. */
	if (!smtp_start_session(session_))
	{
		char buf[128];
		BOOSTER_LOG(error,__FUNCTION__) << "SMTP server problem: " << smtp_strerror(smtp_errno(), buf, sizeof buf);
		return false;
	}
	
	/* Report on the success or otherwise of the mail transfer. */
	status_ = smtp_message_transfer_status(message_);
	BOOSTER_LOG(debug,__FUNCTION__) << status_->code << ((status_->text != NULL) ? status_->text : "\n") << std::endl;
	smtp_enumerate_recipients (message_, print_recipient_status, NULL);
	return true;
}