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; }
/* * Builds the message and tries to send it * via the specified SMTP server. Returns 0 * if everything went ok, -1 if someting goes * south and sets `global_smtp_error_msg'. * If AUTH is required, a dialog will pop up * asking the user to authenticate. */ int send_pr(PROBLEM_REPORT *mypr) { smtp_session_t session; smtp_message_t message; smtp_recipient_t recipient; const smtp_status_t *status; auth_context_t authctx; int noauth = 0; char *host = NULL; int i = 0; enum notify_flags notify = Notify_SUCCESS|Notify_FAILURE; FILE *fp; char tempfile[FILENAME_MAX + 1]; char *tmpdir; int tempfd; char my_smtp_server[1024]; char my_recipient[1024]; char buf[128]; if (gsp_auth_done != TRUE) { my_auth = malloc(sizeof(GSP_AUTH)); my_auth->username = malloc(1024); memset(my_auth->username, 0, 1024); my_auth->password = malloc(1024); memset(my_auth->password, 0, 1024); } tmpdir = getenv("TMPDIR"); memset(tempfile, 0, sizeof(tempfile)); if (tmpdir != NULL) { snprintf(tempfile, FILENAME_MAX, "%s/gtk-send-pr.XXXXXXXX", tmpdir); } else { snprintf(tempfile, FILENAME_MAX , "/tmp/gtk-send-pr.XXXXXXXX"); } tempfd = mkstemp(tempfile); fp = fdopen(tempfd, "w"); build_message(fp, mypr); fclose(fp); auth_client_init(); session = smtp_create_session(); message = smtp_add_message(session); switch (mypr->ssl_mode) { case GSP_SSL_NO : i = smtp_starttls_enable(session, Starttls_DISABLED); break; case GSP_SSL_EN : i = smtp_starttls_enable(session, Starttls_ENABLED); break; case GSP_SSL_RE : i = smtp_starttls_enable(session, Starttls_REQUIRED); break; } snprintf(my_smtp_server, 1024, "%s:%s", mypr->smtp_server, mypr->smtp_port); host = my_smtp_server; smtp_set_server(session, host); authctx = auth_create_context(); auth_set_mechanism_flags(authctx, AUTH_PLUGIN_PLAIN, 0); auth_set_interact_cb(authctx, authinteract, NULL); smtp_set_eventcb(session, event_cb, NULL); if (!noauth) { smtp_auth_set_context(session, authctx); } smtp_set_header(message, "From", mypr->originator, mypr->smtp_from); smtp_set_header(message, "To", mypr->smtp_rcpt, mypr->smtp_to); smtp_set_header(message, "Subject", mypr->smtp_subject); smtp_set_header(message, "Message-Id", NULL); smtp_set_reverse_path(message, mypr->smtp_from); fp = fopen(tempfile, "r"); smtp_set_message_fp(message, fp); /* Recipient must be in RFC2821 format */ snprintf(my_recipient, 1024, "%s", mypr->smtp_to); recipient = smtp_add_recipient(message, my_recipient); smtp_dsn_set_notify(recipient, notify); if (mypr->smtp_cc_num > 0) { for (i = 0; i < mypr->smtp_cc_num; i++) { smtp_set_header(message, "CC", NULL, mypr->smtp_cc[i]); snprintf(my_recipient, 1024, "%s", mypr->smtp_cc[i]); recipient = smtp_add_recipient(message, my_recipient); smtp_dsn_set_notify(recipient, notify); } } if (!smtp_start_session(session)) { snprintf(global_smtp_error_msg, 1024, "SMTP server problem : %s\n", smtp_strerror(smtp_errno(), buf, sizeof buf)); status = smtp_message_transfer_status(message); smtp_destroy_session(session); auth_destroy_context(authctx); fclose(fp); unlink(tempfile); auth_client_exit(); return(-1); } else { status = smtp_message_transfer_status(message); if (status != NULL) { /* Something went wrong... */ if (status->code >= 400) { snprintf(global_smtp_error_msg, 1024, "SMTP server problem : %i %s\n", status->code, status->text); smtp_destroy_session(session); auth_destroy_context(authctx); fclose(fp); unlink(tempfile); auth_client_exit(); return(-1); } } smtp_destroy_session(session); auth_destroy_context(authctx); fclose(fp); unlink(tempfile); auth_client_exit(); return(0); } }