int main(int argc, char **argv, char **envp)
{
  struct in_addr addr;
  void *handle;

  if (argc != 2)
    {
      show_usage(argv[0], 1);
    }

  printf("sendmail: To: %s\n", argv[1]);
  printf("sendmail: From: %s\n", g_sender);
  printf("sendmail: Subject: %s\n", g_subject);
  printf("sendmail: Body: %s\n", g_msg_body);

  net_ipaddr(addr.s_addr, 127, 0, 0, 1);
  handle = smtp_open();
  if (handle)
    {
      smtp_configure(handle, g_host_name, &addr.s_addr);
      smtp_send(handle, argv[1], NULL, g_sender, g_subject,
                g_msg_body, strlen(g_msg_body));
      smtp_close(handle);
    }

  return 0;
}
Example #2
0
void mmsender(struct mailq_t *email)
{
	char domain[MAX_DOMAIN], *p;
	char *msg = NULL;
	struct dnscache_t *mxs_cached=NULL;
	struct mxlist_t *mxs=NULL;

	for (p=email->to; *p && *p != '@'; p++);
	if (*p++ != '@') return;
	lstrcpyn(domain, p, MAX_DOMAIN-1);

	mxs_cached = mm_get_mx(domain);
	if (mxs_cached == NULL)
		return;

	msg = msg_generate(email->to);
	if (msg == NULL) goto ex1;
	smtp_send(mxs_cached->mxs, msg);

	if (msg != NULL)
		GlobalFree((HGLOBAL)msg);
ex1:	if (mxs_cached != NULL)
		if (mxs_cached->ref > 0) mxs_cached->ref--;
	return;
}
Example #3
0
status_t SmtpTransport::Write( Mailmessage *pcMessage )
{
	status_t nError;

	if( false == m_bIsConnected )
		return ENOTCONN;

	if( NULL == pcMessage )
		return EINVAL;

	/* Create a new smtp mail */
	struct smtp_message *psSmtpMessage = smtp_create_message( pcMessage->GetAddress().c_str(), pcMessage->GetData() );
	if( NULL == psSmtpMessage )
		return EIO;

	/* Add the list of recipiants */
	std::list<os::String>vRecipiants;
	pcMessage->GetRecipiants( vRecipiants );

	std::list<os::String>::iterator i;
	for( i = vRecipiants.begin(); i != vRecipiants.end(); i++ )
		smtp_add_recipiant( psSmtpMessage, (*i).c_str() );

	/* Send it */
	nError = smtp_send( m_psSession, psSmtpMessage );
	smtp_destroy_message( psSmtpMessage );

	return nError;
}
Example #4
0
int sendmail_main(int argc, char *argv[])
{
  struct in_addr addr;
#if defined(CONFIG_EXAMPLES_SENDMAIL_NOMAC)
  uint8_t mac[IFHWADDRLEN];
#endif
  void *handle;

  printf("sendmail: To: %s\n", g_recipient);
  printf("sendmail: From: %s\n", g_sender);
  printf("sendmail: Subject: %s\n", g_subject);
  printf("sendmail: Body: %s\n", g_msg_body);

/* Many embedded network interfaces must have a software assigned MAC */

#ifdef CONFIG_EXAMPLES_SENDMAIL_NOMAC
  mac[0] = 0x00;
  mac[1] = 0xe0;
  mac[2] = 0xde;
  mac[3] = 0xad;
  mac[4] = 0xbe;
  mac[5] = 0xef;
  uip_setmacaddr("eth0", mac);
#endif

  /* Set up our host address */

  addr.s_addr = HTONL(CONFIG_EXAMPLES_SENDMAIL_IPADDR);
  uip_sethostaddr("eth0", &addr);

  /* Set up the default router address */

  addr.s_addr = HTONL(CONFIG_EXAMPLES_SENDMAIL_DRIPADDR);
  uip_setdraddr("eth0", &addr);

  /* Setup the subnet mask */

  addr.s_addr = HTONL(CONFIG_EXAMPLES_SENDMAIL_NETMASK);
  uip_setnetmask("eth0", &addr);

  /* Then send the mail */

  uip_ipaddr(addr.s_addr, 127, 0, 0, 1);
  handle = smtp_open();
  if (handle)
    {
      smtp_configure(handle, g_host_name, &addr.s_addr);
      smtp_send(handle, g_recipient, NULL, g_sender, g_subject,
                g_msg_body, strlen(g_msg_body));
      smtp_close(handle);
    }
  return 0;
}
Example #5
0
long smtp_response (void *s,char *response,unsigned long size)
{
  SENDSTREAM *stream = (SENDSTREAM *) s;
  unsigned long i,j;
  char *t,*u;
  if (response) {		/* make CRLFless BASE64 string */
    if (size) {
      for (t = (char *) rfc822_binary ((void *) response,size,&i),u = t,j = 0;
	   j < i; j++) if (t[j] > ' ') *u++ = t[j];
      *u = '\0';		/* tie off string */
      i = smtp_send (stream,t,NIL);
      fs_give ((void **) &t);
    }
    else i = smtp_send (stream,"",NIL);
  }
  else {			/* abort requested */
    i = smtp_send (stream,"*",NIL);
    stream->saslcancel = T;	/* mark protocol-requested SASL cancel */
  }
  return LONGT;
}
Example #6
0
long smtp_auth (SENDSTREAM *stream,NETMBX *mb,char *tmp)
{
  unsigned long trial,auths;
  char *lsterr = NIL;
  char usr[MAILTMPLEN];
  AUTHENTICATOR *at;
  long ret = NIL;
  for (auths = ESMTP.auth, stream->saslcancel = NIL;
       !ret && stream->netstream && auths &&
       (at = mail_lookup_auth (find_rightmost_bit (&auths) + 1)); ) {
    if (lsterr) {		/* previous authenticator failed? */
      sprintf (tmp,"Retrying using %s authentication after %.80s",
	       at->name,lsterr);
      mm_log (tmp,NIL);
      fs_give ((void **) &lsterr);
    }
    trial = 0;			/* initial trial count */
    tmp[0] = '\0';		/* empty buffer */
    if (stream->netstream) do {
      if (lsterr) {
	sprintf (tmp,"Retrying %s authentication after %.80s",at->name,lsterr);
	mm_log (tmp,WARN);
	fs_give ((void **) &lsterr);
      }
      stream->saslcancel = NIL;
      if (smtp_send (stream,"AUTH",at->name) == SMTPAUTHREADY) {
				/* hide client authentication responses */
	if (!(at->flags & AU_SECURE)) stream->sensitive = T;
	if ((*at->client) (smtp_challenge,smtp_response,"smtp",mb,stream,
			   &trial,usr)) {
	  if (stream->replycode == SMTPAUTHED) {
	    ESMTP.auth = NIL;	/* disable authenticators */
	    ret = LONGT;
	  }
				/* if main program requested cancellation */
	  else if (!trial) mm_log ("SMTP Authentication cancelled",ERROR);
	}
	stream->sensitive = NIL;/* unhide */
      }
				/* remember response if error and no cancel */
      if (!ret && trial) lsterr = cpystr (stream->reply);
    } while (!ret && stream->netstream && trial &&
	     (trial < smtp_maxlogintrials));
  }
  if (lsterr) {			/* previous authenticator failed? */
    if (!stream->saslcancel) {	/* don't do this if a cancel */
      sprintf (tmp,"Can not authenticate to SMTP server: %.80s",lsterr);
      mm_log (tmp,ERROR);
    }
    fs_give ((void **) &lsterr);
  }
  return ret;			/* authentication failed */
}
Example #7
0
SENDSTREAM *smtp_close (SENDSTREAM *stream)
{
  if (stream) {			/* send "QUIT" */
    if (stream->netstream) {	/* do close actions if have netstream */
      smtp_send (stream,"QUIT",NIL);
      if (stream->netstream) net_close (stream->netstream);
    }
				/* clean up */
    if (stream->host) fs_give ((void **) &stream->host);
    if (stream->reply) fs_give ((void **) &stream->reply);
    if (ESMTP.dsn.envid) fs_give ((void **) &ESMTP.dsn.envid);
    if (ESMTP.atrn.domains) fs_give ((void **) &ESMTP.atrn.domains);
    fs_give ((void **) &stream);/* flush the stream */
  }
  return NIL;
}
Example #8
0
void smtp_disconnect()
{
	smtp_send("QUIT\r\n");
	ano_sockclose(smail.sock);
}
Example #9
0
/* Send the email */
int smtp_send_email()
{
	char buf[1024];
	if (!smtp_read(buf, 1024))
	{
		alog("SMTP: error reading buffer");
		return 0;
	}

	int code = smtp_get_code(buf);
	if (code != 220)
	{
		alog("SMTP: error expected code 220 got %d",code);
		return 0;
	}

	if (!smtp_send("HELO anope\r\n"))
	{
		alog("SMTP: error writting to socket");
		return 0;
	}

	if (!smtp_read(buf, 1024))
	{
		alog("SMTP: error reading buffer");
		return 0;
	}

	code = smtp_get_code(buf);
	if (code != 250)
	{
		alog("SMTP: error expected code 250 got %d",code);
		return 0;
	}

	strcpy(buf, "MAIL FROM: <");
	strcat(buf, smail.from.c_str());
	strcat(buf, ">\r\n");

	if (!smtp_send(buf))
	{
		alog("SMTP: error writting to socket");
		return 0;
	}

	if (!smtp_read(buf, 1024))
	{
		alog("SMTP: error reading buffer");
		return 0;
	}

	code = smtp_get_code(buf);
	if (code != 250)
		return 0;

	strcpy(buf, "RCPT TO: <");
	strcat(buf, smail.to.c_str());
	strcat(buf, ">\r\n");

	if (!smtp_send(buf))
	{
		alog("SMTP: error writting to socket");
		return 0;
	}

	if (!smtp_read(buf, 1024))
	{
		alog("SMTP: error reading buffer");
		return 0;
	}

	code = smtp_get_code(buf);
	if (smtp_get_code(buf) != 250)
	{
		alog("SMTP: error expected code 250 got %d",code);
		return 0;
	}

	if (!smtp_send("DATA\r\n"))
	{
		alog("SMTP: error writting to socket");
		return 0;
	}

	if (!smtp_read(buf, 1024))
	{
		alog("SMTP: error reading buffer");
		return 0;
	}

	code = smtp_get_code(buf);
	if (code != 354)
	{
		alog("SMTP: error expected code 354 got %d",code);
		return 0;
	}

	for (std::vector<std::string>::const_iterator it = smail.smtp_headers.begin(), it_end = smail.smtp_headers.end(); it != it_end; ++it)
		if (!smtp_send(it->c_str()))
		{
			alog("SMTP: error writting to socket");
			return 0;
		}

	if (!smtp_send("\r\n"))
	{
		alog("SMTP: error writting to socket");
		return 0;
	}

	bool skip_done = false;
	for (std::vector<std::string>::const_iterator it = smail.smtp_body.begin(), it_end = smail.smtp_body.end(); it != it_end; ++it)
		if (skip_done)
		{
			if (!smtp_send(it->c_str()))
			{
				alog("SMTP: error writting to socket");
				return 0;
			}
		}
		else
			skip_done = true;

	if (!smtp_send("\r\n.\r\n"))
	{
		alog("SMTP: error writting to socket");
		return 0;
	}

	return 1;
}
Example #10
0
/*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();
}
Example #11
0
long smtp_rcpt (SENDSTREAM *stream,ADDRESS *adr,long *error)
{
 char *s,tmp[2*MAILTMPLEN],orcpt[MAILTMPLEN];
  while (adr) {			/* for each address on the list */
				/* clear any former error */
    if (adr->error) fs_give ((void **) &adr->error);
    if (adr->host) {		/* ignore group syntax */
				/* enforce SMTP limits to protect the buffer */
      if (strlen (adr->mailbox) > MAXLOCALPART) {
	adr->error = cpystr ("501 Recipient name too long");
	*error = T;
      }
      else if ((strlen (adr->host) > SMTPMAXDOMAIN)) {
	adr->error = cpystr ("501 Recipient domain too long");
	*error = T;
      }
#ifndef RFC2821			/* old code with A-D-L support */
      else if (adr->adl && (strlen (adr->adl) > SMTPMAXPATH)) {
	adr->error = cpystr ("501 Path too long");
	*error = T;
      }
#endif

      else {
	strcpy (tmp,"TO:<");	/* compose "RCPT TO:<return-path>" */
#ifdef RFC2821
	rfc822_cat (tmp,adr->mailbox,NIL);
	sprintf (tmp + strlen (tmp),"@%s>",adr->host);
#else				/* old code with A-D-L support */
	rfc822_address (tmp,adr);
	strcat (tmp,">");
#endif
				/* want notifications */
	if (ESMTP.ok && ESMTP.dsn.ok && ESMTP.dsn.want) {
				/* yes, start with prefix */
	  strcat (tmp," NOTIFY=");
	  s = tmp + strlen (tmp);
	  if (ESMTP.dsn.notify.failure) strcat (s,"FAILURE,");
	  if (ESMTP.dsn.notify.delay) strcat (s,"DELAY,");
	  if (ESMTP.dsn.notify.success) strcat (s,"SUCCESS,");
				/* tie off last comma */
	  if (*s) s[strlen (s) - 1] = '\0';
	  else strcat (tmp,"NEVER");
	  if (adr->orcpt.addr) {
	    sprintf (orcpt,"%.498s;%.498s",
		     adr->orcpt.type ? adr->orcpt.type : "rfc822",
		     adr->orcpt.addr);
	    sprintf (tmp + strlen (tmp)," ORCPT=%.500s",orcpt);
	  }
	}
	switch (smtp_send (stream,"RCPT",tmp)) {
	case SMTPOK:		/* looks good */
	  break;
	case SMTPUNAVAIL:	/* mailbox unavailable? */
	case SMTPWANTAUTH:	/* wants authentication? */
	case SMTPWANTAUTH2:
	  if (ESMTP.auth) return T;
	default:		/* other failure */
	  *error = T;		/* note that an error occurred */
	  adr->error = cpystr (stream->reply);
	}
      }
    }
    adr = adr->next;		/* do any subsequent recipients */
  }
  return NIL;			/* no retry called for */
}
Example #12
0
long smtp_verbose (SENDSTREAM *stream)
{
				/* accept any 2xx reply code */
  return ((smtp_send (stream,"VERB","ON") / (long) 100) == 2) ? LONGT : NIL;
}
Example #13
0
long smtp_mail (SENDSTREAM *stream,char *type,ENVELOPE *env,BODY *body)
{
  RFC822BUFFER buf;
  char tmp[SENDBUFLEN+1];
  long error = NIL;
  long retry = NIL;
  buf.f = smtp_soutr;		/* initialize buffer */
  buf.s = stream->netstream;
  buf.end = (buf.beg = buf.cur = tmp) + SENDBUFLEN;
  tmp[SENDBUFLEN] = '\0';	/* must have additional null guard byte */
  if (!(env->to || env->cc || env->bcc)) {
  				/* no recipients in request */
    smtp_seterror (stream,SMTPHARDERROR,"No recipients specified");
    return NIL;
  }
  do {				/* make sure stream is in good shape */
    if (retry) {		/* need to retry with authentication? */
      NETMBX mb;
				/* yes, build remote name for authentication */
      sprintf (tmp,"{%.200s/smtp%s}<none>",
	       (long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
	       ((long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
		net_remotehost (stream->netstream) :
		net_host (stream->netstream)) :
	       stream->host,
	       (stream->netstream->dtb ==
		(NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL)) ?
	       "/ssl" : "");
      mail_valid_net_parse (tmp,&mb);
      smtp_send (stream,"RSET",NIL);
      if (!smtp_auth (stream,&mb,tmp)) return NIL;
      retry = NIL;		/* no retry at this point */
    }

    strcpy (tmp,"FROM:<");	/* compose "MAIL FROM:<return-path>" */
#ifdef RFC2821
    if (env->return_path && env->return_path->host &&
	!((strlen (env->return_path->mailbox) > SMTPMAXLOCALPART) ||
	  (strlen (env->return_path->host) > SMTPMAXDOMAIN))) {
      rfc822_cat (tmp,env->return_path->mailbox,NIL);
      sprintf (tmp + strlen (tmp),"@%s",env->return_path->host);
    }
#else				/* old code with A-D-L support */
    if (env->return_path && env->return_path->host &&
	!((env->return_path->adl &&
	   (strlen (env->return_path->adl) > SMTPMAXPATH)) ||
	  (strlen (env->return_path->mailbox) > SMTPMAXLOCALPART) ||
	  (strlen (env->return_path->host) > SMTPMAXDOMAIN)))
      rfc822_address (tmp,env->return_path);
#endif
    strcat (tmp,">");
    if (ESMTP.ok) {
      if (ESMTP.eightbit.ok && ESMTP.eightbit.want)
	strcat (tmp," BODY=8BITMIME");
      if (ESMTP.dsn.ok && ESMTP.dsn.want) {
	strcat (tmp,ESMTP.dsn.full ? " RET=FULL" : " RET=HDRS");
	if (ESMTP.dsn.envid)
	  sprintf (tmp + strlen (tmp)," ENVID=%.100s",ESMTP.dsn.envid);
      }
    }
				/* send "MAIL FROM" command */
    switch (smtp_send (stream,type,tmp)) {
    case SMTPUNAVAIL:		/* mailbox unavailable? */
    case SMTPWANTAUTH:		/* wants authentication? */
    case SMTPWANTAUTH2:
      if (ESMTP.auth) retry = T;/* yes, retry with authentication */
    case SMTPOK:		/* looks good */
      break;
    default:			/* other failure */
      smtp_send (stream,"RSET",NIL);
      return NIL;
    }
				/* negotiate the recipients */
    if (!retry && env->to) retry = smtp_rcpt (stream,env->to,&error);
    if (!retry && env->cc) retry = smtp_rcpt (stream,env->cc,&error);
    if (!retry && env->bcc) retry = smtp_rcpt (stream,env->bcc,&error);
    if (!retry && error) {	/* any recipients failed? */
      smtp_send (stream,"RSET",NIL);
      smtp_seterror (stream,SMTPHARDERROR,"One or more recipients failed");
      return NIL;
    }
  } while (retry);
				/* negotiate data command */
  if (!(smtp_send (stream,"DATA",NIL) == SMTPREADY)) {
    smtp_send (stream,"RSET",NIL);
    return NIL;
  }
				/* send message data */
  if (!rfc822_output_full (&buf,env,body,
			   ESMTP.eightbit.ok && ESMTP.eightbit.want)) {
    smtp_fake (stream,"SMTP connection broken (message data)");
    return NIL;			/* can't do much else here */
  }
				/* send trailing dot */
  if (smtp_send (stream,".",NIL) != SMTPOK) {
    smtp_send (stream,"RSET",NIL);
    return NIL;
  }
  return LONGT;
}
Example #14
0
SENDSTREAM *smtp_open_full (NETDRIVER *dv,char **hostlist,char *service,
			    unsigned long port,long options)
{
  SENDSTREAM *stream = NIL;
  long reply;
  char *s,tmp[MAILTMPLEN];
  NETSTREAM *netstream;
  NETMBX mb;
  if (!(hostlist && *hostlist)) mm_log ("Missing SMTP service host",ERROR);
				/* maximum domain name is 64 characters */
  else do if (strlen (*hostlist) < SMTPMAXDOMAIN) {
    sprintf (tmp,"{%.1000s}",*hostlist);
    if (!mail_valid_net_parse_work (tmp,&mb,service ? service : "smtp") ||
	mb.anoflag || mb.readonlyflag) {
      sprintf (tmp,"Invalid host specifier: %.80s",*hostlist);
      mm_log (tmp,ERROR);
    }
    else {			/* light tryssl flag if requested */
      mb.trysslflag = (options & SOP_TRYSSL) ? T : NIL;
				/* explicit port overrides all */
      if (mb.port) port = mb.port;
				/* else /submit overrides port argument */
      else if (!compare_cstring (mb.service,"submit")) {
	port = SUBMITTCPPORT;	/* override port, use IANA name */
	strcpy (mb.service,"submission");
      }
				/* else port argument overrides SMTP port */
      else if (!port) port = smtp_port ? smtp_port : SMTPTCPPORT;
      if (netstream =		/* try to open ordinary connection */
	  net_open (&mb,dv,port,
		    (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL),
		    "*smtps",smtp_sslport ? smtp_sslport : SMTPSSLPORT)) {
	stream = (SENDSTREAM *) memset (fs_get (sizeof (SENDSTREAM)),0,
					sizeof (SENDSTREAM));
	stream->netstream = netstream;
	stream->host = cpystr ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
			       net_host (netstream) : mb.host);
	stream->debug = (mb.dbgflag || (options & OP_DEBUG)) ? T : NIL;
	if (options & SOP_SECURE) mb.secflag = T;
				/* get name of local host to use */
	s = compare_cstring ("localhost",mb.host) ?
	  net_localhost (netstream) : "localhost";

	do reply = smtp_reply (stream);
	while ((reply < 100) || (stream->reply[3] == '-'));
	if (reply != SMTPGREET){/* get SMTP greeting */
	  sprintf (tmp,"SMTP greeting failure: %.80s",stream->reply);
	  mm_log (tmp,ERROR);
	  stream = smtp_close (stream);
	}
				/* try EHLO first, then HELO */
	else if (((reply = smtp_ehlo (stream,s,&mb)) != SMTPOK) &&
		 ((reply = smtp_send (stream,"HELO",s)) != SMTPOK)) {
	  sprintf (tmp,"SMTP hello failure: %.80s",stream->reply);
	  mm_log (tmp,ERROR);
	  stream = smtp_close (stream);
	}
	else {
	  NETDRIVER *ssld =(NETDRIVER *)mail_parameters(NIL,GET_SSLDRIVER,NIL);
	  sslstart_t stls = (sslstart_t) mail_parameters(NIL,GET_SSLSTART,NIL);
	  ESMTP.ok = T;		/* ESMTP server, start TLS if present */
	  if (!dv && stls && ESMTP.service.starttls &&
	      !mb.sslflag && !mb.notlsflag &&
	      (smtp_send (stream,"STARTTLS",NIL) == SMTPGREET)) {
	    mb.tlsflag = T;	/* TLS OK, get into TLS at this end */
	    stream->netstream->dtb = ssld;
				/* TLS started, negotiate it */
	    if (!(stream->netstream->stream = (*stls)
		  (stream->netstream->stream,mb.host,
		   (mb.tlssslv23 ? NIL : NET_TLSCLIENT) |
		   (mb.novalidate ? NET_NOVALIDATECERT:NIL)))){
				/* TLS negotiation failed after STARTTLS */
	      sprintf (tmp,"Unable to negotiate TLS with this server: %.80s",
		       mb.host);
	      mm_log (tmp,ERROR);
				/* close without doing QUIT */
	      if (stream->netstream) net_close (stream->netstream);
	      stream->netstream = NIL;
	      stream = smtp_close (stream);
	    }
				/* TLS OK, re-negotiate EHLO */
	    else if ((reply = smtp_ehlo (stream,s,&mb)) != SMTPOK) {
	      sprintf (tmp,"SMTP EHLO failure after STARTTLS: %.80s",
		       stream->reply);
	      mm_log (tmp,ERROR);
	      stream = smtp_close (stream);
	    }
	    else ESMTP.ok = T;	/* TLS OK and EHLO successful */
	  }
	  else if (mb.tlsflag) {/* user specified /tls but can't do it */
	    sprintf (tmp,"TLS unavailable with this server: %.80s",mb.host);
	    mm_log (tmp,ERROR);
	    stream = smtp_close (stream);
	  }

				/* remote name for authentication */
	  if (stream && ((mb.secflag || mb.user[0]))) {
	    if (ESMTP.auth) {	/* use authenticator? */
	      if ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL)) {
				/* remote name for authentication */
		strncpy (mb.host,
			 (long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
			 net_remotehost (netstream) : net_host (netstream),
			 NETMAXHOST-1);
		mb.host[NETMAXHOST-1] = '\0';
	      }
	      if (!smtp_auth (stream,&mb,tmp)) stream = smtp_close (stream);
	    }
	    else {		/* no available authenticators? */
	      sprintf (tmp,"%sSMTP authentication not available: %.80s",
		       mb.secflag ? "Secure " : "",mb.host);
	      mm_log (tmp,ERROR);
	      stream = smtp_close (stream);
	    }
	  }
	}
      }
    }
  } while (!stream && *++hostlist);
  if (stream) {			/* set stream options if have a stream */
    if (options &(SOP_DSN | SOP_DSN_NOTIFY_FAILURE | SOP_DSN_NOTIFY_DELAY |
		  SOP_DSN_NOTIFY_SUCCESS | SOP_DSN_RETURN_FULL)) {
      ESMTP.dsn.want = T;
      if (options & SOP_DSN_NOTIFY_FAILURE) ESMTP.dsn.notify.failure = T;
      if (options & SOP_DSN_NOTIFY_DELAY) ESMTP.dsn.notify.delay = T;
      if (options & SOP_DSN_NOTIFY_SUCCESS) ESMTP.dsn.notify.success = T;
      if (options & SOP_DSN_RETURN_FULL) ESMTP.dsn.full = T;
    }
    if (options & SOP_8BITMIME) ESMTP.eightbit.want = T;
  }
  return stream;
}
Example #15
0
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;
}
Example #16
0
bool smtp_client::write(const char* data, size_t len)
{
	return smtp_send(client_, data, len) == 0 ? true : false;
}