status_t SmtpTransport::Connect( void ) { status_t nError; if( m_bIsConnected ) return EISCONN; m_psSession = smtp_create_session( m_cServer.c_str(), m_nPort, SMTP_DEFAULT_TIMEOUT, 0 ); if( NULL == m_psSession ) return ENOTCONN; if( m_cUsername == "" ) nError = smtp_connect( m_psSession, NULL, NULL ); else nError = smtp_connect( m_psSession, m_cUsername.c_str(), m_cPassword.c_str() ); if( nError != 0 ) { smtp_destroy_session( m_psSession ); m_psSession = NULL; } else m_bIsConnected = true; return nError; }
static int deliver_message(const char *service, DELIVER_REQUEST *request) { SMTP_STATE *state; int result; if (msg_verbose) msg_info("deliver_message: from %s", request->sender); /* * Sanity checks. The smtp server is unprivileged and chrooted, so we can * afford to distribute the data censoring code, instead of having it all * in one place. */ if (request->nexthop[0] == 0) msg_fatal("empty nexthop hostname"); if (request->rcpt_list.len <= 0) msg_fatal("recipient count: %d", request->rcpt_list.len); /* * Initialize. Bundle all information about the delivery request, so that * we can produce understandable diagnostics when something goes wrong * many levels below. The alternative would be to make everything global. */ state = smtp_state_alloc(); state->request = request; state->src = request->fp; state->service = service; state->misc_flags |= smtp_addr_pref; SMTP_RCPT_INIT(state); /* * Establish an SMTP session and deliver this message to all requested * recipients. At the end, notify the postmaster of any protocol errors. * Optionally deliver mail locally when this machine is the best mail * exchanger. */ result = smtp_connect(state); /* * Clean up. */ smtp_state_free(state); return (result); }
/* Main entry point */ void smtp_alert(real_server_t * rs, vrrp_t * vrrp, vrrp_sgroup_t * vgroup, const char *subject, const char *body) { smtp_t *smtp; /* Only send mail if email specified */ if (!LIST_ISEMPTY(global_data->email) && global_data->smtp_server.ss_family != 0) { /* allocate & initialize smtp argument data structure */ smtp = (smtp_t *) MALLOC(sizeof(smtp_t)); smtp->subject = (char *) MALLOC(MAX_HEADERS_LENGTH); smtp->body = (char *) MALLOC(MAX_BODY_LENGTH); smtp->buffer = (char *) MALLOC(SMTP_BUFFER_MAX); smtp->email_to = (char *) MALLOC(SMTP_BUFFER_MAX); /* format subject if rserver is specified */ if (rs) { snprintf(smtp->subject, MAX_HEADERS_LENGTH, "[%s] Realserver [%s]:%d - %s" , global_data->router_id , inet_sockaddrtos(&rs->addr) , ntohs(inet_sockaddrport(&rs->addr)) , subject); } else if (vrrp) snprintf(smtp->subject, MAX_HEADERS_LENGTH, "[%s] VRRP Instance %s - %s" , global_data->router_id , vrrp->iname , subject); else if (vgroup) snprintf(smtp->subject, MAX_HEADERS_LENGTH, "[%s] VRRP Group %s - %s" , global_data->router_id , vgroup->gname , subject); else if (global_data->router_id) snprintf(smtp->subject, MAX_HEADERS_LENGTH, "[%s] %s" , global_data->router_id , subject); else snprintf(smtp->subject, MAX_HEADERS_LENGTH, "%s", subject); strncpy(smtp->body, body, MAX_BODY_LENGTH); build_to_header_rcpt_addrs(smtp); smtp_connect(smtp); } }
int main(int argc, char *argv[]) { /* Win32 stuff */ #ifdef _WIN32 WSADATA wsa; #endif if (argc == 1) return 0; if (argc == 3 && !strcmp(argv[2], "--debug")) smtp_debug = 1; char *server = strtok(argv[1], ":"), *aport; short port; if ((aport = strtok(NULL, ""))) port = atoi(aport); else port = 25; if (!server) { alog("No Server"); /* Bad, bad, bad. This was a return from main with no value! -GD */ return 0; } else alog("SMTP: server %s port %d",server,port); /* The WSAStartup function initiates use of WS2_32.DLL by a process. */ /* guessing we can skip it under *nix */ #ifdef _WIN32 if (WSAStartup(MAKEWORD(1, 1), &wsa)) return 0; #endif char buf[8192]; bool headers_done = false; /* Read the message and parse it */ while (fgets(buf, 8192, stdin)) { if (smtp_is_header(buf) && !headers_done) { smail.smtp_headers.push_back(strip(buf) + "\r\n"); std::string header, value; smtp_parse_header(buf, header, value); if (header == "From:") { alog("SMTP: from: %s", value.c_str()); smail.from = value; } else if (header == "To:") { alog("SMTP: to: %s", value.c_str()); smtp_set_to(value); } else if (smtp_is_end(buf)) break; else { headers_done = true; smail.smtp_body.push_back(strip(buf) + "\r\n"); } } else smail.smtp_body.push_back(strip(buf) + "\r\n"); } if (!smtp_connect(server, port)) { alog("SMTP: failed to connect to %s:%d", server, port); return 0; } if (!smtp_send_email()) { alog("SMTP: error during sending of mail"); return 0; } smtp_disconnect(); return 1; }
/*Attempt to send a message*/ void SMTP_SendThread( void * dummy ) { int l_nReturn; SMTP_lock(); /*Connect to the SMTP server.*/ l_nReturn = smtp_connect( g_pClient, g_szServer, 25 ); if ( l_nReturn != NSMAIL_OK ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } l_nReturn = smtp_processResponses( g_pClient ); if ( l_nReturn != NSMAIL_OK || g_serverError == TRUE ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } /*Send the EHLO command passing in the domain name.*/ l_nReturn = smtp_ehlo( g_pClient, g_szDomain ); if ( l_nReturn != NSMAIL_OK ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } l_nReturn = smtp_processResponses( g_pClient ); if ( l_nReturn != NSMAIL_OK || g_serverError == TRUE ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } /*Send the MAIL FROM command.*/ l_nReturn = smtp_mailFrom( g_pClient, g_szSender, NULL ); if ( l_nReturn != NSMAIL_OK ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } l_nReturn = smtp_processResponses( g_pClient ); if ( l_nReturn != NSMAIL_OK || g_serverError == TRUE ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } /*Send the RCPT TO command.*/ l_nReturn = smtp_rcptTo( g_pClient, g_szRecipient, NULL ); if ( l_nReturn != NSMAIL_OK ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } l_nReturn = smtp_processResponses( g_pClient ); if ( l_nReturn != NSMAIL_OK || g_serverError == TRUE ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } /*Send the DATA command.*/ l_nReturn = smtp_data( g_pClient ); if ( l_nReturn != NSMAIL_OK ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } l_nReturn = smtp_processResponses( g_pClient ); if ( l_nReturn != NSMAIL_OK || g_serverError == TRUE ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } /* Send the message.*/ l_nReturn = smtp_send( g_pClient, g_szData ); if ( l_nReturn != NSMAIL_OK ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } l_nReturn = smtp_processResponses( g_pClient ); if ( l_nReturn != NSMAIL_OK || g_serverError == TRUE ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } l_nReturn = smtp_quit( g_pClient ); if ( l_nReturn != NSMAIL_OK ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } l_nReturn = smtp_processResponses( g_pClient ); if ( l_nReturn != NSMAIL_OK || g_serverError == TRUE ) { printf( "smtptest.c::SMTP_SendThread" ); SMTP_unlock(); return; } SMTP_unlock(); }
int main( int argc, char *argv[] ) { int ret; struct smtp_session *session; struct smtp_message *message; char *server, *from, *to, *data; int bytes; if( argc < 4 ) { printf("DemoApp [server] [from] [to]\n"); return EXIT_FAILURE; } server = argv[1]; from = argv[2]; to = argv[3]; printf("Connecting to %s\n", server ); session = smtp_create_session( server, SMTP_DEFAULT_PORT, SMTP_DEFAULT_TIMEOUT, 0 ); if( NULL == session ) { printf("smtp_create_session() failed\n"); return EXIT_FAILURE; } ret = smtp_connect( session ); if( ret ) { printf("smtp_connect() failed\n"); printf("%s", session->buffer ); return EXIT_FAILURE; } printf("%s", session->buffer ); data = (char*)malloc( 4096 ); if( NULL == data ) { printf("Couldn't malloc() data, but this is nothing to do with libsmtp!\n"); return EXIT_FAILURE; } printf("Enter the message to be sent. End with CTRL+D\n"); memset( data, 0, 4096 ); for( bytes = 0; bytes < 406; ++bytes ) { int c = getchar(); if( c == EOF ) break; *(data + bytes) = c; } printf("\nMessage to be sent is as follows"); printf("\n--\n%s\n--\n", data ); message = smtp_create_message( from, data ); smtp_add_recipiant( message, to ); ret = smtp_send( session, message ); if( ret ) { printf("smtp_send() failed\n"); printf("%s", session->buffer ); return EXIT_FAILURE; } printf("%s", session->buffer ); smtp_destroy_message( message ); smtp_disconnect( session ); printf("%s", session->buffer ); smtp_destroy_session( session ); return EXIT_SUCCESS; }