Logo Search packages:      
Sourcecode: nbsmtp version File versions  Download package

int send_mail ( string_t msg,
servinfo_t serverinfo,
string_t rcpts 
)

Opens a socket and sends all data to the relayhost.

Parameters:
[in] msg A pointer to the buffer with the headers
[in] serverinfo A pointer to a servinfo_t struct holding all information we need
[in] rcpts An array with all recipients
Returns:
1 if something goes wrong and 0 in other case

Definition at line 434 of file original.c.

References servinfo::auth_pass, servinfo::auth_user, servinfo::fromaddr, get_socket(), log_msg(), nbsmtp_auth(), nbsmtp_data(), nbsmtp_ehlo(), nbsmtp_helo(), nbsmtp_rcpts(), smtp_exit(), smtp_last_message(), smtp_okay(), smtp_write(), and servinfo::sockfd.

Referenced by main().

{
      int (*greet_f)(servinfo_t *);
      char *local_out_buf;          /* Only for protocol commands */

      if(get_socket(serverinfo) <= 0)
      {
            return 1;
      }

      if(!smtp_okay(serverinfo))
      {
            log_msg(LOG_ERR,"Error after connect and before sending HELO/EHLO");
            return 1;
      }

      greet_f = (serverinfo->auth_user ? nbsmtp_ehlo : nbsmtp_helo);

      if(!greet_f(serverinfo))
      {
            /* If we wrote EHLO and it failed, we try HELO w/o auth before die */
            if (serverinfo->auth_user)
            {
                  if(!nbsmtp_helo(serverinfo))
                  {
                        return 1;
                  }
                  else
                  {
                        /* HELO worked, so we NULL all authentication vars and warn to syslog */
                        serverinfo->auth_user = (char)NULL;
                        serverinfo->auth_pass = (char)NULL;

                        log_msg(LOG_WARNING,"nbSMTP will continue without authentication");

#ifdef HAVE_SSL
                        /* Without EHLO there is no STARTTLS posibility */
                        if (serverinfo->use_starttls==True)
                        {
                              serverinfo->use_starttls=False;
                              log_msg(LOG_WARNING,"EHLO command failed, no STARTTLS will be used");
                        }
#endif
                  }
            }
      }

#ifdef HAVE_SSL
      if (serverinfo->use_starttls==True)
      {
            asprintf(&local_out_buf,"STARTTLS");

            if (smtp_write(serverinfo,local_out_buf)<1)
            {
                  log_msg(LOG_ERR,"Error writting STARTTLS command to the socket");
                  return 1;
            }

            free(local_out_buf);

            if (!smtp_okay(serverinfo))
            {
                  log_msg(LOG_ERR,"STARTTLS not supported by server, exiting");
                  return 1;
            }

            nbsmtp_sslinit(serverinfo);

            if(!greet_f(serverinfo))
            {
                  log_msg(LOG_DEBUG,"Error after SSL connection was stablished");
                  return 1;
            }
      }
#endif

      if (serverinfo->auth_user && serverinfo->auth_pass)
      {
            if(!nbsmtp_auth(serverinfo))
            {
                  return 1;
            }
      }

      asprintf(&local_out_buf,"MAIL FROM:<%s>",serverinfo->fromaddr);

      if(smtp_write(serverinfo, local_out_buf)<1)
      {
            log_msg(LOG_ERR,"Error writting MAIL command to the socket");
            return 1;
      }

      free(local_out_buf);

      if(!smtp_okay(serverinfo))
      {
            log_msg(LOG_ERR,"Error issuing MAIL command");
            log_msg(LOG_ERR,"Server said: '%s'",smtp_last_message());

            return 1;
      }

      if(nbsmtp_rcpts(serverinfo,rcpts)<0)
      {
            return 1;
      }

      if(nbsmtp_data(serverinfo,msg)<0)
      {
            return 1;
      }

      asprintf(&local_out_buf,"%s","QUIT");
      
      if(smtp_write(serverinfo, local_out_buf)<1)
      {
            return 1;
      }

      free(local_out_buf);

      /* Read bye message to avoid warning on some MTAs */
      smtp_okay(serverinfo);

      smtp_exit();

      close(serverinfo->sockfd);

#ifdef HAVE_SSL
      nbsmtp_sslexit(serverinfo);
#endif

      return 0;
}


Generated by  Doxygen 1.6.0   Back to index