void alloc() { if (session) smtp_destroy_session(session); // create. session = smtp_create_session(); message = smtp_add_message(session); // set server. smtp_set_server(session, d_owner->server.c_str()); // set auth. if (authctx) auth_destroy_context(authctx); authctx = auth_create_context(); // set auth. auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0); auth_set_interact_cb (authctx, authinteract, d_owner); // set passwd. smtp_starttls_set_password_cb(tlsinteract, d_owner); smtp_set_eventcb(session, event_cb, d_owner); smtp_auth_set_context(session, authctx); smtp_set_messagecb(message, readlinefp_cb, d_owner); }
static int notify_email_init (void) { char server[MAXSTRING]; auth_client_init(); if (!(session = smtp_create_session ())) { ERROR ("notify_email plugin: cannot create SMTP session"); return (-1); } smtp_set_monitorcb (session, monitor_cb, NULL, 1); smtp_set_hostname (session, hostname_g); ssnprintf(server, sizeof (server), "%s:%i", (smtp_host == NULL) ? DEFAULT_SMTP_HOST : smtp_host, smtp_port); smtp_set_server (session, server); if (smtp_user && smtp_password) { authctx = auth_create_context (); auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0); auth_set_interact_cb (authctx, authinteract, NULL); } if ( !smtp_auth_set_context (session, authctx)) { ERROR ("notify_email plugin: cannot set SMTP auth context"); return (-1); } return (0); } /* int notify_email_init */
void EmailNotify::notify(Withdrawal *withdrawal) { User *user; smtp_session_t smtp_session; smtp_message_t smtp_message; QString subject, txtState, To, ToBoss; QDate start, end; if (!configOk) return; user = withdrawal->getUser(); switch (withdrawal->getState()) { case WDRL_REQUEST: txtState = tr("Request"); break; case WDRL_APPROVED: txtState = tr("Approved"); break; case WDRL_REJECTED: txtState = tr("Rejected"); break; case WDRL_CANCEL_REQUEST: txtState = tr("Cancellation Request"); break; case WDRL_CANCELLED: txtState = tr("Cancelled"); break; } start = QDate::fromJulianDay(withdrawal->getStart()); end = QDate::fromJulianDay(withdrawal->getEnd()); subject = tr("[Vacation - ") + user->getFirstName() + " " + user->getLastName() + "] (" + txtState + ") " + QDate::shortMonthName(start.month()) + ' ' + QString::number(start.day()) + " - " + QDate::shortMonthName(end.month()) + ' ' + QString::number(end.day()); To = user->getEmail(); To.replace('#', '.'); ToBoss = user->getBoss()->getEmail(); ToBoss.replace('#', '.'); smtp_session = smtp_create_session(); smtp_message = smtp_add_message(smtp_session); smtp_set_header(smtp_message, "To", NULL, (const char *)To.toUtf8()); smtp_set_server(smtp_session, server.toUtf8()); smtp_set_reverse_path(smtp_message, reply.toUtf8()); smtp_set_header(smtp_message, "Subject", (const char *)subject.toUtf8()); smtp_set_header_option(smtp_message, "Subject", Hdr_OVERRIDE, 1); smtp_set_message_str(smtp_message, (void*)"Vacation Update"); smtp_add_recipient(smtp_message, To.toUtf8()); if (ToBoss.size()) smtp_add_recipient(smtp_message, ToBoss.toUtf8()); smtp_start_session(smtp_session); // send messages smtp_destroy_session(smtp_session); }
/** * Send a message to the specified remote node. * * @param tsession the GNUNET_MessageHello identifying the remote node * @param msg what to send * @param size the size of the message * @param important is this message important enough to override typical limits? * @return GNUNET_SYSERR on error, GNUNET_OK on success */ static int api_send (GNUNET_TSession * tsession, const void *msg, const unsigned int size, int important) { const GNUNET_MessageHello *hello; const EmailAddress *haddr; char *m; char *filter; char *fvalue; SMTPMessage *mp; struct GetMessageClosure gm_cls; smtp_session_t session; smtp_message_t message; smtp_recipient_t recipient; #define EBUF_LEN 128 char ebuf[EBUF_LEN]; GNUNET_CronTime now; if (smtp_shutdown == GNUNET_YES) return GNUNET_SYSERR; if ((size == 0) || (size > smtpAPI.mtu)) { GNUNET_GE_BREAK (ectx, 0); return GNUNET_SYSERR; } now = GNUNET_get_time (); if ((important != GNUNET_YES) && ((now - last_transmission) * rate_limit) < GNUNET_CRON_HOURS) return GNUNET_NO; /* rate too high */ last_transmission = now; hello = (const GNUNET_MessageHello *) tsession->internal; if (hello == NULL) return GNUNET_SYSERR; GNUNET_mutex_lock (lock); session = smtp_create_session (); if (session == NULL) { GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_IMMEDIATE, _("SMTP: `%s' failed: %s.\n"), "smtp_create_session", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } if (0 == smtp_set_server (session, smtp_server_name)) { GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_IMMEDIATE, _("SMTP: `%s' failed: %s.\n"), "smtp_set_server", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); smtp_destroy_session (session); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } haddr = (const EmailAddress *) &hello[1]; message = smtp_add_message (session); if (message == NULL) { GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"), "smtp_add_message", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); smtp_destroy_session (session); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } smtp_set_header (message, "To", NULL, haddr->senderAddress); smtp_set_header (message, "From", NULL, email); filter = GNUNET_strdup (haddr->filter); fvalue = strstr (filter, ": "); GNUNET_GE_ASSERT (NULL, NULL != fvalue); fvalue[0] = '\0'; fvalue += 2; if (0 == smtp_set_header (message, filter, fvalue)) { GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"), "smtp_set_header", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); smtp_destroy_session (session); GNUNET_mutex_unlock (lock); GNUNET_free (filter); return GNUNET_SYSERR; } GNUNET_free (filter); m = GNUNET_malloc (size + sizeof (SMTPMessage)); GNUNET_memcpy (m, msg, size); mp = (SMTPMessage *) &m[size]; mp->header.size = htons (size + sizeof (SMTPMessage)); mp->header.type = htons (0); mp->sender = *core_api->my_identity; gm_cls.ebody = NULL; gm_cls.pos = 0; gm_cls.esize = GNUNET_STRINGS_base64_encode (m, size + sizeof (SMTPMessage), &gm_cls.ebody); GNUNET_free (m); if (0 == smtp_size_set_estimate (message, gm_cls.esize)) { GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"), "smtp_size_set_estimate", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); } if (0 == smtp_set_messagecb (message, &get_message, &gm_cls)) { GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"), "smtp_set_messagecb", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); smtp_destroy_session (session); GNUNET_mutex_unlock (lock); GNUNET_free (gm_cls.ebody); return GNUNET_SYSERR; } recipient = smtp_add_recipient (message, haddr->senderAddress); if (recipient == NULL) { GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"), "smtp_add_recipient", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); smtp_destroy_session (session); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } if (0 == smtp_start_session (session)) { GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"), "smtp_start_session", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); smtp_destroy_session (session); GNUNET_mutex_unlock (lock); GNUNET_free (gm_cls.ebody); return GNUNET_SYSERR; } if (stats != NULL) stats->change (stat_bytesSent, size); if (core_api->load_monitor != NULL) GNUNET_network_monitor_notify_transmission (core_api->load_monitor, GNUNET_ND_UPLOAD, gm_cls.esize); smtp_message_reset_status (message); /* this is needed to plug a 28-byte/message memory leak in libesmtp */ smtp_destroy_session (session); GNUNET_mutex_unlock (lock); GNUNET_free (gm_cls.ebody); return GNUNET_OK; }
int main() { smtp_session_t session; smtp_message_t message; struct sigaction sa; const smtp_status_t *status; char buf[128]; FILE *fp; /* This program sends only one message at a time. Create an SMTP * session and add a message to it. */ if( (session = smtp_create_session ()) == NULL){ fprintf (stderr, "smtp_create_session problem %s\n", smtp_strerror (smtp_errno (), buf, sizeof buf)); return 1; } if((message = smtp_add_message (session)) == NULL){ fprintf (stderr, "smtp_add_message problem %s\n", smtp_strerror (smtp_errno (), buf, sizeof buf)); return 1; } /* NB. libESMTP sets timeouts as it progresses through the protocol. * In addition the remote server might close its socket on a timeout. * Consequently libESMTP may sometimes try to write to a socket with * no reader. Ignore SIGPIPE, then the program doesn't get killed * if/when this happens. */ sa.sa_handler = SIG_IGN; sigemptyset (&sa.sa_mask); sa.sa_flags = 0; sigaction (SIGPIPE, &sa, NULL); /* Set the host running the SMTP server. LibESMTP has a default port * number of 587, however this is not widely deployed so the port * is specified as 25 along with the default MTA host. */ smtp_set_server (session, "smtp.qq.com:25"); /* Set the reverse path for the mail envelope. (NULL is ok) */ smtp_set_reverse_path (message, "*****@*****.**"); /* RFC 2822 doesn't require recipient headers but a To: header would * be nice to have if not present. */ smtp_set_header (message, "To", NULL, NULL); smtp_set_header (message, "Subject", " test mail"); smtp_set_header_option (message, "Subject", Hdr_OVERRIDE, 1); fprintf(stderr,"%s\n","smtp_set_server."); if ((fp = fopen ("./test-mail.eml", "r")) == NULL) { fprintf (stderr, "can't open mail file: %s\n", strerror (errno)); return (1); } smtp_set_message_fp (message, fp); smtp_add_recipient (message,"test@localhost"); /* Initiate a connection to the SMTP server and transfer the * message. */ if (!smtp_start_session (session)){ fprintf (stderr, "SMTP server problem %s\n", smtp_strerror (smtp_errno (), buf, sizeof buf)); } else{ /* Report on the success or otherwise of the mail transfer. */ status = smtp_message_transfer_status (message); printf ("%d %s", status->code, (status->text != NULL) ? status->text : "\n"); } /* Free resources consumed by the program. */ smtp_destroy_session (session); if(fp != NULL){ fclose(fp); } return 0; }
/* * 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); } }
/* * Back-end function to actually send the email */ static int send_email(char *msg) { int i, err = PMIX_SUCCESS; char *str = NULL; char *errmsg = NULL; struct sigaction sig, oldsig; bool set_oldsig = false; smtp_session_t session = NULL; smtp_message_t message = NULL; message_status_t ms; pmix_plog_smtp_component_t *c = &mca_plog_smtp_component; if (NULL == c->to_argv) { c->to_argv = pmix_argv_split(c->to, ','); if (NULL == c->to_argv || NULL == c->to_argv[0]) { return PMIX_ERR_OUT_OF_RESOURCE; } } ms.sent_flag = SENT_NONE; ms.prev_string = NULL; ms.msg = msg; /* Temporarily disable SIGPIPE so that if remote servers timeout or hang up on us, it doesn't kill this application. We'll restore the original SIGPIPE handler when we're done. */ sig.sa_handler = SIG_IGN; sigemptyset(&sig.sa_mask); sig.sa_flags = 0; sigaction(SIGPIPE, &sig, &oldsig); set_oldsig = true; /* Try to get a libesmtp session. If so, assume that libesmtp is happy and proceeed */ session = smtp_create_session(); if (NULL == session) { err = PMIX_ERR_NOT_SUPPORTED; errmsg = "stmp_create_session"; goto error; } /* Create the message */ message = smtp_add_message(session); if (NULL == message) { err = PMIX_ERROR; errmsg = "stmp_add_message"; goto error; } /* Set the SMTP server (yes, it's a weird return status!) */ asprintf(&str, "%s:%d", c->server, c->port); if (0 == smtp_set_server(session, str)) { err = PMIX_ERROR; errmsg = "stmp_set_server"; goto error; } free(str); str = NULL; /* Add the sender */ if (0 == smtp_set_reverse_path(message, c->from_addr)) { err = PMIX_ERROR; errmsg = "stmp_set_reverse_path"; goto error; } /* Set the subject and some headers */ asprintf(&str, "PMIx SMTP Plog v%d.%d.%d", c->super.base.pmix_mca_component_major_version, c->super.base.pmix_mca_component_minor_version, c->super.base.pmix_mca_component_release_version); if (0 == smtp_set_header(message, "Subject", c->subject) || 0 == smtp_set_header_option(message, "Subject", Hdr_OVERRIDE, 1) || 0 == smtp_set_header(message, "To", NULL, NULL) || 0 == smtp_set_header(message, "From", (NULL != c->from_name ? c->from_name : c->from_addr), c->from_addr) || 0 == smtp_set_header(message, "X-Mailer", str) || 0 == smtp_set_header_option(message, "Subject", Hdr_OVERRIDE, 1)) { err = PMIX_ERROR; errmsg = "smtp_set_header"; goto error; } free(str); str = NULL; /* Add the recipients */ for (i = 0; NULL != c->to_argv[i]; ++i) { if (NULL == smtp_add_recipient(message, c->to_argv[i])) { err = PMIX_ERR_OUT_OF_RESOURCE; errmsg = "stmp_add_recipient"; goto error; } } /* Set the callback to get the message */ if (0 == smtp_set_messagecb(message, message_cb, &ms)) { err = PMIX_ERROR; errmsg = "smtp_set_messagecb"; goto error; } /* Send it! */ if (0 == smtp_start_session(session)) { err = PMIX_ERROR; errmsg = "smtp_start_session"; goto error; } /* Fall through */ error: if (NULL != str) { free(str); } if (NULL != session) { smtp_destroy_session(session); } /* Restore the SIGPIPE handler */ if (set_oldsig) { sigaction(SIGPIPE, &oldsig, NULL); } if (PMIX_SUCCESS != err) { int e; char em[256]; e = smtp_errno(); smtp_strerror(e, em, sizeof(em)); pmix_show_help("help-pmix-plog-smtp.txt", "send_email failed", true, "libesmtp library call failed", errmsg, em, e, msg); } return err; }